Generate a custom unique ID for SharePoint list items

Applies to: SharePoint Online and On-Premises

Description

SharePoint generates a unique ID for each list item. This Id is numeric that is incremented for each new item. For many business scenarios, it is required to have an alpha-numeric Id. For example, for a Purchase Order it could be PO-<YEAR>-<Id>. Here the Id contains a fixed prefix PO, 4 digit running year and a unique Id.

This article describes the steps to generate the custom unique Id for each item in required format. This process uses the SharePoint generated numeric Id as part of the alpha-numeric Id so that it is guaranteed to be unique for each item. These steps take the example where Id is based on values in the list item column ‘Department’, current year (as per item creation time) and the SharePoint generated item Id.

Example: “IT/2019/00001”

  • “IT” represents the Department
  • “2019” represents the current year
  • “0000” is the prefix for the Item Id
  • “1” is the SharePoint generated unique item Id

Summary of Steps

  1. Configure Execute Script action in Custom Action
    • This action generates the unique ID
  2. Invoke custom action in NITRO form

Detailed Steps

1. Configure Execute script action in Custom Action to generate unique Id

Add a new custom action with an execute script action as shown below. This action generates the unique custom Id for the Purchase Request based on Department Id, Current Year and SharePoint generated unique Item Id.

Note: Below action will work for new items. For existing items you can configure a similar action with added conditions so that it is visible only for older items where unique Id is not set. Additional conditions will be to check that “Purchase Request #” is empty. More conditions can be added as required such as based on status. Enable this custom action for multiple items by selecting option “List View Ribbon” and “Enable for multiple items” in the custom action configuration.

Custom Action

Execute Script action

Script

if(!currentItem){
window.ccs_g_Utils.writeLog("Error accessing the list item");
functionCallback(true, "Error accessing the list item");
return;
}

var PRNumberFieldInternalName = "PRNumber";
var itemCreated = currentItem.get_item("Created");
var departmentCode = currentItem.get_item("DeptCode");
var itemId = currentItem.get_id();
var PRID = departmentCode + "/" + (new Date(itemCreated + "")).getFullYear() + "/" + ("00000" + itemId).slice(-5);

if (listColumnsInfo.hasOwnProperty(PRNumberFieldInternalName) && listColumnsInfo[PRNumberFieldInternalName].FieldTypeKind == SP.FieldType.text) {
currentItem.set_item(PRNumberFieldInternalName, PRID);
currentItem.update();
spContext.executeQueryAsync(
function () {
functionCallback();
}, function (sender, msg) {
functionCallback(true, msg.get_message());
});
}
else {
functionCallback(true, "'PRNumber' column does not exist or is not a text type of column");
}

Note:

  • In the above script, modify the “PRNumber” with internal name of the “Purchase Request #” column for which you want to update with unique generate item Id
  • Modify the “DeptCode” with internal name of the Department column

2. Invoke custom action in NITRO form

Go to the Purchase Request list -> List Settings -> Crow Canyon NITRO Forms -> Drag and drop the ‘Submit Action’ button and select the custom action configured above (“Generate Purchase Request Id”)