如果您想使用 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 实现,请参考以下步骤
注册 Azure AD 应用程序
创建客户端密码。
为应用程序添加 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);