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

时间:2019-05-22 23:08:50

标签: c# microsoft-graph microsoft-graph-sdks

我正在尝试使用Microsoft Graph(C#SDK)在SharePoint中的现有文件夹下创建一个文件夹。我知道使用Graph API时在SharePoint或OneDrive上创建文件夹应该是相同的,但是仍然找不到任何好的在线参考。我发现的唯一文章是an old one,其中只有一个JavaScript示例。

我有一个根文件夹A,我想在B下创建一个子文件夹A

代码如下:

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;

最后一行代码抛出AggregateException(由于TPL),其中包含内部异常:

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

我想知道创建子文件夹的正确语法。

1 个答案:

答案 0 :(得分:2)

对于子文件夹,用于创建文件夹的端点应为

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"
}

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