How Can I Share a Folder with external users using the Go GraphAPI SDK

Sidi_244 0 Reputation points
2024-11-20T16:47:14.3133333+00:00

I need to share folders on SharePoint using the Go SDK. How could I achieve this, as the users should also include externals / guests only referenced by Email addresses.

Thanks!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,377 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,904 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 42,766 Reputation points
    2024-11-21T08:36:24.94+00:00

    Hi @Sidi_244

    You can send a sharing invitation to external users, which will send an email containing a sharing link to the target users. After that, users will be able to click the sharing link to access the shared folder.

    // Code snippets are only available for the latest major version. Current major version is $v1.*
    // Dependencies
    import (
    	  "context"
    	  msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
    	  graphdrives "github.com/microsoftgraph/msgraph-sdk-go/drives"
    	  graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
    	  //other-imports
    )
    requestBody := graphdrives.NewInvitePostRequestBody()
    driveRecipient := graphmodels.NewDriveRecipient()
    email := "[email protected]"
    driveRecipient.SetEmail(&email) 
    recipients := []graphmodels.DriveRecipientable {
    	driveRecipient,
    }
    requestBody.SetRecipients(recipients)
    message := "Here's the file that we're collaborating on."
    requestBody.SetMessage(&message) 
    requireSignIn := true
    requestBody.SetRequireSignIn(&requireSignIn) 
    sendInvitation := true
    requestBody.SetSendInvitation(&sendInvitation) 
    roles := []string {
    	"read",
    }
    requestBody.SetRoles(roles)
    password := "password123"
    requestBody.SetPassword(&password) 
    expirationDateTime := "2025-07-15T14:00:00.000Z"
    requestBody.SetExpirationDateTime(&expirationDateTime) 
    // To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
    invite, err := graphClient.Drives().ByDriveId("drive-id").Items().ByDriveItemId("driveItem-id").Invite().PostAsInvitePostResponse(context.Background(), requestBody, nil)
    

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    0 comments No comments

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.