In SharePoint Online, sharing a site or individual file from a document library is an important requirement for team collaboration. You can easily share resources with an internal and external user using SharePoint GUI. In this post, we will explore how to share a file document using Client-Side Object Model (CSOM) based PowerShell script.
If you planned to share with an external user, then please make sure your SharePoint tenant is configured to allow external users. Go to the SharePoint Admin center site, on left-side navigation, select Sharing in the Policies section. Under External Sharing, ensure the SharePoint sharing permission level is set as Anyone (Most permissive).
Share a File with Internal or External User using PowerShell
We can use the Web.ShareObject method from CSOM client to share a document or site. The below script sends an invite to the given user and provides view access to the given file. Run the below script after replacing the following parameters.
- $SiteURL – Provide URL of the site where the file exists.
- $ItemUrl – Full URL of the document to be shared.You can get the full path of the document from the Properties window (Details pane) in Document Library UI.(Ex: https://contoso.sharepoint.com/sites/site_name/shared%20documents/TestFile.docx).
- $User – Provide login name (email) of the user to share. If you share with an external user, then the user should have been already added as a guest user into your SharePoint tenant site.
#Refrence - https://docs.microsoft.com/en-us/archive/blogs/vesku/external-sharing-api-for-sharepoint-and-onedrive-for-business
#Load required SharePoint client assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
#Specify SharePoint Online Site URL
$SiteURL = "https://contoso.sharepoint.com/sites/site_name"
#Set full path (URL) of the document to be shared.
$ItemUrl = "https://contoso.sharepoint.com/sites/site_name/document_library/testfile.docx"
#Get user credentials to connect
$Cred = Get-Credential
#User to share the resource
$User = "[email protected]"
# peoplePickerInput object as json string
$PeoplePickerInput = "[{'Key' : '$($User)', 'Description' : '$($User)', 'DisplayText' : '$($User)', 'EntityType' : 'User', 'ProviderDisplayName' : 'Tenant', 'ProviderName' : 'Tenant', 'IsResolved' : false, 'EntityData' : {'Title' : '', 'MobilePhone' : '', 'Department' : '', 'Email' : '$($User)'}, 'MultipleMatches' : []}]"
#Set permission/role for the shared user. View = 1073741826, Edit = 1073741827
$RoleValue = "role:1073741826"
$SendEmail = $true
$IncludedAnonymousLinkInEmail = $false
$GroupId = 0
$UseSimplifiedRoles = $false
#A flag to determine if permissions should be pushed to items with unique permissions.
$PropageAcl =$false
$EmailSubject = "Site Shared"
$EmailBody = "Please accept this invitation to access the TestDocument file"
#Setup SharePoint site context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Share document with external users
$Result = [Microsoft.SharePoint.Client.Web]::ShareObject($Ctx,$ItemUrl,$PeoplePickerInput,$RoleValue,$Groupid,$PropageAcl,$SendEmail,$IncludedAnonymousLinkInEmail,$EmailSubject,$EmailBody,$UseSimplifiedRoles)
$Ctx.Load($Result)
$Ctx.ExecuteQuery()
Write-Host "Status code :$($Result.StatusCode)"
if($Result.ErrorMessage)
{
Write-Host "ErrorMessage :$($Result.ErrorMessage)" f Red
}