
Microsoft 365 OneDrive for Business: Create Sharing Link
Microsoft 365 OneDrive for Business: 共有リンク作成
This item creates a URL to access the specified file/folder in OneDrive so that anyone who knows the link can view it.
Basic Configs
- Step Name
- Note
Configs for this Auto Step
- conf_OAuth2
- C1: OAuth2 Setting *
- conf_DriveItemUrl
- C2: File / Folder URL to share *
- conf_SharingLink
- C3: Data item to save the Sharing Link *
- conf_SharingLinkId
- C4: Data item to save the Sharing Link ID
Notes
- This Auto Step is for OneDrive for Business of Microsoft 365
- It does not work for OneDrive personal
- To get the URL of a file/folder on OneDrive, open the Details window of the file/folder by clicking the Information icon, proceed to “More details”, and click the icon next to “Path”

- Sharing Link cannot be created when external sharing is not allowed either in the organization settings or in the SharePoint site settings
- Refer to the Microsoft 365 Documentation to know how to check the sharing settings
- When sharing a file/folder in a Shared Library, remember to check the site-level sharing settings as well in the SharePoint site admin page
Capture

See Also
- Intermediate Error Catch Event (Boundary Type)
- Microsoft 365 OneDrive for Business: Delete Sharing Link
Script (click to open)
- An XML file that contains the code below is available to download
- onedrive-file-link-create.xml (C) Questetra, Inc. (MIT License)
- If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step
// OAuth2 config sample at [OAuth 2.0 Setting]
// - Authorization Endpoint URL: https://login.microsoftonline.com/{your-tenant-id}/oauth2/v2.0/authorize
// - Token Endpoint URL: https://login.microsoftonline.com/{your-tenant-id}/oauth2/v2.0/token
// - Scope: https://graph.microsoft.com/Files.ReadWrite.All offline_access
// - Consumer Key: (Get by Microsoft Azure Active Directory)
// - Consumer Secret: (Get by Microsoft Azure Active Directory)
const GRAPH_URI = 'https://graph.microsoft.com/v1.0/';
function main() {
//// == 工程コンフィグの参照 / Config Retrieving ==
const oauth2 = configs.getObject('conf_OAuth2');
const driveItemUrl = retrieveDriveItemUrl();
const sharingLinkDef = configs.getObject('conf_SharingLink');
const sharingLinkIdDef = configs.getObject('conf_SharingLinkId');
//// == 演算 / Calculating ==
const driveItemInfo = getDriveItemInfoByUrl(oauth2, driveItemUrl);
const {sharingLink, id} = createLink(oauth2, driveItemInfo);
//// == ワークフローデータへの代入 / Data Updating ==
engine.setData(sharingLinkDef, sharingLink);
if (sharingLinkIdDef !== null) {
engine.setData(sharingLinkIdDef, id);
}
}
/**
* config からファイル / フォルダ URL の値を読み出す
* URL が設定されていない場合はエラー
* @returns {String} fileUrl ファイル URL
*/
function retrieveDriveItemUrl() {
const driveItemUrlDef = configs.getObject('conf_DriveItemUrl');
let driveItemUrl = '';
if (driveItemUrlDef === null) {
driveItemUrl = configs.get('conf_DriveItemUrl');
} else {
driveItemUrl = engine.findData(driveItemUrlDef);
}
if (driveItemUrl === null || driveItemUrl === '') {
throw 'File / Folder URL is blank.';
}
return driveItemUrl;
}
/**
* OneDrive のファイル / フォルダのメタデータを取得し、JSONオブジェクトを返す
* APIの仕様: https://docs.microsoft.com/ja-jp/onedrive/developer/rest-api/api/shares_get?view=odsp-graph-online
* @param {AuthSettingWrapper} oauth2 OAuth2 設定情報
* @param {String} sharingUrl ファイル / フォルダの共有 URL
* @returns {Object} ファイル / フォルダ情報 {driveId, id}
*/
function getDriveItemInfoByUrl(oauth2, sharingUrl) {
// encoding sharing URL
const encodedSharingUrl = encodeSharingUrl(sharingUrl);
// preparing for API Request
const response = httpClient.begin()
.authSetting(oauth2)
.queryParam('select', 'id,parentReference')
.get(`${GRAPH_URI}shares/${encodedSharingUrl}/driveItem`);
const status = response.getStatusCode();
const responseStr = response.getResponseAsString();
if (status !== 200) {
engine.log(responseStr);
throw `Failed to get drive item. status: ${status}`;
}
const {
id,
parentReference: {
driveId
}
} = JSON.parse(responseStr);
return {driveId, id};
}
/**
* 共有URLを unpadded base64url 形式にエンコードする
* @param {String} sharingUrl 共有URL
* @returns {String} encodedSharingUrl エンコードされた共有URL
*/
function encodeSharingUrl(sharingUrl) {
let encodedSharingUrl = base64.encodeToUrlSafeString(sharingUrl);
while (encodedSharingUrl.slice(-1) === '=') {
encodedSharingUrl = encodedSharingUrl.slice(0, -1);
}
return `u!${encodedSharingUrl}`;
}
/**
* OneDrive のファイル / フォルダに共有リンクを作成する
* @param {AuthSettingWrapper} oauth2 OAuth2 設定情報
* @param {Object} driveItemInfo ファイル / フォルダ情報 {driveId, id}
* @param {String} driveItemInfo.driveId ドライブ ID
* @param {String} driveItemInfo.id ファイル / フォルダ ID
* @returns {Object} result
* @returns {String} result.id 共有リンクの ID
* @returns {String} result.sharingLink 共有リンク
*/
function createLink(oauth2, {driveId, id}) {
const requestBodyObj = {
type: 'view',
scope: 'anonymous'
};
const response = httpClient.begin()
.authSetting(oauth2)
.body(JSON.stringify(requestBodyObj), 'application/json')
.post(`${GRAPH_URI}drives/${driveId}/items/${id}/createLink`);
const status = response.getStatusCode();
const responseStr = response.getResponseAsString();
// 200: OK (returns the existing link), 201: Created
if (status !== 200 && status !== 201) {
engine.log(responseStr);
throw `Failed to create sharing link. status: ${status}`;
}
const responseJson = JSON.parse(responseStr);
// id と url を返す
return {
id: responseJson.id,
sharingLink: responseJson.link.webUrl
};
}