使用图形 API 在 SharePoint Online 列表中创建文件夹

问题描述 投票:0回答:1

我需要在文件夹内创建列表项(文件夹将具有唯一的权限,因此有此要求)。第二个要求是能够与外部用户共享父文件夹。

是否可以通过图形 API 在 SharePoint Online 列表的根目录中创建文件夹(并在外部共享)?我似乎找不到合适的方法来实现它。

我知道这应该可以使用 SharePoint REST API 实现,但我找不到从客户端应用程序使用 API 的方法(例如,我尝试使用 PowerShell 和 appregistration/Oauth 来获取访问令牌,但它不适用于 SPO ,但是它适用于图表)

azure powershell sharepoint-online
1个回答
0
投票

首先,注册一个 Entra ID 应用程序并授予

Sites.ReadWrite.All
应用程序类型权限,如下所示:

enter image description here

我在 SharePoint 网站中有一个文档库,其中包含以下文件,名为

sridemosite
:

enter image description here

要使用 Graph API 在上述 SharePoint Online 列表中创建新文件夹,您可以使用以下 PowerShell 脚本:

$siteId = "your-site-id"
$listId = "your-list-id"
$folderName = "DemoFolder"

# Obtain an access token
$tenantId = "your-tenant-id"
$clientId = "your-client-id"
$clientSecret = "your-client-secret"
$authUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$authBody = @{
    client_id     = $clientId
    client_secret = $clientSecret
    scope         = "https://graph.microsoft.com/.default"
    grant_type    = "client_credentials"
}
$response = Invoke-RestMethod -Method Post -Uri $authUrl -ContentType "application/x-www-form-urlencoded" -Body $authBody
$token = $response.access_token

# Create the folder
$uri = "https://graph.microsoft.com/v1.0/sites/$siteId/lists/$listId/drive/root/children"
$body = @{
    name = $folderName
    folder = @{}
    '@microsoft.graph.conflictBehavior' = "rename"
}
$jsonBody = $body | ConvertTo-Json
$response = Invoke-RestMethod -Method Post -Uri $uri -Headers @{Authorization = "Bearer $token"} -Body $jsonBody -ContentType "application/json"
$response

回复:

enter image description here

为了确认这一点,我在 SharePoint Online 列表中检查了相同的内容,其中文件夹已成功创建,如下所示:

enter image description here

要将此文件夹共享给外部用户,请使用运行此 API 的以下脚本:

$folderId = $response.id
$driveId = "driveId"
$externalUserEmail = "[email protected]"

# Share the folder
$shareUri = "https://graph.microsoft.com/v1.0/sites/$siteId/drives/$driveId/items/$folderId/invite"
$shareBody = @{
    recipients = @(
        @{
            email = $externalUserEmail
        }
    )
    message = "Here's the file you requested."
    requireSignIn = $true
    sendInvitation = $true
    roles = @("read")
    password = $password
    expirationDateTime = $expirationDateTime
}
$jsonShareBody = $shareBody | ConvertTo-Json
$result = Invoke-RestMethod -Method Post -Uri $shareUri -Headers @{Authorization = "Bearer $token"} -Body $jsonShareBody -ContentType "application/json"

$result

回复:

enter image description here

这将向具有文件夹访问权限的外部用户发送邮件,如下所示:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.