c# - 使用 Graph SDK 在 Sharepoint Drive 的特定目录中创建文件夹
问题描述
如何仅使用给定的 Url 以及是否有可能实现这一点,idk?
我想做的是:
根据字符串在驱动器中的特定位置创建文件夹..
此字符串由 3 部分组成(每个部分都代表文件夹简单!)例如 mystring = "Analyse_General_Theory" ,驱动器中的路径应类似于: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 实现,请参考以下步骤
为应用程序添加 API 权限。请添加应用程序权限:
Files.ReadWrite.All
和Sites.ReadWrite.All
。代码。我使用客户凭证流。
/* 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);
推荐阅读
- python - 第一次尝试 python,错误(“IndexError: index 8 is out of bounds for axis 0 with size 8”)和效率问题
- java - JavaFX 异常:内部图形尚未初始化
- python - Python - 有没有办法提出同一系列问题并存储每组答案,直到用户完成?
- python - 使用 python 和 VS Code 在“键盘”上导入错误
- python - Python usig pygame中的错误(AttributeError:'builtin_function_or_method'对象没有属性'right')
- python - 如何创建一个字符串作为使用来自字符串输入的特定单词的输出?
- lookup-tables - OpenModelica 中的查找表 (CombiTimeTable)
- javascript - 如何动态更改progressbarjs运行时
- html - 如何使用路由器路径作为根拥有 Express.js 服务器文件?
- python - 将字符串参数转换为 Pandas 对象