CSOM code is raising this error “Version conflict.” when trying to update ListItems inside foreach

Valued Contributor

I have the following CSOM code:-

using (ClientContext context2 = Helpers.GetAppOnlyContext(properties.ItemEventProperties.WebUrl))
                {
                    try
                    {
                        //set unique permsion for the risk Value + Asset item
                        RoleDefinition readerDef = context2.Web.RoleDefinitions.GetByType(RoleType.Reader);
                        RoleDefinitionBindingCollection readOnlyBinding = new RoleDefinitionBindingCollection(context2);
                        readOnlyBinding.Add(readerDef);

                        RoleDefinition contributeDef = context2.Web.RoleDefinitions.GetByType(RoleType.Contributor);
                        RoleDefinitionBindingCollection contributeBinding = new RoleDefinitionBindingCollection(context2);
                        contributeBinding.Add(contributeDef);
                        CamlQuery camlQuery = new CamlQuery();
                        camlQuery.ViewXml = string.Format("<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"RiskValueAsset\" LookupId=\"TRUE\" /><Value Type=\"Lookup\">{0}</Value></Eq></Where></Query></View>", listItemID);

                        context2.Load(context2.Web);
                        context2.ExecuteQuery();
                        ListItemCollection listItemCollection = context2.Web.GetList(context2.Web.ServerRelativeUrl + "/lists/RiskValue").GetItems(camlQuery);

                        context2.Load(listItemCollection, items => items.Include(
                                            item => item.Id,
                                            item => item["Title"],
                                            item => item.RoleAssignments
                                           ));
                        context2.ExecuteQuery();

                        ListItem listItem = context2.Web.GetList(context2.Web.ServerRelativeUrl + "/lists/Assets").GetItemById(listItemID);
                        context2.Load(listItem);
                        context2.ExecuteQuery();
                        FieldUserValue creator = listItem["Author"] as FieldUserValue;
                        foreach (ListItem listItem2 in listItemCollection)
                        {
                            
                            
                            listItem2.BreakRoleInheritance(false, false);
                            listItem2.RoleAssignments.Add(context2.Web.EnsureUser(creator.LookupValue), new RoleDefinitionBindingCollection(context2) { contributeDef });
                            listItem2.RoleAssignments.Add(context2.Site.RootWeb.SiteGroups.GetByName("Risk Users"), new RoleDefinitionBindingCollection(context2) { readerDef });
                            listItem2.RoleAssignments.Add(context2.Site.RootWeb.SiteGroups.GetByName("Risk Admins"), new RoleDefinitionBindingCollection(context2) { contributeDef });
                            listItem2.Update();
                            context2.ExecuteQuery();

                        }                           
                    }

now the foreach will run once and set the RoleAssignment, but on the second iteration it will raise this exception "Version conflict.".. any advice?

I also tried to replace the foreach with for loop as follow, but did not fix the issue:-

for (int i = 0; i < listItemCollection.Count ; i++)
                        {


                            listItemCollection[i].BreakRoleInheritance(false, false);
                            listItemCollection[i].RoleAssignments.Add(context2.Web.EnsureUser(creator.LookupValue), new RoleDefinitionBindingCollection(context2) { contributeDef });
                            listItemCollection[i].RoleAssignments.Add(context2.Site.RootWeb.SiteGroups.GetByName("Risk Users"), new RoleDefinitionBindingCollection(context2) { readerDef });
                            listItemCollection[i].RoleAssignments.Add(context2.Site.RootWeb.SiteGroups.GetByName("Risk Admins"), new RoleDefinitionBindingCollection(context2) { contributeDef });
                            listItemCollection[i].SystemUpdate();
                            context2.ExecuteQuery();

                        }
0 Replies