Struggling to set permissions on a listitem… everything appears to be working fine.. no errors.. but when checking the document on sharepoint.. the permission groups im adding are not set.
any idea what i’m missing?
sharepoint dev is a real nightmare!
public void SetDocumentPermission(ListItem doc, List<Group> newGroup)
{
using (var clientContext = GetNewContext())
{
var rdb = new RoleDefinitionBindingCollection(clientContext);
var rootWeb = clientContext.Web;
clientContext.Load(rootWeb);
clientContext.ExecuteQuery();
foreach (var group in newGroup)
{
try
{
var usr = (Principal)group;
var roll = rootWeb.RoleDefinitions.GetByType(RoleType.Reader);
clientContext.Load(roll);
clientContext.ExecuteQuery();
rdb.Add(roll);
if (!doc.HasUniqueRoleAssignments)
{
doc.BreakRoleInheritance(true, true);
}
doc.RoleAssignments.Add(usr, rdb);
doc.Update();
clientContext.ExecuteQuery();
}
catch (Exception exception)
{
throw new ApplicationException(exception.Message);
}
}
}
}
thanks
ok i resolved this problem by setting the Client context at class level rather than method.
i’m not sure if there is some sort of binding done between listitem and the ctx.. but as i returned the listitem from another method (which also had a using around the ctx) and passed it into this permissions method.. the listitem appeared to lose it’s connection.