Как мне обновить ListItem или перезагрузить его с URL - PullRequest
0 голосов
/ 12 февраля 2019

Проблема: В foreach ниже у меня есть 2 части.

Часть 1 открывает файл Excel и изменяет его. Часть 2 обновляет 3 из полей ListItem

Обе части работают по отдельности, но при их включении часть 2 не может Microsoft.SharePoint.Client.ServerException: 'Version conflict.' понятно

Вопрос: Как я могу обновить / перезагрузить свой ListItem между частью 1 и частью 2?

Я попробовал ListItem.RefreshLoad(); И я что-то искал по всем направлениямиз ListItem.GetFromURL(); но не могу найти то, что я ищу

            ClientContext cc = new ClientContext(site)
            SecureString pass = new SecureString();
            Web web = cc.Web;

            foreach (char c in "password".ToCharArray())
            {
                pass.AppendChar(c);
            }

            cc.Credentials = new SharePointOnlineCredentials("User@Domain.com", pass);

            Microsoft.SharePoint.Client.List list = web.Lists.GetByTitle("MyLibrary");

            CamlQuery qry = new CamlQuery
            {
                ViewXml = "<View><Query><Where><Contains><FieldRef Name='Active'/><Value Type='Boolean'>1</Value></Contains></Where></Query></View>"
            };
            Microsoft.SharePoint.Client.ListItemCollection listItems = list.GetItems(qry);

            cc.Load(listItems);
            cc.ExecuteQuery();

            foreach (Microsoft.SharePoint.Client.ListItem listItem in listItems)
            {

                //PART 1
                //Open the file
                //Modify the File
                //Save the file

                //PART 2
                listItem["Updated"] = DateTime.Now;
                listItem["Update_x0020_Duration"] = totalTime;
                listItem["System_x0020_Comment"] = "TestComment";
                listItem.Update();
                cc.ExecuteQuery();
            }

1 Ответ

0 голосов
/ 12 февраля 2019

Из моего опыта Microsoft.SharePoint.Client пространство имен может быть немного сложнее.Я боролся с той же проблемой в прошлом.Это общая идея, основанная на вашем коде.пожалуйста, прочитайте комментарии:

    ClientContext cc = new ClientContext(site);
    // try to use the List object, using the name of the list as the parameter
    List oList = cc.Web.Lists.GetByTitle(listName);
    // pass the credantials!
    cc.Credentials = new System.Net.NetworkCredential(< username >, < password >, < domain >);
    // another option - default NetworkCredentials credantials
    // context.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

    // pass the uniq id of the record
    ListItem listItem = oList.GetItemById(id);
    // now you can to this
    // ...............
    listItem["System_x0020_Comment"] = "TestComment";
    listItem.Update();
    cc.ExecuteQuery();
...