Home > OS >  How to use CreateLink() MS Graph C# SDK for Scope type User
How to use CreateLink() MS Graph C# SDK for Scope type User

Time:12-04

I cannot find a MS Graph SDK c# example for how to use the "user" scope parameter. The source docs gives no examples how to use this "user" parameter. Do you put the email addresses in string format or some other format?

https://learn.microsoft.com/en-us/graph/api/driveitem-createlink?view=graph-rest-1.0&tabs=http

        public async Task<Permission> GetFilePreviewLinkAsync(string DriveID, string DriveItemID, string Scope, string Type)
    {

        Permission response = null;

        try
        {
            response = await _graphServiceClient.Me.Drives[DriveID].Items[DriveItemID]
                .CreateLink(Type, Scope, null, null, null, true)
                .Request()
                .PostAsync();
        }
        catch (ServiceException ex)
        {
            Console.WriteLine($"Error uploading: {ex.ToString()}");
        }

        return response;

    }

CodePudding user response:

The first step is to create a link with user scope. The second step is to grant users access to a link.

The first request returns an Permission object with ShareId property

var permission = await graphClient.Me.Drive.Items["{driveItem-id}"]
          .CreateLink(type,scope,null,null,null,null)
          .Request()
          .PostAsync();

Use ShareId in the second call to grant users access. Users are specified in DriveRecipient collection either by email or by id.

var recipients = new List<DriveRecipient>()
{
    new DriveRecipient
    {
        Email = "[email protected]"
        // ObjectId = <User.Id>
    },
    new DriveRecipient
    {
        Email = "[email protected]"
    }
};

var roles = new List<String>()
{
    "read"
};

await graphClient.Shares[permission.ShareId].Permission
    .Grant(roles,recipients)
    .Request()
    .PostAsync();

Documentation: Grant access to sharing link

  • Related