Monday, February 25, 2013

SharePoint 2010 C# set document set permissions

Ever needed to change permissions to a document set? Here's a sample of how you do it.

e.g. document set on http://sharepoint/Site1/Library1/DocumentSet1
Example of Parameters:
siteUrl:
webRelativeUrl:
libraryName:
folderName:
itemUrl:
roleMaps:
removeExistingPermission:



     public string SetItemLevelPermission(string siteUrl, string webRelativeUrl, string libraryName, string folderName,   
       string itemUrl, string roleMaps, bool removeExistingPermission)  
     {  
       // Pass in a string of permissions?  
       // <Role Definition>:<GroupName1>|<GroupName2>,<Role Definition>:<GroupName1>|<GroupName2>  
       // e.g. Design-Purchasing  
       string output = "";  
       try  
       {  
         SPSecurity.RunWithElevatedPrivileges(delegate()  
         {  
           using (SPSite oSiteCollection = new SPSite(siteUrl))  
           {  
             using (SPWeb oSPWeb = oSiteCollection.OpenWeb(webRelativeUrl))  
             {  
               oSPWeb.AllowUnsafeUpdates = true;  
               SPList list = oSPWeb.Lists.TryGetList(libraryName);  
               SPQuery query = new SPQuery();  
               query.Query = "<Where><Eq><FieldRef Name=\"Title\" /><Value Type=\"Text\">" + folderName + "</Value></Eq></Where>";  
               query.ViewFields = "";  
               query.RowLimit = 1;  
               SPListItemCollection items = list.GetItems(query);  
               SPListItem item = items[0];  
               // Break permission inheritance  
               if (!item.HasUniqueRoleAssignments)  
               {  
                 item.BreakRoleInheritance(true);  
               }  
               if (removeExistingPermission)  
               {  
                 SPRoleAssignmentCollection currentRoleAssignments = item.RoleAssignments;  
                 for (int i = currentRoleAssignments.Count - 1; i >= 0; i--)  
                 {  
                   currentRoleAssignments.Remove(i);  
                 }  
               }  
               // webroledefinitions – Full Right, Design, Contribute and Read  
               SPRoleDefinitionCollection webRoleDef = oSPWeb.RoleDefinitions;  
               // Add for groups  
               string[] roleMappings = roleMaps.Split(',');  
               foreach (string m in roleMappings)  
               {  
                 string[] arr = m.Split(':');  
                 string roleDefinition = arr[0];  
                 string[] groups = arr[1].Split('|');  
                 foreach (string g in groups)  
                 {  
                   SPPrincipal principal = getUserOrGroup(g, oSPWeb.SiteGroups, oSiteCollection);  
                   SPRoleAssignment roleAssignment = new SPRoleAssignment(principal);  
                   //SPGroup group = oSPWeb.SiteGroups[g];  
                   //SPRoleAssignment roleAssignment = new SPRoleAssignment(group);  
                   roleAssignment.RoleDefinitionBindings.Add(webRoleDef[roleDefinition]);  
                   item.RoleAssignments.Add(roleAssignment);  
                 }  
               }  
               oSPWeb.AllowUnsafeUpdates = false;  
             }  
           }  
         });  
         output = "OK";  
       }  
       catch (Exception e)  
       {  
         output = output + e.ToString();  
       }  
       return output;  
      }  
     private SPPrincipal getUserOrGroup(string userOrGroup, SPGroupCollection groups, SPSite site)  
     {  
       SPPrincipal myUser = null;  
       try  
       {  
         if (SPUtility.IsLoginValid(site, userOrGroup))  
         {  
           myUser = site.RootWeb.EnsureUser(userOrGroup);  
         }  
         else  
         {  
           myUser = groups[userOrGroup];  
         }  
       }  
       catch (Exception e)  
       {  
       }  
       return myUser;  
     }  
Related Posts Plugin for WordPress, Blogger...