Search code examples
sharepointmicrosoft-graph-apisharemicrosoft-graph-filessharefile

Microsoft Graph create share link for specific people


I would like to share document by link in sharepoint from microsoft graph code. Default behaviour is that every person who has link can see this file. I want to make this link working just for specific people.

So my code look like this:

Permission permission = await _graphClient.Sites[_options.SiteId]
                        .Drives[driveId]
                        .Items[itemId]
                        .CreateLink("view", "organization")
                        .Request()
                        .PostAsync();

This create share link for all people in organization. Now I would like to grant permissions (https://learn.microsoft.com/en-us/graph/api/permission-grant?view=graph-rest-1.0&tabs=csharp)

await graphClient.Shares["{sharedDriveItem-id}"].Permission
    .Grant(roles,recipients)
    .Request()
    .PostAsync();

But I have no idea what should be in place "{sharedDriveItem-id}". When I put there itemId it doesn't work. Also if I put there permission.link.webUrl it also doesn't work.

What am I doing wrong?


Solution

  • Okey, I found solution. There are few steps:

    1. As sharedDriveItem-id I used encoded webUrl following by this instruction https://learn.microsoft.com/en-us/graph/api/shares-get?view=graph-rest-1.0&tabs=http
    2. When I was creating link (https://learn.microsoft.com/en-us/graph/api/driveitem-createlink?view=graph-rest-1.0&tabs=http) in place scope i put "users"- there is no option like that in documentation but without that it doesn't work
    3. I added Prefer in header https://learn.microsoft.com/en-us/graph/api/driveitem-createlink?view=graph-rest-1.0&tabs=http
    4. I was using clientSecret/clientId authorization so I had to add azure app access to Sites.Manage.All and Sites.FullControl.All in Graph Api Permissions

    Everything works If you using Microsoftg.Graph nuget in newest version (4.3 right now if I remember correctly)