3

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

  • what is the error you are getting? – Vineet Desai Apr 4 '18 at 9:59
  • I am not getting any error. – Brishal Apr 4 '18 at 13:41
2

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();
        }
  • yes I am using that, forget to update in code, updated now. Field update is happening. But it has updated all rows to BBB irrespective to AAA. I only want to update value of field which has value AAA – Brishal Apr 4 '18 at 14:01
  • Have updated caml query view xml in the answer above. your query was missing "View" and "Query" xml tags. – Vineet Desai Apr 5 '18 at 3:58
  • Thanks lot Vineet, it helped. just one more thing I want to add 2 parameter like if Business = AAA change to BBB and if business = XXX change to ZZZ. how to pass 2 parameter – Brishal Apr 5 '18 at 7:38
  • Modify your CAML query to add an Or clause <View><Query><Where><Or><Eq><FieldRef Name='Business' /><Value Type='Text'>AAA</Value></Eq><Eq><FieldRef Name='Business' /><Value Type='Text'>XXX</Value></Eq></Or></Where></Query></View> – Vineet Desai Apr 5 '18 at 8:49
  • item["Business"] == "AAA" ? item["Business"] = "BBB" : item["Business"] = "ZZZ"; item.Update(); clientContext.ExecuteQuery(); – Vineet Desai Apr 5 '18 at 8:52
2

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.

  • I have updated my code above(in question as per your query). It runs fine but value does not get updated – Brishal Apr 4 '18 at 13:42
  • Field update is happening. But it has updated all rows to BBB irrespective to AAA. I only want to update value of field which has value AAA – Brishal Apr 4 '18 at 14:01
0

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.

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.