
Microsoft Lists: Add List Item
This item adds a new list item to the specified list on Microsoft Lists.
Basic Configs
- Step Name
- Note
Configs for this Auto Step
- conf_OAuth2
- C1: OAuth2 Setting *
- conf_SiteUrl
- C2: SharePoint Site URL *
- conf_ListTitle
- C3: List Title *
- conf_ListItemId
- C4: Data item to save list item ID
- conf_ColumnName1
- C-K1: Lists Field Name of Column 1
- conf_ColumnValue1
- C-V1: Value of Column 1#{EL}
- conf_ColumnName2
- C-K2: Lists Field Name of Column 2
- conf_ColumnValue2
- C-V2: Value of Column 2#{EL}
- conf_ColumnName3
- C-K3: Lists Field Name of Column 3
- conf_ColumnValue3
- C-V3: Value of Column 3#{EL}
- conf_ColumnName4
- C-K4: Lists Field Name of Column 4
- conf_ColumnValue4
- C-V4: Value of Column 4#{EL}
- conf_ColumnName5
- C-K5: Lists Field Name of Column 5
- conf_ColumnValue5
- C-V5: Value of Column 5#{EL}
- conf_ColumnName6
- C-K6: Lists Field Name of Column 6
- conf_ColumnValue6
- C-V6: Value of Column 6#{EL}
- conf_ColumnName7
- C-K7: Lists Field Name of Column 7
- conf_ColumnValue7
- C-V7: Value of Column 7#{EL}
Notes
- The SharePoint Site URL is the part before
/Listsof the List URL- When the List is in My lists, it looks like
https://{sub-domain}-my.sharepoint.com/personal/{user-identifier}_{domain}_onmicrosoft_com - When the List is in a SharePoint site, it looks like
https://{sub-domain}.sharepoint.com/sites/{site-identifier}
- When the List is in My lists, it looks like
- There are some ways to get field names of the columns:
- When you export a CSV with a schema of the List, the field names are shown in the
Nameproperty of each column’s schema - When you sort the the List by a column on WebUI, the field name of the column is included in the URL in the form of
sortField={fieldName}
- When you export a CSV with a schema of the List, the field names are shown in the
- The supported Microsoft Lists column types are shown in the following table:
| Supported column types on Microsoft Lists | Example Value |
|---|---|
| Single line of text | Single line of text |
| Multiple lines of text | Multiple lines of text |
| Number, Currency | 123 |
| Choice (when multiple selections are disabled) | Choice 1 |
| Date and time * The date and time displayed on Microsoft Lists is based on the timezone setting of the SharePoint Site (which is different from the timezone setting of your Microsoft 365 account) | 2024-03-26 02:15 |
Capture

See Also
Script (click to open)
- An XML file that contains the code below is available to download
- microsoft-lists-listitem-add.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/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);
}
}