API to generate URL for folder from sharepoint online to external users

Ravindra Shukla 121 Reputation points
2021-08-20T13:39:18.5+00:00

Hello,

I am working on sharepoint to upload a folder there on my sharepoint online through powershell script and after uploading the folder, I need to generate a URL for that uploaded folder and share that URL with external users to access it.

I am aware about the api for URL generation of file stored in sharepoint online, however I am not aware about the folder.

Could you please let me know the api for URL generation for a folder from sharepoint online which I can use in my powershell script?

Thanks

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2021-08-23T05:47:41.767+00:00

    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.

    0 comments No comments

  2. Ravindra Shukla 121 Reputation points
    2021-08-23T10:34:19.907+00:00

    Hi @RaytheonXie_MSFT

    Thank you for your reply.

    I have two queries about this.

    1) If I try the method "DocumentSharingManager.UpdateDocumentSharingInfo" as you suggested for sharing a folder in Sharepoint to external users, will it send an email to the users which are assigned to "$Users" variable?. Just wanted to confirm, because I tried the same and in my case, email is not getting sent to the mentioned external user.

    2) Is this method useful to share the sharepoint online folder with external user without having Microsoft account or the user should require the MS account to access it? I want to do it using access code instead of having MS account for external user.

    Kindly confirm.

    Thanks


  3. Ravindra Shukla 121 Reputation points
    2021-08-24T07:34:05.923+00:00

    @RaytheonXie_MSFT
    Thanks for your reply.

    The permission for external sharing to all won't be allowed by my organization. Is there any other alternative to achieve this?

    Also I am looking to obtain a link for a folder from sharepoint online so that I can get it in a variable and use the same to send it to the users.

    Please suggest.

    Thanks


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.