Home > Back-end >  Sharepoint change listitem field from c#
Sharepoint change listitem field from c#

Time:06-21

I need to update sharepoint list item url. There is no errors but it does not work

ClientContext context = new ClientContext("http://www.sharepoint-site");
List myList = context.Web.Lists.GetByTitle("myListName");
            
CamlQuery query = CamlQuery.CreateAllItemsQuery();
ListItemCollection items = myList.GetItems(query);
            
context.Load(items);
context.ExecuteQuery();

for (int i = 0; i < items.Count; i  )
{
    ListItem li = items[i];

    object obj = li.FieldValues["_x0421__x0441__x044b__x043b__x04"]; 
    //my field key
    FieldUrlValue url = (FieldUrlValue)obj;
    url.Url = url.Url.Replace("usr","users");
    li.FieldValues["_x0421__x0441__x044b__x043b__x04"] = url;

    li.Update();
    context.Load(li);
    context.ExecuteQuery();
}

Sharepoint 2010 version

CodePudding user response:

For me the code works if I remove context.Load(li); This line is not needed at all. Just call li.Update() and then context.ExecuteQuery();

for (int i = 0; i < items.Count; i  )
{
    ListItem li = items[i];

    object obj = li.FieldValues["_x0421__x0441__x044b__x043b__x04"]; 
    //my field key
    FieldUrlValue url = (FieldUrlValue)obj;
    url.Url = url.Url.Replace("usr","users");
    li.FieldValues["_x0421__x0441__x044b__x043b__x04"] = url;

    li.Update();
    context.ExecuteQuery();
}
  • Related