gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:56:22 31 4
gpt4 key购买 nike

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

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

代码如下:

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

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

最佳答案

如果是子文件夹,创建文件夹的端点应该是

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

关于c# - Microsoft Graph API 客户端库 - 创建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56266087/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com