命名空间:microsoft.graph
可以使用 createLink 操作通过共享链接共享 DriveItem。
如果调用应用程序指定的链接类型尚不存在,CreateLink 操作将创建新的共享链接。
如果应用程序指定的共享链接类型已存在,则返回现有的共享链接。
DriveItem 资源从其上级继承共享权限。
此 API 可用于以下国家级云部署。
全局服务 |
美国政府 L4 |
美国政府 L5 (DOD) |
由世纪互联运营的中国 |
✅ |
✅ |
✅ |
✅ |
权限
为此 API 选择标记为最低特权的权限。
只有在应用需要它时,才使用更高的特权权限。 有关委派权限和应用程序权限的详细信息,请参阅权限类型。 要了解有关这些权限的详细信息,请参阅 权限参考。
权限类型 |
最低特权权限 |
更高特权权限 |
委派(工作或学校帐户) |
Files.ReadWrite |
Files.ReadWrite.All、Sites.ReadWrite.All |
委派(个人 Microsoft 帐户) |
Files.ReadWrite |
Files.ReadWrite.All |
应用程序 |
Files.ReadWrite.All |
Sites.ReadWrite.All |
HTTP 请求
POST /drives/{driveId}/items/{itemId}/createLink
POST /groups/{groupId}/drive/items/{itemId}/createLink
POST /me/drive/items/{itemId}/createLink
POST /sites/{siteId}/drive/items/{itemId}/createLink
POST /users/{userId}/drive/items/{itemId}/createLink
名称 |
说明 |
Authorization |
持有者 {token}。 必填。 详细了解 身份验证和授权。 |
Content-Type |
application/json. 必需。 |
请求正文
请求正文定义应用程序正在请求的共享链接的属性。
请求应为具有以下属性的 JSON 对象。
名称 |
类型 |
说明 |
类型 |
string |
要创建的共享链接的类型。
view 、 edit 或 embed 。 |
password |
string |
创建者设置的共享链接的密码。 仅限可选和 OneDrive 个人版。 |
expirationDateTime |
string |
DateTime 格式为 yyyy-MM-ddTHH:mm:ssZ 的字符串表示权限的过期时间。 |
retainInheritedPermissions |
布尔值 |
可选。 如果 true (默认) ,则首次共享此项目时,将保留共享项上任何现有继承的权限。 如果 false 为 ,则首次共享时将删除所有现有权限。 |
scope |
string |
可选。 要创建的链接的范围。
anonymous 、 organization 或 users 。 |
链接类型
type 参数允许使用以下值:
类型值 |
说明 |
view |
创建到 DriveItem 的只读链接。 |
edit |
创建到 DriveItem 的读写链接。 |
embed |
创建到 DriveItem 的可嵌入链接。 此选项仅适用于 OneDrive 个人版中的文件。 |
范围类型
scope 参数允许使用以下值。
如果未指定 scope 参数,则为组织创建默认的链接类型。
值 |
说明 |
anonymous |
拥有该链接的任何人都可以访问,无需登录。 这可能包括组织外部的人员。 管理员可能会禁用匿名链接支持。 |
organization |
登录到组织(租户)的任何人都可以使用该链接获取访问权限。 仅适用于 OneDrive for Business 和 SharePoint。 |
users |
仅与组织内部或组织外部选择的人员共享。 |
响应
如果成功,此方法将在响应正文中返回单个 Permission 资源,此响应正文表示请求的共享权限。
如果已经为此项目创建新的共享链接,则响应为 201 Created
;如果返回现有链接,则为 200 OK
。
示例
下面的示例请求为在用户的 OneDrive 中按 {itemId} 指定的 DriveItem 创建共享链接。
共享链接配置为只读并且可由具有该链接的任何用户使用。
如果 retainInheritedPermissions
为 false,则首次共享时将删除所有现有权限。
请求
POST https://graph.microsoft.com/v1.0/me/drive/items/{item-id}/createLink
Content-type: application/json
{
"type": "view",
"password": "ThisIsMyPrivatePassword",
"scope": "anonymous",
"retainInheritedPermissions": false
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Drives.Item.Items.Item.CreateLink;
var requestBody = new CreateLinkPostRequestBody
{
Type = "view",
Password = "ThisIsMyPrivatePassword",
Scope = "anonymous",
RetainInheritedPermissions = false,
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].CreateLink.PostAsync(requestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
mgc drives items create-link post --drive-id {drive-id} --drive-item-id {driveItem-id} --body '{\
"type": "view",\
"password": "ThisIsMyPrivatePassword",\
"scope": "anonymous",\
"retainInheritedPermissions": false\
}\
'
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphdrives "github.com/microsoftgraph/msgraph-sdk-go/drives"
//other-imports
)
requestBody := graphdrives.NewCreateLinkPostRequestBody()
type := "view"
requestBody.SetType(&type)
password := "ThisIsMyPrivatePassword"
requestBody.SetPassword(&password)
scope := "anonymous"
requestBody.SetScope(&scope)
retainInheritedPermissions := false
requestBody.SetRetainInheritedPermissions(&retainInheritedPermissions)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
createLink, err := graphClient.Drives().ByDriveId("drive-id").Items().ByDriveItemId("driveItem-id").CreateLink().Post(context.Background(), requestBody, nil)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.drives.item.items.item.createlink.CreateLinkPostRequestBody createLinkPostRequestBody = new com.microsoft.graph.drives.item.items.item.createlink.CreateLinkPostRequestBody();
createLinkPostRequestBody.setType("view");
createLinkPostRequestBody.setPassword("ThisIsMyPrivatePassword");
createLinkPostRequestBody.setScope("anonymous");
createLinkPostRequestBody.setRetainInheritedPermissions(false);
var result = graphClient.drives().byDriveId("{drive-id}").items().byDriveItemId("{driveItem-id}").createLink().post(createLinkPostRequestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
const options = {
authProvider,
};
const client = Client.init(options);
const permission = {
type: 'view',
password: 'ThisIsMyPrivatePassword',
scope: 'anonymous',
retainInheritedPermissions: false
};
await client.api('/me/drive/items/{item-id}/createLink')
.post(permission);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Drives\Item\Items\Item\CreateLink\CreateLinkPostRequestBody;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new CreateLinkPostRequestBody();
$requestBody->setType('view');
$requestBody->setPassword('ThisIsMyPrivatePassword');
$requestBody->setScope('anonymous');
$requestBody->setRetainInheritedPermissions(false);
$result = $graphServiceClient->drives()->byDriveId('drive-id')->items()->byDriveItemId('driveItem-id')->createLink()->post($requestBody)->wait();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
Import-Module Microsoft.Graph.Files
$params = @{
type = "view"
password = "ThisIsMyPrivatePassword"
scope = "anonymous"
retainInheritedPermissions = $false
}
New-MgDriveItemLink -DriveId $driveId -DriveItemId $driveItemId -BodyParameter $params
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.drives.item.items.item.create_link.create_link_post_request_body import CreateLinkPostRequestBody
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = CreateLinkPostRequestBody(
type = "view",
password = "ThisIsMyPrivatePassword",
scope = "anonymous",
retain_inherited_permissions = False,
)
result = await graph_client.drives.by_drive_id('drive-id').items.by_drive_item_id('driveItem-id').create_link.post(request_body)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
响应
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": "123ABC",
"roles": ["write"],
"link": {
"type": "view",
"scope": "anonymous",
"webUrl": "https://1drv.ms/A6913278E564460AA616C71B28AD6EB6",
"application": {
"id": "1234",
"displayName": "Sample Application"
},
},
"hasPassword": true
}
创建公司可共享的链接
OneDrive for Business 和 SharePoint 都支持公司可共享的链接。
此类链接与匿名链接类似,只不过仅适用于拥有组织的成员。
若要创建公司可共享的链接,请使用值为 organization
的 scope 参数。
请求
POST https://graph.microsoft.com/v1.0/me/drive/items/{item-id}/createLink
Content-Type: application/json
{
"type": "edit",
"scope": "organization"
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Drives.Item.Items.Item.CreateLink;
var requestBody = new CreateLinkPostRequestBody
{
Type = "edit",
Scope = "organization",
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].CreateLink.PostAsync(requestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
mgc drives items create-link post --drive-id {drive-id} --drive-item-id {driveItem-id} --body '{\
"type": "edit",\
"scope": "organization"\
}\
'
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphdrives "github.com/microsoftgraph/msgraph-sdk-go/drives"
//other-imports
)
requestBody := graphdrives.NewCreateLinkPostRequestBody()
type := "edit"
requestBody.SetType(&type)
scope := "organization"
requestBody.SetScope(&scope)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
createLink, err := graphClient.Drives().ByDriveId("drive-id").Items().ByDriveItemId("driveItem-id").CreateLink().Post(context.Background(), requestBody, nil)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.drives.item.items.item.createlink.CreateLinkPostRequestBody createLinkPostRequestBody = new com.microsoft.graph.drives.item.items.item.createlink.CreateLinkPostRequestBody();
createLinkPostRequestBody.setType("edit");
createLinkPostRequestBody.setScope("organization");
var result = graphClient.drives().byDriveId("{drive-id}").items().byDriveItemId("{driveItem-id}").createLink().post(createLinkPostRequestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
const options = {
authProvider,
};
const client = Client.init(options);
const permission = {
type: 'edit',
scope: 'organization'
};
await client.api('/me/drive/items/{item-id}/createLink')
.post(permission);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Drives\Item\Items\Item\CreateLink\CreateLinkPostRequestBody;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new CreateLinkPostRequestBody();
$requestBody->setType('edit');
$requestBody->setScope('organization');
$result = $graphServiceClient->drives()->byDriveId('drive-id')->items()->byDriveItemId('driveItem-id')->createLink()->post($requestBody)->wait();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
Import-Module Microsoft.Graph.Files
$params = @{
type = "edit"
scope = "organization"
}
New-MgDriveItemLink -DriveId $driveId -DriveItemId $driveItemId -BodyParameter $params
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.drives.item.items.item.create_link.create_link_post_request_body import CreateLinkPostRequestBody
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = CreateLinkPostRequestBody(
type = "edit",
scope = "organization",
)
result = await graph_client.drives.by_drive_id('drive-id').items.by_drive_item_id('driveItem-id').create_link.post(request_body)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
响应
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": "123ABC",
"roles": ["write"],
"link": {
"type": "edit",
"scope": "organization",
"webUrl": "https://contoso-my.sharepoint.com/personal/ellen_contoso_com/...",
"application": {
"id": "1234",
"displayName": "Sample Application"
},
},
}
创建可嵌入的链接
使用 embed
链接类型时,可以在 <iframe>
HTML 元素中嵌入返回的 webUrl。
创建嵌入链接时,webHtml
属性包含 <iframe>
的 HTML 代码以托管内容。
注意:仅 OneDrive 个人版支持嵌入链接。
请求
POST https://graph.microsoft.com/v1.0/me/drive/items/{item-id}/createLink
Content-Type: application/json
{
"type": "embed"
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Drives.Item.Items.Item.CreateLink;
var requestBody = new CreateLinkPostRequestBody
{
Type = "embed",
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].CreateLink.PostAsync(requestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
mgc drives items create-link post --drive-id {drive-id} --drive-item-id {driveItem-id} --body '{\
"type": "embed"\
}\
'
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest major version. Current major version is $v1.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphdrives "github.com/microsoftgraph/msgraph-sdk-go/drives"
//other-imports
)
requestBody := graphdrives.NewCreateLinkPostRequestBody()
type := "embed"
requestBody.SetType(&type)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
createLink, err := graphClient.Drives().ByDriveId("drive-id").Items().ByDriveItemId("driveItem-id").CreateLink().Post(context.Background(), requestBody, nil)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
com.microsoft.graph.drives.item.items.item.createlink.CreateLinkPostRequestBody createLinkPostRequestBody = new com.microsoft.graph.drives.item.items.item.createlink.CreateLinkPostRequestBody();
createLinkPostRequestBody.setType("embed");
var result = graphClient.drives().byDriveId("{drive-id}").items().byDriveItemId("{driveItem-id}").createLink().post(createLinkPostRequestBody);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
const options = {
authProvider,
};
const client = Client.init(options);
const permission = {
type: 'embed'
};
await client.api('/me/drive/items/{item-id}/createLink')
.post(permission);
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
<?php
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Graph\Generated\Drives\Item\Items\Item\CreateLink\CreateLinkPostRequestBody;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new CreateLinkPostRequestBody();
$requestBody->setType('embed');
$result = $graphServiceClient->drives()->byDriveId('drive-id')->items()->byDriveItemId('driveItem-id')->createLink()->post($requestBody)->wait();
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
Import-Module Microsoft.Graph.Files
$params = @{
type = "embed"
}
New-MgDriveItemLink -DriveId $driveId -DriveItemId $driveItemId -BodyParameter $params
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph import GraphServiceClient
from msgraph.generated.drives.item.items.item.create_link.create_link_post_request_body import CreateLinkPostRequestBody
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = CreateLinkPostRequestBody(
type = "embed",
)
result = await graph_client.drives.by_drive_id('drive-id').items.by_drive_item_id('driveItem-id').create_link.post(request_body)
有关如何将 SDK 添加到项目并创建 authProvider 实例的详细信息,请参阅 SDK 文档。
响应
HTTP/1.1 201 Created
Content-Type: application/json
{
"id": "123ABC",
"roles": ["read"],
"link": {
"type": "embed",
"webHtml": "<IFRAME src=\"https://onedrive.live.com/...\"></IFRAME>",
"webUrl": "https://onedrive.live.com/...",
"application": {
"id": "1234",
"displayName": "Sample Application"
},
}
}
- 使用此操作创建的链接不会过期,除非对组织强制执行了默认过期策略。
- 链接在项的共享权限中可见,可以由该项的所有者删除。
- 除非项已被签出,否则链接始终指向该项的最新版本(仅限 SharePoint)。