Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Last active April 16, 2024 20:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joerodgers/32d5c2e48f16fccbe8c9b8588ed6009a to your computer and use it in GitHub Desktop.
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.
#requries -modules "PnP.PowerShell"
function Get-PeoplePickerValueForEmail
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[string]
$Email
)
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
@suchodol
Copy link

Where can I find this part?
[Microsoft.SharePoint.Client.ExternalSharingExtensions]::ResolvePeoplePickerValueForEmail( $web, $Email )

@joerodgers
Copy link
Author

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

@suchodol
Copy link

@joerodgers Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment