Sharepoint Files access restricting

Shandy T 1 Reputation point
2021-12-27T19:10:38.22+00:00

I have a site with multiple folders which specific Visitors access and drop their companies files in, in ALL of these areas is also a single Excel file which I do not want the visitors to access.
Each of the Visitor's folders have sub monthly folders 01-12 and each have an Excel .xlsm file which I want to block from the visitor's viewing, is this possible via a Powershell script (or any other way).
I can see in PnPListItems that there is a FileValues of "Restricted" , "AccessPolicy", can any of these be used to restrict access to all the .xlsm files within each of the folders?

Microsoft 365 and Office SharePoint For business Windows
{count} votes

1 answer

Sort by: Most helpful
  1. Echo Du_MSFT 17,316 Reputation points
    2021-12-28T09:33:29.95+00:00

    Hi @Shandy T ,

    Welcome to Q&A Forum!

    According to my research, there is no similar -FileValues parameter in PnPListItems in PowerShell commands.

    Get-PnPListItem

    Get-PnPListItem
    [-List] <ListPipeBind>
    [-FolderServerRelativeUrl <String>]
    [-Fields <String[]>]
    [-PageSize <Int32>]
    [-ScriptBlock <ScriptBlock>]
    [-Connection <PnPConnection>]
    [<CommonParameters>]

    Set-PnPListItem

    Set-PnPListItem
    [-List] <ListPipeBind>
    -Identity <ListItemPipeBind>
    [-ContentType <ContentTypePipeBind>]
    [-Values <Hashtable>]
    [-UpdateType <UpdateType>]
    [-Label <String>]
    [-ClearLabel]
    [-Connection <PnPConnection>]

    Thanks,
    Echo Du

    ===========================
    Updated Answer =========================

    Hi @Shandy T ,

    According to my understanding, we cannot restrict access to all .xlsx files within each of the folder through “AccessPolicy” and “Restricted”.

    161020-p.jpg

    SharePoint access control policies:

    Microsoft recommends you protect content in SharePoint sites with enterprise and specialized security content with device access controls. You do this by creating a policy that specifies the level of protection and the sites to apply the protection to.

    • Enterprise sites: Allow browser-only access. This prevents users from editing and downloading files.
    • Specialized security sites: Block access from unmanaged devices.

    You can refer to the below workaround. Please follow the steps:

    1.Navigation to the Documents library as a site admin

    2.Select one root folder and click "Manage access"

    161008-1.jpg

    3.On the Manage Access panel, click "Advanced" link

    161071-2.jpg

    4.On the Permissions Settings page, click "Stop Inheriting Permissions" button

    160988-3.jpg

    5.Grant Edit permission to the specific visitor

    161015-4.jpg

    6.Please run the below PowerShell script as an admin:

    $SiteURL = "https://domain.sharepoint.com/sites/sitename"  
    $ListName = "xxxx"  
    $FolderURL = "/xxxx/yyyy"   
    
    Connect-PnPOnline -Url $SiteURL –Credentials (Get-Credential)   
    
    #$Folders = Get-PnPFolder -List $ListName  
    $Folders = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType Folder  
    
    foreach($Folder in $Folders)  
    {  
        $FolderRelativeUrl = $FolderURL+"/"+$Folder.Name    
        #$FolderRelativeUrl  
        $FolderItems = Get-PnPFolderItem -FolderSiteRelativeUrl $FolderRelativeUrl -ItemType File  
    
        #Write-host "Total Number of Items in the Folder in the list:" $FolderItems.Count  
    
        if($FolderItems.Count -ne 0)  
        {  
            #Write-host "Folder Name:" $Folder.Name  
    
            foreach($Item in $FolderItems)  
            {  
                if($Item.Name -like "*.xlsx")  
                {  
                    Write-host "File Title:" $Item.Name  
                    $FileItem = get-pnpfile $FilePath -AsListItem   
                    Write-host "File ID:" $FileItem['ID']                 
                    $FilePath = $Item.ServerRelativeUrl  
                    Write-host "File URL:" $FilePath                 
                    Write-host -ForegroundColor Cyan "  ************************************************************  "  
                    Write-host " "  
                    Set-PnPListItemPermission -List $ListName -Identity $FileItem['ID'] -User '******@domain.onmicrosoft.com' -RemoveRole 'Edit'  
    
                }  
            }         
    }  
    }  
    

    161035-5.jpg

    7.Here is my test:

    • a.Excel files delete the specific visitor's permission

    161054-6.jpg

    161017-7.jpg

    • b.These Excel files cannot be seen when accessing the Documents library as the specific visitor

    161018-8.jpg

    Thanks,
    Echo Du

    ==================================

    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.