Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a SharePoint Document Library with Microsoft Graph

I'm trying to create a new document library in a SharePoint Teams site using Microsoft Graph.

var docLibrary = $@"{{ ""name"": ""{listName}"", ""list"": {{ ""template"": ""documentLibrary"" }} }}";
var res = await GraphClient.QueryGraphAsyncPost($"/groups/{groupId}/sites/root/lists/", docLibrary, user);
var result = await res.Content.ReadAsStringAsync();

This is the code that I'm using, but it is returning a bad request. I can't seem to find in the documentation the correct way to create a Document Library.

like image 277
Paul Cavacas Avatar asked Sep 14 '26 22:09

Paul Cavacas


1 Answers

You'll want to specify the displayName instead of the name. The value of name is generated by the server, and so the 400 is an error saying it cannot be written to. You should see an error response that contains the following message:

Cannot define a 'name' for a list as it is assigned by the server. Instead, provide 'displayName'

If you change your request to the following it should work:

var docLibrary = $@"{{ ""displayName"": ""{listName}"", ""list"": {{ ""template"": ""documentLibrary"" }} }}";
var res = await GraphClient.QueryGraphAsyncPost($"/groups/{groupId}/sites/root/lists", docLibrary, user);
var result = await res.Content.ReadAsStringAsync();
like image 71
Brad Avatar answered Sep 19 '26 01:09

Brad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!