Ошибка обновления элемента списка Sharepoint при вызове из веб-службы - PullRequest
2 голосов
/ 27 октября 2010

Я использую ajax для вызова веб-службы, которая обновляет список sharepoint.

Это работает, когда я вызываю код из модульных тестов, но запуск кода в браузере вызывает исключение:

System.InvalidOperationException: операция недопустима из-за текущего состояния объекта.в Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl (контекст HttpContext) в Microsoft.SharePoint.WebControls.SPControl.GetContextWeb (контекст HttpContext) в Microsoft.SharePoint.SPContext.get_Current ().Логическое bSystem, булева bPreserveItemVersion, булева bNoVersion, булева bMigration, булева bPublish, булева bCheckOut, булева bCheckin, Guid newGuidOnAdd, Int32 & ulID, объект и objAttachmentNames, объект и objAttachmentContents, Boolean suppressAfterEvents) при Microsoft.SharePoint.SPListItem.UpdateInternal (булево bSystem, булева bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents) в Microsoft.SharePoint.SPListItem.Update ()

* 1008 - обновить список * до 1007 *

1009 *

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 ();

Ответы [ 2 ]

1 голос
/ 27 октября 2010

Попробуйте добавить это :

HttpContext context = HttpContext.Current;
if (HttpContext.Current != null)
{
    if (context.Items["HttpHandlerSPWeb"] == null)
        context.Items["HttpHandlerSPWeb"] = site.RootWeb;
    if (context.Items["Microsoft.Office.ServerContext"] == null)
        context.Items["Microsoft.Office.ServerContext"] = ServerContext.GetContext(site);
}
0 голосов
/ 28 октября 2010

Проблема была с безопасностью. Необходимо добавить следующую строку (хотя и не идеально)

web.AllowUnsafeUpdates = true;

Я тоже убрал строку

SPSecurity.RunWithElevatedPrivileges(delegate()

и изменил SPSite и SPWeb, чтобы они не использовали «using».

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...