0

Using the below code, I am able to update the Publications column but not the Authors column as it returns

Invalid data has been used to update the list item. The field you are trying to update may be read only.

Is there a more obvious way to work with these columns, I'm constantly guessing a naming convention, etc.

        List NewsAlertsList = clientContext.Web.Lists.GetByTitle(SharePointConnector.sp_NewsAlerts_list);

        // This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll" 
        // so that it grabs all list items, regardless of the folder they are in.                 
        CamlQuery CamlQuery = new CamlQuery();
        string qs = String.Format("<View><RowLimit>{0}</RowLimit><Query><OrderBy><FieldRef Name=\"ID\" Ascending =\"False\"/></OrderBy></Query></View>", 500);
        CamlQuery.ViewXml = qs;

        ListItemCollection z = NewsAlertsList.GetItems(CamlQuery);

        // Retrieve all items in the ListItemCollection from List.GetItems(Query). 
        clientContext.Load(z,
                          items => items.Include(
                           item => item["Publication"],
                           item => item["Author"]));
        clientContext.ExecuteQuery();

        ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
        ListItem oListItem = NewsAlertsList.AddItem(itemCreateInfo);

        oListItem["to"] = newsalert.to;
        oListItem["url"] = newsalert.url;
        oListItem["subject"] = newsalert.subject;
        oListItem["Title"] = newsalert.title;
        oListItem["source"] = newsalert.source;
        oListItem["Category"] = newsalert.categories;
        oListItem["userid"] = newsalert.userid;
        oListItem["comments"] = newsalert.comments;
        oListItem["Language"] = newsalert.language;

        var p = clientContext.Web.Lists.GetByTitle("ADMPANewsAlertsPublications").GetItemById(Convert.ToInt32(newsalert.publicationID));
        var a = clientContext.Web.Lists.GetByTitle("ADMPANewsAlertsAuthors").GetItemById(Convert.ToInt32(newsalert.authorID));

        oListItem["Publication"] = p;
        oListItem["Author"] = a;

        oListItem.Update();

        clientContext.ExecuteQuery();
  • Seems like you are referencing a 2007 assembly, was this an upgrade project? Try replacing any reference to Microsoft.Sharepoint.Security with the assembly on any 2010 sharepoint server here: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI – Mike Jun 29 '17 at 17:23
  • I dont have access to the server. I will update my question because I got this partially working so I have a more specific issue now. – Tim Cadieux Jun 29 '17 at 20:10
  • It turns out the code bove does work, it was my Author Column was for some reason called Author0, once i changed that line, it now updates – Tim Cadieux Jun 30 '17 at 0:10
  • It turns out the code bove does work, it was my Author Column was for some reason called Author0, once i changed that line, it now updates – Tim Cadieux Jun 30 '17 at 0:10
0

You can update the People field as follows

Here is working code

ListItem item = ctx.Web.Lists.GetByTitle("test").GetItemById(6);
item["Author"] = 21;
item.Update();

ctx.ExecuteQuery();

As per this you can update your code as follows

oListItem["Publication"] = newsalert.publicationID;
oListItem["Author"] = newsalert.authorID;
  • 1
    Unfortunately, I'm not really sure how to use this. I already call the ListItem using var a = clientContext.Web.Lists.GetByTitle("ADMPANewsAlertsAuthors").GetItemById(Convert.ToInt32(newsalert.authorID)); but for some reasonbn I cannot submit it the same way I do the Publication? I have to assume there is something in how the Author table is created that is causing this? – Tim Cadieux Jun 29 '17 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.