About Document library permission using csv

ネパリ サンデャ 500 Reputation points
2023-06-16T08:28:10.6333333+00:00

I want to ask if there is any way to grant permission on Document library using CSV?

I want to grant permission on document library using following CSV

I could not find any way i tried following csv with

following script but now work well

Can anyone tell me the correct way to grant permission on document library using csv format?

#Config Variables
$SiteURL = "https://domain.sharepoint.com/sites/Amy12345/"
$ListNames ="library 1","library 2","library 3"

$filePath = "C:\Users\spadmin\Desktop\pnp.csv" 

$csv = Import-CSV $filePath
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive  

ForEach ($ListName in $ListNames){
    #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
    }
}

User's image

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,229 questions
0 comments No comments
{count} votes

Accepted answer
  1. Emily Du-MSFT 51,826 Reputation points Microsoft External Staff
    2023-06-19T06:43:47.2666667+00:00

    1.Create a csv file as following picture show.

    1

    2.Please run below PNP PowerShell.

    $SiteURL = "https://tenant.sharepoint.com/sites/emilytestcom"
    $CSVFile = "C:\pnp.csv"
    
    Connect-PnPOnline -Url $SiteURL -Interactive
     
    $CSVData = Import-CSV $CSVFile
    
    ForEach($Row in $CSVData)
    {
         #Get the user
         $Users = $Row.user
    
         ForEach($User in $Users)
         {     
               Set-PnPList -Identity $Row.library -BreakRoleInheritance -CopyRoleAssignments
               Set-PnPListPermission -Identity $Row.library -AddRole $Row.permission -User $User
                
         }
       
    }
    

    Result:

    2

    3


    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

0 additional answers

Sort by: Most helpful

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.