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.
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();