
Microsoft Lists: リストアイテム追加
Microsoft Lists: Add List Item
この工程は、Microsoft Lists の指定リストに、新しいリストアイテムを 1 件追加します。
Basic Configs
- 工程名
- メモ
Configs for this Auto Step
- conf_OAuth2
- C1: OAuth2 設定 *
- conf_SiteUrl
- C2: SharePoint サイトの URL *
- conf_ListTitle
- C3: リストの名前 *
- conf_ListItemId
- C4: リストアイテム ID を保存するデータ項目
- conf_ColumnName1
- C-K1: 列 1 の Lists フィールド名
- conf_ColumnValue1
- C-V1: 列 1 の値#{EL}
- conf_ColumnName2
- C-K2: 列 2 の Lists フィールド名
- conf_ColumnValue2
- C-V2: 列 2 の値#{EL}
- conf_ColumnName3
- C-K3: 列 3 の Lists フィールド名
- conf_ColumnValue3
- C-V3: 列 3 の値#{EL}
- conf_ColumnName4
- C-K4: 列 4 の Lists フィールド名
- conf_ColumnValue4
- C-V4: 列 4 の値#{EL}
- conf_ColumnName5
- C-K5: 列 5 の Lists フィールド名
- conf_ColumnValue5
- C-V5: 列 5 の値#{EL}
- conf_ColumnName6
- C-K6: 列 6 の Lists フィールド名
- conf_ColumnValue6
- C-V6: 列 6 の値#{EL}
- conf_ColumnName7
- C-K7: 列 7 の Lists フィールド名
- conf_ColumnValue7
- C-V7: 列 7 の値#{EL}
Notes
- SharePoint サイトの URL は、リスト URL の
/Listsより前の部分です- マイリスト内のリストの場合、
https://{サブドメイン}-my.sharepoint.com/personal/{ユーザ識別子}_{サブドメイン}_onmicrosoft_comのような形式です - SharePoint サイト内のリストの場合、
https://{サブドメイン}.sharepoint.com/sites/{サイト識別子}のような形式です
- マイリスト内のリストの場合、
- フィールド名を取得する方法はいくつかあります
- リストのスキーマ付き CSV をエクスポートすると、各列のスキーマ内の
Nameプロパティの値がフィールド名です - WebUI でリストを表示し、列の値で並び替えを行うと、URL に
sortField={フィールド名}の形式でフィールド名が含まれます
- リストのスキーマ付き CSV をエクスポートすると、各列のスキーマ内の
- サポートしている Microsoft Lists の列タイプは、下表の通りです:
| サポートする Microsoft Lists の列タイプ | 値の例 |
|---|---|
| 1 行テキスト | 単一行の文字列 |
| 複数行テキスト | 複数行の 文字列 |
| 数、通貨 | 123 |
| 選択肢(複数選択を許可していない場合) | 選択肢 1 |
| 日付と時刻 ※ Microsoft Lists で表示される日付と時刻は、SharePoint サイトのタイムゾーン設定(Microsoft 365 アカウントのタイムゾーン設定とは異なります)に応じたものです | 2024-03-26 02:15 |
Capture

See Also
Script (click to open)
- 次のスクリプトが記述されている XML ファイルをダウンロードできます
- microsoft-lists-listitem-add.xml (C) Questetra, Inc. (MIT License)
- Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます
// 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/Sites.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/';
const COLUMN_NUM = 7; // 扱える列の数
const main = () => {
//// == 工程コンフィグの参照 / Config Retrieving ==
const oauth2 = configs.getObject('conf_OAuth2');
const siteUrl = retrieveSiteUrl();
const listTitle = configs.get('conf_ListTitle');
const listItemIdDef = configs.getObject('conf_ListItemId');
const listItemObj = retrieveListItemContent();
//// == 演算 / Calculating ==
const siteId = getSiteIdByUrl(oauth2, siteUrl);
const listItemId = addListItem(oauth2, siteId, listTitle, listItemObj);
//// == ワークフローデータへの代入 / Data Updating ==
saveData(listItemIdDef, listItemId);
};
/**
* 工程コンフィグからサイトの URL を取得する
* @returns {String} サイトの URL
*/
const retrieveSiteUrl = () => {
let siteUrl = configs.get('conf_SiteUrl');
// 末尾にスラッシュがある場合、削除
if (siteUrl.endsWith('/')) {
siteUrl = siteUrl.slice(0, -1);
}
return siteUrl;
};
/**
* 工程コンフィグから列の名前と値の組を取得し、オブジェクトに格納して返す
* @returns {Object} listItemObj
*/
const retrieveListItemContent = () => {
const fields = {};
for (let i = 0; i < COLUMN_NUM; i++) {
const columnName = configs.get(`conf_ColumnName${i + 1}`);
let columnValue = configs.get(`conf_ColumnValue${i + 1}`);
if (columnName === '') {
if (columnValue === '') { // 列名と値がともに空の組は無視
continue
}
throw new Error(`C-V${i+1} is set while C-K${i+1} is blank.`);
}
if (fields[columnName] !== undefined) { // 列名の指定が重複
throw new Error(`The field name "${columnName}" is set multiple times.`);
}
if (columnValue === '') {
columnValue = null;
}
fields[columnName] = columnValue;
}
// フィールドが一つも設定されていなくとも、エラーにはしない
// デフォルトの値でリストアイテムを作成する
return { fields };
};
/**
* サイトのメタデータを取得し、siteId を返す
* APIの仕様: https://docs.microsoft.com/ja-jp/onedrive/developer/rest-api/api/shares_get?view=odsp-graph-online
* @param {AuthSettingWrapper} oauth2 OAuth2 設定情報
* @param {String} siteUrl SharePoint サイトの URL
* @returns {String} siteId
*/
const getSiteIdByUrl = (oauth2, siteUrl) => {
// encoding sharing URL
const encodedSharingUrl = encodeSharingUrl(siteUrl);
// preparing for API Request
const response = httpClient.begin()
.authSetting(oauth2)
.queryParam('select', 'id')
.get(`${GRAPH_URI}shares/${encodedSharingUrl}/site`);
const status = response.getStatusCode();
const responseStr = response.getResponseAsString();
if (status !== 200) {
engine.log(responseStr);
throw new Error(`Failed to get site info. status: ${status}`);
}
return JSON.parse(responseStr).id;
};
/**
* 共有URLを unpadded base64url 形式にエンコードする
* @param {String} sharingUrl 共有URL
* @returns {String} encodedSharingUrl エンコードされた共有URL
*/
const encodeSharingUrl = (sharingUrl) => {
let encodedSharingUrl = base64.encodeToUrlSafeString(sharingUrl);
while (encodedSharingUrl.slice(-1) === '=') {
encodedSharingUrl = encodedSharingUrl.slice(0, -1);
}
return `u!${encodedSharingUrl}`;
};
/**
* リストアイテムを追加し、リストアイテム ID を返す
* @param {AuthSettingWrapper} oauth2 OAuth2 設定情報
* @param {String} siteId SharePoint サイトの ID
* @param {String} listTitle リストの名前
* @param {Object} listItemObj リストアイテムの JSON オブジェクト
* @returns {String} listItemId リストアイテム ID
*/
const addListItem = (oauth2, siteId, listTitle, listItemObj) => {
const url = `${GRAPH_URI}sites/${encodeURIComponent(siteId)}/lists/${encodeURIComponent(listTitle)}/items`;
const response = httpClient.begin()
.authSetting(oauth2)
.queryParam('$select', 'id')
.body(JSON.stringify(listItemObj), 'application/json; charset=UTF-8')
.post(url);
const status = response.getStatusCode();
const responseStr = response.getResponseAsString();
if (status !== 201) {
engine.log(responseStr);
throw new Error(`Failed to add list item. status: ${status}`);
}
const listItem = JSON.parse(responseStr);
return listItem.id;
};
/**
* データ項目に保存する
* @param {ProcessDataDefinitionView} dataDef データ項目の ProcessDataDefinitionView
* @param {String} dataString 出力する文字列
*/
function saveData(dataDef, dataString){
if (dataDef !== null) {
engine.setData(dataDef, dataString);
}
}