我使用ajax調用更新SharePoint列表的web服務。更新Sharepoint列表項時,從Web服務調用時出錯
它的工作原理,當我從單元測試調用的代碼,但運行在瀏覽器中的代碼會導致一個例外:
System.InvalidOperationException:操作無效由於對象的當前狀態。 在Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext的上下文) 在Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(HttpContext的上下文) 在Microsoft.SharePoint.SPContext.get_Current() 在Microsoft.SharePoint.SPListItem.AddOrUpdateItem在Microsoft.SharePoint.SPListItem(布爾BADD,布爾bSystem,布爾bPreserveItemVersion,布爾bNoVersion,布爾bMigration,布爾bPublish,布爾bCheckOut,布爾bCheckin,的Guid newGuidOnAdd,的Int32 & ULID,對象& objAttachmentNames,對象& objAttachmentContents,布爾suppressAfterEvents) .UpdateInternal(Boolean bSystem,Boolean bPreserveItemVersion,Guid newGuidOnAdd,Boolean bMigration,Boolean bPublish,Boolean bNoVersion,Boolean bCheckOut,Bo奧利安bCheckin,布爾suppressAfterEvents) 在Microsoft.SharePoint.SPListItem.Update()
我的代碼更新列表項是:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteURL))
{
using (SPWeb web = site.OpenWeb(path))
{
SPList userProfile = web.Lists[userList];
SPQuery qry = new SPQuery
{
Query =
"<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" +
accountName +
"</Value></Eq></Where><ViewFields><FieldRef Name='ID' /><FieldRef Name='Title' /><FieldRef Name='LastUpdated' /><FieldRef Name='Reason' /></ViewFields>"
};
SPListItemCollection spListItemCollection = userProfile.GetItems(qry);
if (spListItemCollection.Count == 1)
{
//edit user
SPListItem item = spListItemCollection[0];
item["Updated"] = DateTime.Now;
item["Reason"] = updateReason;
item.Update();
}
}
}
});
上item.Update
它錯誤();
當您運行單元測試時,它是否在與Web服務相同的帳戶憑據下執行?我想知道這是否是一個安全問題。 – CBono 2010-10-27 12:48:00
它對兩者使用相同的憑據。 – 2010-10-27 14:48:11