csom iterate sharepoint list item and update field
-
13-01-2021 - |
Frage
I have list with 10000 record and want to update field "Business" whose value is AAA to BBB. for this I need to iterate each list items and update. Below is my code.
ClientContext clientContext = new ClientContext(siteUrl);
Web oWebsite = clientContext.Web;
List oList = clientContext.Web.Lists.GetByTitle("List name");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<Where> <Eq> <FieldRef Name='Business' /> <Value Type='Text'>AAA</Value> </Eq> </Where>";
ListItemCollection items = oList.GetItems(camlQuery);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (ListItem item in items)
{
item["Business"] = "BBB";
item.Update();
clientContext.ExecuteQuery();
}
where I am doing wrong> in caml Can someone help to modify to correct code
Lösung
Your Caml Query ViewXml
property is missing <View>
and <Query>
tags
ClientContext clientContext = new ClientContext(siteUrl);
Web oWebsite = clientContext.Web;
List oList = clientContext.Web.Lists.GetByTitle("List name");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View><Query><Where><Eq><FieldRef Name='Business' /><Value Type='Text'>AAA</Value></Eq></Where></Query></View>";
ListItemCollection items = oList.GetItems(camlQuery);
clientContext.Load(items);
clientContext.ExecuteQuery();
foreach (ListItem item in items)
{
item["Business"] = "BBB";
item.Update();
clientContext.ExecuteQuery();
}
Andere Tipps
You have to modify the camlquery to retrieve the AAA items alone and then you can modify the value without any condition,
Query = @"<Where> <Eq> <FieldRef Name='Business' /> <Value Type='Text'>AAA</Value> </Eq> </Where>"
Then modify the for loop code as below,
foreach (ListItem item in items) {
item["Business"] = 'BBB';
item.Update();
}
Please let me know, whether the above code solves your issue.
You are fetching the data where business is equal to EBL, don't know its a typo mistake or not, if you change EBL to AAA in the query so I don't think so there is need of if condition. Apart from a query, I don't find any mistake in the code.