I have the following CSOM code inside my remote event receiver:
ListItem listItem = context2.Web.GetList(context2.Web.ServerRelativeUrl + "/lists/Assets").GetItemById(listItemID);
context2.Load(listItem);
context2.ExecuteQuery();
FieldUserValue creator = listItem("Author") as FieldUserValue;
listItem.BreakRoleInheritance(false, false);
listItem.RoleAssignments.Add(context2.Web.EnsureUser(creator.LookupValue), new RoleDefinitionBindingCollection(context2) { contributeDef });
The above code will get a list item >> assign the author of the item inside the listitem permission. The code will work on 99% of the cases.. but we face this case:-
- there is an internal user named
user.ABC
- there is another external user named also
user.ABC
So my above code will always select the internal username, even if the author was the external user.
So can I improve my code by passing the user id to the EnsureUser
method? or another way to assign the Author of a list item to the list item permission?