I'm using this script below to upload a file or multiple files in a document library in sharepoint online. I would like to set only the 'read' permission so that only one specific user can read (see) the file.
How can I set the 'read' permission for a specific domain user first and then upload the file?
Why? -> I want to make a folder "paychecks 2019" and then upload a paycheck and set the permissions so that the workers can only see their personal paycheck.
I can also create a folder for each worker and set the read permission on folder level but I prefer to give them all access to "paychecks 2019" and give them permissions on file level during uploading.
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password) $Context.Credentials = $Creds $List = $Context.Web.Lists.GetByTitle($DocLibName) $Context.Load($List.RootFolder) $Context.ExecuteQuery() $TargetFolder = $Context.Web.GetFolderByServerRelativeUrl($List.RootFolder.ServerRelativeUrl + "/" + $FolderName); Foreach ($File in (dir $Folder -File)) { $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open) $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation $FileCreationInfo.Overwrite = $true $FileCreationInfo.ContentStream = $FileStream $FileCreationInfo.URL = $File $Upload = $TargetFolder.Files.Add($FileCreationInfo) $Context.Load($Upload) $Context.ExecuteQuery() }