简体   繁体   English

Microsoft Graph API客户端库-创建文件夹

[英]Microsoft Graph API client library - create a folder

I'm trying to create a folder under an existing folder in SharePoint using Microsoft Graph (C# SDK). 我正在尝试使用Microsoft Graph(C#SDK)在SharePoint中的现有文件夹下创建一个文件夹。 I understand creating the folder on SharePoint or OneDrive should be the same when using the Graph API, but still, I couldn't find any good online references. 我知道使用Graph API时在SharePoint或OneDrive上创建文件夹应该是相同的,但是仍然找不到任何好的在线参考。 The only article I found is an old one , which only has an example in JavaScript. 我发现的唯一一篇文章是一篇旧文章,其中只有一个JavaScript示例。

I have a root folder A and I want to create a subfolder B under A . 我有一个根文件夹A ,我想在A下创建一个子文件夹B

Here is the code: 这是代码:

var driveRequestBuilder = graphClient
    .Sites[SharePointSiteId]
    .Lists[ListId]
    .Drive;

var folderRequestBuilder = driveRequestBuilder
    .Root
    .ItemWithPath("A");
var folderDriveItem = folderRequestBuilder
    .Request()
    .GetAsync()
    .Result; // This returns the root folder "A"'s info

var subFolderDriveItem = new DriveItem()
{
    Name = "B",
    Folder = folderDriveItem.Folder
};
var result = folderRequestBuilder
    .Request()
    .CreateAsync(subFolderDriveItem)
    .Result;

The last line of code throws an AggregateException (because of TPL), which contains the inner exception: 代码的最后一行抛出一个AggregateException (由于TPL),其中包含内部异常:

Code: -1, Microsoft.SharePoint.Client.InvalidClientQueryException
Message: The parameter folder does not exist in method getByPath.
Inner error

I want to know the correct syntax to create the subfolder. 我想知道创建子文件夹的正确语法。

In case of sub folder the endpoint for creating a folder should be 如果是子文件夹,则用于创建文件夹的端点应为

POST https://graph.microsoft.com/v1.0/sites/{site-id}/lists/{list-id}/drive/root:/{path}:/children
Content-Type: application/json

{
  "name": "New folder name",
  "folder": { },
  "@microsoft.graph.conflictBehavior": "rename"
}

Example for msgraph-sdk-dotnet : msgraph-sdk-dotnet示例:

var folder = new DriveItem
{
    Name = "<sub folder name>",
    Folder = new Folder()
};

var result = await graphClient
    .Sites[siteId]
    .Lists[listId]
    .Drive
    .Root
    .ItemWithPath("<folder path>")
    .Children
    .Request()
    .AddAsync(folder);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM