Monday, January 01, 2007

SharePoint V3: Workaround for creating a unique id for list items and documents

SharePoint V3 does not provide a good way to uniquely id list items and/or documents. However, the SharePoint Designer workflow capabilities can be used to configure a workaround.
One way to ensure a unique id is to create the id number based on current date and time, down to the precision of a second. This method won't provide a sequential id number for items in a list or document library, but in many real life scenarios as long as the id number is unique, it will suffice.
SharePoint offers a calculation field type for lists and document libraries. This is great for performing the calculation of the id number. However, you do not want to store the id number in this type of field because it is dynamic and the value it generates will change based upon the system. What I mean is, if you generate a unique id off of the created field, then some how the created date is changed, the value of your calculation will be compromised. This happens if you move item from one web part to another. So, instead, it is best to store the id number in a text field where it is safe.
SharePoint V2 didn't allow you to copy a value from a calc field to a text field, but, V3 offers workflow and it allows you to do this.
In this example, I will demonstrate how to generate a unique id for custom list items. The result is a unique id (although not sequential) that is stored in a single line text field.

1. Create a site in the portal to contain the demo custom list
2. Create a custom list with the following fields

  • Title (single line text)
  • Notes (mulitple line text)
  • ItemIDText (single line text)
  • ItemIDCalc (calculated, single line of text)
3. Set the ItemIDCalc field to this regular expression:
=MID(YEAR([Created]),1,4)&IF(LEN(MONTH([Created]))=1,"0"&MID(MONTH([Created]),1,1),IF(LEN(MONTH([Created]))>1,MID(MONTH([Created]),1,2)))&IF(LEN(DAY([Created]))=1,"0"&MID(DAY([Created]),1,1),IF(LEN(DAY([Created]))>1,MID(DAY([Created]),1,2)))&IF(LEN(HOUR([Created]))=1,"0"&MID(HOUR([Created]),1,1),IF(LEN(HOUR([Created]))>1,MID(HOUR([Created]),1,2)))&IF(LEN(MINUTE([Created]))=1,"0"&MID(MINUTE([Created]),1,1),IF(LEN(MINUTE([Created]))>1,MID(MINUTE([Created]),1,2)))&IF(LEN(SECOND([Created]))=1,"0"&MID(SECOND([Created]),1,1),IF(LEN(SECOND([Created]))>1,MID(SECOND([Created]),1,2)))

4. Create a view for the list displaying all of the fields

5. Using Sharepoint Designer, open the site

6. File, New, Workflow

7.
Name the workflow as "ItemID Workflow"
Set the workflow to start automatically when item is created or manually
Click Next

8.
In Step 1 of workflow:
Set the condition as If Created equals ListName: Created
Set the action as Update item in ListName
Field = ItemIDText
Value = ListName: ItemIDCalc
Click Finish

9.
Add a new item to the list. Populate the Title and Notes field. Click ok. You will see that the item is created and that the ItemIDCalc field and ItemIDText Field automatically populate with a unique item id.

No comments:

Post a Comment