I have a site in which unique permission group are there.
I want to update the list item as per the changes made in the user group in site, i.e. if user is removed from the site user group then the user from the list item should also be deleted.
My code is:
if (groupDeleted.Name.EndsWith(UserGroups.Managers.Name))
{
DeleteUserFromList(userDeleted, FieldNames.ProjectManager, item, properties.Web);
}
The method is:
private void DeleteUserFromList(SPUser userDeleted, string fieldName, SPListItem item, SPWeb sPWeb)
{
try
{
SPFieldUserValueCollection Users = item[fieldName] as SPFieldUserValueCollection;
SPFieldUserValue DeletedUserValue = Users.First(u => u.LookupId == userDeleted.ID);
Users.Remove(DeletedUserValue);
item[fieldName] = Users;
item.Update();
item.ParentList.Update();
}
catch { }
}
The code works fine if I delete single user from the group. But when I delete multiple users then I am getting error : Save Conflict
.
One more thing I need to clarify that when I add two users and debug the program then the entire code is executed 2 times(i.e. 2 cycles) but when I delete two users then each statement gets executed two times.
What am I missing?