Last active
April 16, 2024 20:14
-
-
Save joerodgers/32d5c2e48f16fccbe8c9b8588ed6009a to your computer and use it in GitHub Desktop.
Example of using the SP.Web.ShareObject REST endpoint to share a file or folder with a user email address.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requries -modules "PnP.PowerShell" | |
function Get-PeoplePickerValueForEmail | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[Parameter(Mandatory=$true)] | |
[string] | |
) | |
begin | |
{ | |
$web = Get-PnPWeb | |
} | |
process | |
{ | |
return [Microsoft.SharePoint.Client.ExternalSharingExtensions]::ResolvePeoplePickerValueForEmail( $web, $Email ) | |
} | |
end | |
{ | |
} | |
} | |
function Grant-UserPermission | |
{ | |
[CmdletBinding()] | |
param | |
( | |
# the full url of file or folder to grant access to | |
[Parameter(Mandatory=$true)] | |
[System.Uri] | |
$ObjectUri, | |
# email address of the user to grant permisions to | |
[Parameter(Mandatory=$true)] | |
[string] | |
$EmailAddress, | |
# the level of access given to the user | |
[Parameter(Mandatory=$false)] | |
[ValidateSet("View", "Edit")] | |
[string] | |
$ExternalSharingOption, | |
# determines if permissions should be pushed to items with unique permissions | |
[Parameter(Mandatory=$false)] | |
[switch] | |
$PropagateAcl | |
) | |
begin | |
{ | |
$roleValue = "" | |
switch( $ExternalSharingOption ) | |
{ | |
"Edit" | |
{ | |
$roleValue = "role:1073741827" # Edit | |
} | |
default | |
{ | |
$roleValue = "$ole:1073741826" # View | |
} | |
} | |
} | |
process | |
{ | |
$peoplePickerInput = Get-PeoplePickerValueForEmail -Email $EmailAddress | |
Write-Verbose "PeoplePickerInput: $peoplePickerInput" | |
$content = [PSCustomObject] @{ | |
url = $ObjectUri.ToString() | |
peoplePickerInput = $peoplePickerInput | |
roleValue = $roleValue | |
groupId = 0 # the id of the group to be added to. Zero if not adding to a permissions group. | |
propagateAcl = $PropagateAcl.IsPresent | |
sendEmail = $false | |
includeAnonymousLinkInEmail = $false | |
emailSubject = "" | |
emailBody = "" | |
useSimplifiedRoles = $true | |
} | ConvertTo-Json | |
Write-Verbose "Share Request: $content" | |
Invoke-PnPSPRestMethod ` | |
-Method POST ` | |
-Url "/_api/SP.Web.ShareObject" ` | |
-Content $content.ToString() ` | |
-ContentType "application/json" ` | |
} | |
end | |
{ | |
} | |
} | |
Connect-PnPOnline ` | |
-Url "https://contoso.sharepoint.com/sites/teamsite1" ` | |
-ClientId $env:O365_CLIENTID ` | |
-Thumbprint $env:O365_THUMBPRINT ` | |
-Tenant $env:O365_TENANTID | |
$grant = Grant-UserPermission ` | |
-ObjectUri "https://contoso.sharepoint.com/sites/teamsite1/Shared Documents/Folder2" ` | |
-EmailAddress "adamb@contoso.com" ` | |
-ExternalSharingOption Edit ` | |
-PropagateAcl |
Where can I find this part?
[Microsoft.SharePoint.Client.ExternalSharingExtensions]::ResolvePeoplePickerValueForEmail( $web, $Email )
You can find it in the PnP.Framework assembly
PS C:\Windows\system32> [Microsoft.SharePoint.Client.ExternalSharingExtensions].Assembly.FullName
PnP.Framework, Version=1.11.2.0, Culture=neutral, PublicKeyToken=0d501f89f11b748c
@joerodgers Thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where can I find this part?
[Microsoft.SharePoint.Client.ExternalSharingExtensions]::ResolvePeoplePickerValueForEmail( $web, $Email )