locked
Setting Document Library Permissions Programatically. RRS feed

  • Question

  • Is it possible to programatically manage and set the folder permissions within a document library? I can manually edit the permissions through the web UI but can't work out how to stop inheritance and set custom permissions and roles.

     

    I've written something similar to set custom permissions in Powershell for a List but can't work out the structure of the Document Library..

     

    Code Block

    #Load the Sharepoint .net Assembly
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

     

    #set the url of the site collection to a variable
    $siteurl = "http://MOSS/sites/customer/"

    #create the new object passing the site collection URL
    $mysite=new-object Microsoft.SharePoint.SPSite($siteurl)

     

    #Open the subsite
    $subsite = $mysite.openweb("SUBSITE")

     

    #open the list Techwiki
    $mylist = $subsite.lists["Techwiki"]

     

    #Pass $mylist to FL to view the contents
    #$mylist | FL

     

    #break inheritance from the top level foler.
    $mylist.BreakRoleInheritance($false)

     

    #List the different roles

    $mylist.RoleAssignments

     

    #Complicated stuff to build the new assignment and role. 

    $spRoleAssignment = New-Object microsoft.sharepoint.SPRoleAssignment("corp2\sps-all","all@corp2.com","Technical USer","User Notes")
    $sproledefinition = $mylist.RoleAssignments
    $sproledefinition = $mylist.RoleAssignment
    $sproledefinition = $subsite.RoleDefinitions["Contribute"]
    $sproleassignment.RoleDefinitionBindings.Add($sproledefinition)

     

    #add the role assignment to the list
    $mylist.RoleAssignments.Add($sproleassignment)

     

    #update the list.
    $mylist.update()

    $mylist.RoleAssignments

     

     

    Examples in Powershell would be brilliant but I can usually translate C# examples eventually...

     

     

    • Moved by Mike Walsh FIN Tuesday, April 13, 2010 7:08 AM programming q (From:SharePoint - Design and Customization (pre-SharePoint 2010))
    Friday, November 9, 2007 12:58 PM

Answers

  • $mylist.BreakRoleInheritance($false) does not break inheritance, you must use $true there.

    Or something that I have in C#:
     
    Code Block

    foreach (SPListItem item in docLib.Folders)

    {

    ...
    ...
      SPUser sui = site.AllUsers[<UserName>];
      SPRoleDefinition RoleDefinition = site.RoleDefinitions[<Role>];
      SPRoleAssignment RoleAssignment = new SPRoleAssignment(sui.LoginName, sui.Email, sui.Name, sui.Notes);
      RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
     
      if (!item.HasUniqueRoleAssignments) item.BreakRoleInheritance(true);
      item.RoleAssignments.Add(RoleAssignment);
      item.Update();
      
    }     

    • Proposed as answer by shridha Monday, January 4, 2010 8:23 AM
    • Marked as answer by Mike Walsh FIN Tuesday, April 13, 2010 7:07 AM
    Tuesday, November 13, 2007 6:24 AM