首页 > 解决方案 > 使用 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

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

标签: c#azure-ad-graph-apimicrosoft-graph-files

解决方案


如果您想使用 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. 注册 Azure AD 应用程序

  2. 创建客户端密码

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

在此处输入图像描述


推荐阅读