Creation of bulk document library and grant permission on Document library at a same time

ネパリ サンデャ 500 Reputation points
2023-06-15T05:45:21.1466667+00:00

I found script of bulk creation of document library and i tried and worked

https://www.sharepointdiary.com/2019/04/powershell-to-create-multiple-folders-in-document-library-from-csv.html

I could not find script grant permission to user on document library using CSV

Now one of my customer wants to create bulk document library at the same time grant user permission on that document library

Is there any script to do so

I try multiple script but didnot work

I am very new to understand the script structure may be that could be the reason that i cannot able to create right script

Can anyone got an idea for this?

Microsoft 365 and Office | SharePoint Server | For business
Microsoft 365 and Office | Install, redeem, activate | For business | Windows
Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

Accepted answer
  1. Yanli Jiang - MSFT 31,611 Reputation points Microsoft External Staff
    2023-06-19T09:50:52.6+00:00

    Hi @ネパリ サンデャ ,

    Please try this script:

    #Config Variables
    $SiteURL = "https://tenant.sharepoint.com/sites/amy12345"
    
    $filePath = "C:\Users\spadmin\Desktop\pnp.csv"
    
    #Get the CSV file
    $csv = Import-CSV $filePath
     
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive  
    
    ForEach($row in $csv)
    {
    
            Write-host -f Yellow "Creating Document Library:"$row.library
            New-PnPList -Title $row.library -Template DocumentLibrary -ErrorAction Stop
            Set-PnPList -Identity $row.library -CopyRoleAssignments:$False -BreakRoleInheritance -ListExperience NewExperience
    }
     
    
    ForEach($row in $csv) {
            Set-PnPListPermission -Identity $row.library -AddRole $row.permission -User $row.user
    }
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Yanli Jiang - MSFT 31,611 Reputation points Microsoft External Staff
    2023-06-15T09:46:10.04+00:00

    Hi @ネパリ サンデャ ,

    According to the link you provided, it is to create folders in batches in a document library, right?

    Then I understand that your requirement is to grant permissions in batches to this document library, right?

    If so, you can try the following these steps:

    1, Create a csv file like this:

    06153

    2, Run below PowerShell script:

    #Config Variables
    $SiteURL = "https://domain.sharepoint.com/sites/Amy12345/"
    $ListName ="library 1"
    
    $filePath = "C:\Users\spadmin\Desktop\pnp.csv" 
    
    $csv = Import-CSV $filePath
     
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive  
     
    #Break Permission Inheritance of the List
    Set-PnPList -Identity $ListName -BreakRoleInheritance -CopyRoleAssignments
     
    #Grant permission on Library to User
    ForEach($Row in $csv) {
             Set-PnPListPermission -Identity $ListName -AddRole $Row.permission -User $Row.user
    }  
    

    The complete process of running the script:

    06154

    The result:

    06155


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Yanli Jiang - MSFT 31,611 Reputation points Microsoft External Staff
    2023-06-16T09:18:24.44+00:00

    Hi @ネパリ サンデャ ,

    For the file format you provide, you can do as follow steps:

    1, Make the csv file like this:

    06162

    2,Run below PowerShell script:

    #Config Variables
    $SiteURL = "https://domian.sharepoint.com/sites/Amy12345/"
    
    $filePath = "C:\Users\spadmin\Desktop\pnp.csv" 
    
    #Get the CSV file
    $csv = Import-CSV $filePath
     
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive  
    
    ForEach($row in $csv)
    {
        Try {
            #Create document library
            Write-host -f Yellow "Creating Document Library:"$row.libraryname
            New-PnPList -Title $row.libraryname -Template DocumentLibrary -ErrorAction Stop
     
            #Set Library Properties
            Set-PnPList -Identity $row.libraryname -EnableVersioning $True -EnableMinorVersions $True -CopyRoleAssignments:$False -BreakRoleInheritance -ListExperience NewExperience
            $Library  = Get-PnPList -Identity $row.libraryname
     
            #Exclude from Search Results
            $Library.NoCrawl = $True
            $Library.Update()
        
          
     
            #Grant permission on Library to User
            ForEach($row in $csv) {
                Set-PnPListPermission -Identity $row.libraryname -AddRole $row.permission -User $row.user
            }
         Write-host -f Green "`tCreated Document Library:"$row.libraryname
        }
        Catch {
            write-host -f Red "`tError:" $_.Exception.Message
        }
    }
    

    The result:

    06164

    06165

    06166

    Note: If you want to grant multiple user permissions to a document library at the same time, it is recommended to use the script I provided before. Separate creation from granting permissions.

    Again, please hide your private information when posting, such as domain, etc.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.