Skip to content

Instantly share code, notes, and snippets.

@AsishP
Created January 25, 2018 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AsishP/57e15d528f2ed5666808bd6331dce052 to your computer and use it in GitHub Desktop.
Save AsishP/57e15d528f2ed5666808bd6331dce052 to your computer and use it in GitHub Desktop.
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