Microsoft Graph .NET SDK returns Invalid Request (400) when trying to CreateLink

Jakub Pernica 220 Reputation points
2023-05-23T13:05:20.9066667+00:00

Hello,

I'm using latest version of Microsoft Graph .NET SDK (5.11.0). What I'm trying to accomplish is to Create shareable link for an item that's stored in SharePoint site drive. I've been trying to follow the Graph API documentation here

public async Task<Result<string>> CreateShare(string requestUri)
{
    var client = GetOrCreateSharepointClient();
   
    var requestBody = new Microsoft.Graph.Drives.Item.Items.Item.CreateLink.CreateLinkPostRequestBody
    {
        Type = "edit",
        Scope = "anonymous"
    };

 	var item = client.Drives[myDriveId].Items["root:/" + pathToFile];
    var permission = await item.CreateLink.PostAsync(requestBody);

    return Result.Success<string>(/* return link here */);
}

However the line item.CreateLink.PostAsync(requestBody) throws an ODataError exception with the error message Invalid Request, status code 400. I've tried to use different properties and values in the requestBody as well.

There shouldn't be any problem with permissions, I've enabled that files can be shared with anyone and the application has permissions to Files.ReadWrite.All, Sites.ReadWrite.All

Appreciate any help in advance.

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

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-05-24T02:17:20.9933333+00:00

    Hi @Jakub Pernica

    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [Microsoft Graph .NET SDK returns Invalid Request (400) when trying to CreateLink]

    Issue Symptom:

    Create shareable link return 400 Invalid Request.

    Solution:

    Fixed the issue with following code

    var item = client.Drives[myDriveId].Items["root"].ItemWithPath(pathToFile);
    

    When using .GetAsync() .DeleteAsync(), the former syntax worked without an issue.


    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Jakub Pernica 220 Reputation points
    2023-05-23T13:50:54.2533333+00:00

    I've managed to fix this issue. The problem is with this line

    var item = client.Drives[myDriveId].Items["root:/" + pathToFile];
    

    Should look like this:

    var item = client.Drives[myDriveId].Items["root"].ItemWithPath(pathToFile);
    

    When I was using .GetAsync() .DeleteAsync(), the former syntax worked for me without an issue.

    1 person found this answer helpful.
    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.