Hi @Ravindra Shukla ,
We can use DocumentSharingManager.UpdateDocumentSharingInfo method to share folder with external users.
Here is the code for sample:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$Users = "******@gmail.com","******@gmail.com"
$absoluteFileUrl = "https://crescent.sharepoint.com/sites/marketing/DocumentLib/Test/Test Folder"
#Get Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Create Request List
$UserList = New-Object "System.Collections.Generic.List``1[Microsoft.SharePoint.Client.Sharing.UserRoleAssignment]"
#Set Role for each user
ForEach($User in $Users)
{
$UserRoleAssignment = New-Object Microsoft.SharePoint.Client.Sharing.UserRoleAssignment
$UserRoleAssignment.UserId = $User
$UserRoleAssignment.Role = [Microsoft.SharePoint.Client.Sharing.Role]::View #Other Options: Edit, or Owner
$UserList.Add($UserRoleAssignment)
}
#Send invites
$EmailMessage = "Please accept this invitation to our Marketing Site. Thanks!"
[Microsoft.SharePoint.Client.Sharing.DocumentSharingManager]::UpdateDocumentSharingInfo($Ctx, $absoluteFileUrl, $UserList, true, true, true, $EmailMessage, true, true)
$Ctx.ExecuteQuery()
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
If an Answer is helpful, please click "Accept Answer" and upvote it.
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.