2

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;

    }

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Hey! So from the Onedrive UI we actually shared access to the parent folder (/WebFiles) holding our files for this app with certain users. Is there a way for CreateLink() for all the files in the sub folders to inherit what the parent folders permissions so I do not have to do this sharing method each time i want to get the preview link for this document? From the UI there is an option for in "Copy Link" which I assume is the same as CreateLink() and the option for who you would like this link to work with is "People with existing access". Is that an option in the API?
There is also a fifth parameter called retainedinheritedpermissions. Would setting that to true with users scope do what I’m expecting?
I think that default value for retainedinheritedpermissions is true and if true, any existing inherited permissions are retained on the shared item when sharing this item for the first time. If false, all existing permissions are removed when sharing for the first time.
Oh okay. So if I have shared the parent folder initially with the users already then any links created from CreateLink() with user scope should by default retain to the users already given permission from the parent right? I am just wondering if step 2 in your response is even needed then for the children files in the parent folder which has already been shared. Thanks!
If this is a group being assigned how would that look? Turns out that is easier to manage then a individual user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.