gpt4 book ai didi

c# - 使用 Graph SDK 在 Sharepoint Drive 的特定目录中创建文件夹

转载 作者:行者123 更新时间:2023-12-01 21:38:58 27 4
gpt4 key购买 nike

如何仅使用给定的 Url 实现这一点,是否可能,idk?

我想做什么:

根据字符串在驱动器中的特定位置创建文件夹..
这个字符串由 3 部分组成(每个部分代表文件夹简单!)例如, mystring = "Analyse_General_Theory",驱动器中的路径应该是这样的:分析/一般/理论

所以:

我对解决方案的想象就是这样:)

将我的 stringUrl 传递给构建请求然后发布我的文件夹

stringUrl = "https://CompanyDomin.sharepoint.com/sites/mySite/SharedFolders/Analyse/General/Theory"

然后

await graphClient.Request(stringUrl).PostAsync(myLastFolder) !!! 

结果就是这样!

分析/一般/理论/myLastFolder

有这样的东西吗?或者可能类似于这种方法?

最佳答案

如果您想使用 Graph API 在 SharePoint 中创建文件夹,请使用以下 Microsoft graph Rest API .因为 Azure AD 图形 API 只能用于管理 Azure AD 资源(如用户、组等),不能用于管理 SharePoint 资源。如果我们想用Graph API管理SharePoint资源,我们需要使用Microsoft Graph API

POST https://graph.microsoft.com/v1.0/sites/{site-id}/drive/items/{parent-item-id}/children

例如

POST https://graph.microsoft.com/v1.0/sites/CompanyDomin.sharepoint.com/drive/items/root:/
{folder path}:/children

{
"name": "<the new folder name>",
"folder": { },
"@microsoft.graph.conflictBehavior": "rename"
}

关于如何用SDK实现,请引用下面的步骤

  1. Register Azure AD application

  2. Create a client secret .

  3. 为应用程序添加 API 权限。请添加应用程序权限:Files.ReadWrite.AllSites.ReadWrite.All

  4. 代码。我使用客户端凭证流。

/* please run the following command install sdk Microsoft.Graph and Microsoft.Graph.Auth 

Install-Package Microsoft.Graph
Install-Package Microsoft.Graph.Auth -IncludePrerelease

*/

string clientId = "<your AD app client id>";
string clientSecret = "<your AD app client secret>";
string tenantId = "<your AD tenant domain>";
IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
.Create(clientId)
.WithTenantId(tenantId)
.WithClientSecret(clientSecret)
.Build();

ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var item = new DriveItem
{

Name = "myLastFolder",
Folder= new Folder { },
AdditionalData = new Dictionary<string, object>()
{
{"@microsoft.graph.conflictBehavior","rename"}
}
};
var r = await graphClient.Sites["<CompanyDomin>.sharepoint.com"].Drive.Items["root:/Analyse/General/Theory:"].Children.Request().AddAsync(item);
Console.WriteLine("the folder name : " + r.Name);

enter image description here

关于c# - 使用 Graph SDK 在 Sharepoint Drive 的特定目录中创建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61591023/

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