在部署到SharePoint的WCF服务中创建SPListItem
我在WCF服务中有以下方法,该方法已使用Shail Malik的指南部署到SharePoint:在部署到SharePoint的WCF服务中创建SPListItem,wcf,security,sharepoint,service,Wcf,Security,Sharepoint,Service,我在WCF服务中有以下方法,该方法已使用Shail Malik的指南部署到SharePoint: [OperationContract] public string AddItem(string itemTitle, Guid? idOfListToUse) { using (var portal = new SPSite(SPContext.Current.Site.Url, SPContext.Current.Site.SystemAccount.UserToken)) {
[OperationContract]
public string AddItem(string itemTitle, Guid? idOfListToUse)
{
using (var portal = new SPSite(SPContext.Current.Site.Url, SPContext.Current.Site.SystemAccount.UserToken))
{
using (var web = portal.OpenWeb())
{
Guid listId;
web.AllowUnsafeUpdates = true;
if (idOfListToUse != null && idOfListToUse.Value != new Guid())
{
listId = idOfListToUse.Value;
}
else
{
try
{
listId = new Guid(web.Properties[PropertyBagKeys.TagsList]);
}
catch (Exception ex)
{
throw new MyException("No List Id for the tag list (default list) has been found!", ex);
}
}
var list = web.Lists[listId];
string title = "";
SPSecurity.RunWithElevatedPrivileges(delegate{
var newItem = list.Items.Add();
newItem["Title"] = itemTitle;
newItem.Update();
title = newItem.Title;
});
web.AllowUnsafeUpdates = false;
return title;
}
}
}
当从Javascript(使用Rick Strahl的优秀ServiceProxy.js)调用该方法时,它会失败,并且会在newItem.Update()上失败,因为ValidateFormDigest()
这是一个关键,当我一步一步地完成代码时,它是有效的!没有例外 我认为您无法访问“列表”,因为它是在提升的代码块之外创建的 我猜,当您正在逐步执行整个流程时,整个流程处于管理模式,因此所有流程都处于提升状态。好的,找到了答案(有2个偶数:-D) 首先,肮脏的一个: 在上下文中设置FormDigestValidatedProperty:
HttpContext.Current.Items["FormDigestValidated"] = true;
第二,稍微不那么脏的版本(基本上为XSS攻击留出了空间,但这毕竟是一个内部网)
科林,尝试在WCF服务中访问HttpContext(类似于SPContext)是一个非常糟糕的主意。请看这里: 从文章中: HttpContext:当前值始终为空 从WCF中访问时 服务 很可能这就是你问题的原因
编辑:我注意到您试图使用SPContext获取网站集的url。我也没有找到一个很好的解决方案,所以我只是将目标网站集的url作为参数发送到服务调用。不是最理想的解决方案,但我想不出更好的办法。另外,如果您需要检查身份验证/标识等,请使用
ServiceSecurityContext.Current
RunWithElevatedPriviliges只是一个试用,整个代码块都在一个using语句中,该语句使用SystemAccount的UserToken打开SPSIte。我已将aspNetCompatibilityEnabled设置为true,它将服务放入asp.net管道中。我不需要一个“精确”的上下文,只需要足够的url和systemaccount的usertoken。与这些我打开一个新的SPSite。