I'm trying to work with privileges on elements of a list. I want the item to mantain the same privileges for all SPPrincipal I don't modify.
This is the code
public static void AssignPermissionsToItem(SPListItem item, SPPrincipal obj, SPRoleType roleType)
{
if (!item.HasUniqueRoleAssignments)
{
item.BreakRoleInheritance(false, true);
}
SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
item.RoleAssignments.Add(roleAssignment);
}
This code (proposed to me by Alexander), destroys all the permissions and add the permission specified as parameter..
To mantain the past privileges I must set
item.BreakRoleInheritance(true, true);
which mantains the old privileges.
I tried to make a RemoveAll on the roleAssignment before the add of the new one, but it mantains also the old privileges, like RemoveAll didn't to anything!
This is the code:
public static void AddPermissionsToItem(SPListItem item, SPPrincipal obj, SPRoleType roleType)
{
if (!item.HasUniqueRoleAssignments)
{
item.BreakRoleInheritance(true, true);
}
SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
roleAssignment.RoleDefinitionBindings.RemoveAll();
roleAssignment.Update();
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
item.RoleAssignments.Add(roleAssignment);
}
My code (simplyfied):
[WebMethod]
public ResponseObject InsertInAllReplyers(string username)
{
ResponseObject SPResultStatus = new ResponseObject();
try
{
SPUserToken myToken = TokenManager.GetToken(username);
//ATTENZIONE!! SOLO PER LA MACCHINA LOCALE DIEGO VA MANTENUTO LO SPAZIO!!!
string fullsite = SPContext.Current.Web.Url + "/sites/Reply Corp";
//ATTENZIONE!! SOLO PER LA MACCHINA LOCALE DIEGO VA MANTENUTO LO SPAZIO!!!
using (SPSite site = new SPSite(fullsite, myToken))
{
site.AllowUnsafeUpdates = true;
using (SPWeb currentWeb = site.OpenWeb("All Replyers"))
{
currentWeb.AllowUnsafeUpdates = true;
SPList targetList = currentWeb.Lists["Contenuti"];
SPListItem listItem = targetList.AddItem();
listItem["Title"] = "Prova :)";
listItem["tt_contentid"] = "TT_ID252";
listItem["Tag"] = "Tag2";
listItem.Update();
var allreplyers = currentWeb.Groups["All Replyers"];
AssignPermissionsToItem(listItem, (SPPrincipal)allreplyers, SPRoleType.Reader);
AssignPermissionsToItem(listItem, (SPPrincipal)currentWeb.AllUsers[username], SPRoleType.Contributor);
SPResultStatus.SPResultStatus = ResponseObjectSPResultStatus.SUCCESS;
SPResultStatus.SPResultUrl = listItem.Url;
SPResultStatus.SPResultId = listItem.ID.ToString();
SPResultStatus.SPResultType = ResponseObjectSPResultType.LIST;
}
}
}
catch (Exception exx)
{
return new ResponseObject() { SPResultStatus = ResponseObjectSPResultStatus.FAILURE, Message = exx.Message, Stacktrace = exx.StackTrace, ExceptionType = exx.GetType().ToString() };
}
return SPResultStatus;
}
public static void AssignPermissionsToItem(SPListItem item, SPPrincipal obj, SPRoleType roleType)
{
if (!item.HasUniqueRoleAssignments)
{
item.BreakRoleInheritance(false, true);
}
item.RoleAssignments.Remove(obj);
item.SystemUpdate();
SPRoleAssignment roleAssignment = new SPRoleAssignment(obj);
SPRoleDefinition roleDefinition = item.Web.RoleDefinitions.GetByType(roleType);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
item.RoleAssignments.Add(roleAssignment);
}