Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
log.Info("Inheriting permissions");
//Inherit permissions if broken
rListitem.ResetRoleInheritance();
transList.ResetRoleInheritance();
log.Info("Now let's start breaking the permissions again");
// Break permissions
rListitem.BreakRoleInheritance(false, false);
transList.BreakRoleInheritance(false, false);
RoleAssignmentCollection collRoleAssign = destList.RoleAssignments;
context.ExecuteQuery();
collRoleAssign.Add(author, new RoleDefinitionBindingCollection(context){web.RoleDefinitions.GetByType(RoleType.Contributor)});
collRoleAssign.Add(editor, new RoleDefinitionBindingCollection(context){web.RoleDefinitions.GetByType(RoleType.Contributor)});
applog.LogInformation("Update finised for created by and modified by");
try{
FieldUserValue[] fldContributors = (FieldUserValue[])rListitem["Contributors"];
if(fldContributors != null)
{
applog.LogInformation($"{fldContributors.Length} Contributors found. Applying updates");
for (int i = 0; i < fldContributors.Length; i++)
{
try
{
var userInfoList = context.Site.RootWeb.SiteUserInfoList;
var userInfo = userInfoList.GetItemById(fldContributors[i].LookupId);
context.Load(userInfo,info => info.ContentType);
context.ExecuteQuery();
string userType = "Person";
if(userInfo != null)
userType = userInfo.ContentType.Name;
if(userType == "Person" || userType == "DomainGroup")
{
var fldContributoruser = web.EnsureUser(fldContributors[i].LookupValue);
if(fldContributors[i].LookupValue.ToString() != authorUser.LookupValue.ToString() || fldContributors[i].LookupValue.ToString() != modifiedUser.LookupValue.ToString())
{
collRoleAssign.Add(fldContributoruser, new RoleDefinitionBindingCollection(context){web.RoleDefinitions.GetByType(RoleType.Contributor)});
}
try
{
Group visitorsGroup = grpCollection.GetByName("<Group Name>");
UserCollection collUser = visitorsGroup.Users;
collUser.AddUser(fldContributoruser);
context.Load(fldContributoruser);
context.Load(visitorsGroup);
context.ExecuteQuery();
}catch(Exception ex8){applog.LogError($"Error at Visitors group Add: {ex8.Message}");}
}
else if(userType == "SharePointGroup")
{
var primaryGroup = grpCollection.GetByName(fldContributors[i].LookupValue);
collRoleAssign.Add(primaryGroup, new RoleDefinitionBindingCollection(context){web.RoleDefinitions.GetByType(RoleType.Contributor)});
rListitem.RoleAssignments.Add(primaryGroup, new RoleDefinitionBindingCollection(context){web.RoleDefinitions.GetByType(RoleType.Contributor)});
}
}
catch(Exception ex3)
{applog.LogError($"Error at Contributor Creation: {ex3.Message}");}
}
applog.LogInformation("Contributors processing finished");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.