SharePoint Online Restricted View with PnpUpload in a script

vincent manzari 41 Reputation points
2023-01-30T13:22:02.6766667+00:00

Hello all,

we are developing a powershell script that it create a sharepoint site, folder structure within the Document Library and manage permissions (create SPO groups, break inheritance and set permissions based on the requirements).

All works well but we have an issue with the Restrictec View permissions. We need to set the restricted view for 1 folder in the structure. For what we know, to enable the Restricted View, it's needed to upload a file and after that the restricted View is available in the sets of permissions. We have tested the upload through the web interface and works. We have tried to do this through the powershell script but seems doesn't work.

In the script, first we upload a file from a local path with the PnP command

Add-PnPFile -Path $localPath -Folder "Shared Documents".

The upload works well but it seems that if the upload is generated by powershell the Restricted View permission is not available, so we cannot user the script to set permissions.

have you some idea about this behaviour?

Thanks

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,219 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,296 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yanli Jiang - MSFT 29,041 Reputation points Microsoft Vendor
    2023-01-31T09:28:45.3466667+00:00

    Hi @vincent manzari ,

    You cannot use PnP PowerShell to directly grant users "Restricted View" permissions to folders and files in a SharePoint Online document library. Because the "Restricted View" permission is a hidden permission level as shown below(Not displayed by default in the Permission level).

    But you can do that by adding the " Restricted View " permission level and then using powershell. There are steps:

    1. Library setting-> permission for this document library -> Add " Restricted View " permission level.(You need to Select the permissions to include in this permission level yourself.)
    2. Use the following PnP powershell code:
    #Config Variables
    $SiteURL = "https://domain.sharepoint.com/sites/xxx"
    $ListName="Documents"
    $FolderServerRelativeURL = "/sites/xxx/Shared Documents/xxx"
    $UserAccount = "******@domain.com"
      
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
     
    #Get the Folder from URL
    $Folder = Get-PnPFolder -Url $FolderServerRelativeURL
         
    #Grant Permission to a Folder
    Set-PnPListItemPermission -List $ListName -Identity $Folder.ListItemAllFields -User $UserAccount -AddRole 'Restricted View'
    
    

    For more information, please refer to:

    https://www.sharepointdiary.com/2016/09/sharepoint-online-set-folder-permissions-powershell.html

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    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.