Updated the code examples to support lists with folders. I have also added a code example for use with DFFS (update DFFS to 3.26 or above). You must update spjs-utility to the latest version.
This solution let you use the item ID in a calculated column as part of a formatted “autonumber”.
Please note that if you apply the number in NewForm you cannot guarantee that the ID it retrieves is correct because someone other than you can “steal” the ID in a scenario where two or more users submit to the same list or library at the exact same time. In this case you can end up with a duplicated ID.
This is the price you must pay to have this number added in NewForm. If you can wait to add the number in EditForm, you are guaranteed a correct result. You can also use the code in both NewForm AND EditForm to correct any wrong ID added in NewForm.
Add a new column of type “Single line of text”, and the name “_ID” to the list. Add a HTML form web part to the NewForm / EditForm of the list, and add the code supplied below.
Please note that the column “_ID” must be included in all content types as it must be present in the form. The code will hide the field from view.
This code kicks in when you hit the save button, the function “PreSaveItem” queries the current list for the last item added. It gets the item ID, and increment the number by 1, before writing it to “_ID”.
This _ID column can now be used in a calculated column to create a number format of your liking.
<script type="text/javascript" src="/Scripts/jquery-1.10.2.js"></script> <script type="text/javascript" src="/Scripts/spjs-utility/spjs-utility.js"></script> <script type="text/javascript"> // spjs-utility.js version check if(spjs.utility.version < 1.178){ alert("You must upgrade spjs-utility.js to v1.178 or above."); } $(document).ready(function(){ // Hide the field $("input[title='_ID']").parents("tr:first").hide(); }); function PreSaveItem(){ var listGuid, listBaseUrl, query, res; listGuid = _spPageContextInfo.pageListId; listBaseUrl = _spPageContextInfo.siteServerRelativeUrl !== "/" ? _spPageContextInfo.siteServerRelativeUrl : ""; query = "<Where><IsNotNull><FieldRef Name='ID' /></IsNotNull></Where><OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>"; res = spjs_QueryItems({"listName":listGuid,"listBaseUrl":listBaseUrl,"query":query,"viewFields":["ID"],"rowLimit":"1","scope":"Recursive"}); if(res.count === 0){ $("input[title='_ID']").val("1"); }else{ $("input[title='_ID']").val(parseInt(res.items[0].ID,10)+1); } return true; } </script>
The PreSaveItem function will automatically execute when the list item is saved, and it coexists with any instances of PreSaveAction.
If you want to use this code with Dynamic Forms for SharePoint you must do the following:
- Update DFFS to v3.26 or above
- Update spjs-utility.js to v1.178 or above
- Replace the function PreSaveItem with this function:
function dffs_PreSaveAction(){ var listGuid, listBaseUrl, query, res; listGuid = _spPageContextInfo.pageListId; listBaseUrl = _spPageContextInfo.siteServerRelativeUrl !== "/" ? _spPageContextInfo.siteServerRelativeUrl : ""; query = "
"; res = spjs_QueryItems({"listName":listGuid,"listBaseUrl":listBaseUrl,"query":query,"viewFields":["ID"],"rowLimit":"1","scope":"Recursive"}); if(res.count === 0){ $("input[title='_ID']").val("1"); }else{ $("input[title='_ID']").val(parseInt(res.items[0].ID,10)+1); } }
Please note that this code example is for SP2010 and 2013. If you are using SP2007 you must change the variables "listGuid" and "listBaseUrl" like this:
listGuid = "GUID or display name of the list"; listBaseUrl = L_Menu_BaseUrl;
You find jQuery here, and spjs-utility.js here.
<script type="text/javascript" src="/Scripts/jquery-1.10.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ // Hide the field $("input[title='_ID']").parents("tr:first").hide(); }); function PreSaveItem(){ $("input[title='_ID']").val(GetUrlKeyValue("ID")); return true; } </script>
This code pulls the ID from the URL and writes it to "_ID".
If you want to use this code with Dynamic Forms for SharePoint you must update DFFS to v3.26 or above, and replace the function PreSaveItem with this function:
function dffs_PreSaveAction(){ $("input[title='_ID']").val(GetUrlKeyValue("ID")); }
<script type="text/javascript" src="/Scripts/jquery-1.10.2.js"></script> <script type="text/javascript"> $(document).ready(function(){ // Hide the field $("a[name='SPBookmark__ID']").parents("tr:first").hide(); }); </script>
I have a choice column named "Category", with the following options:
A_Something
B_Something
C_Something
I then add a calculated column with this formula:
=LEFT(Category,1)&"-"&IF(VALUE(_ID)<10,"000"&_ID,IF(VALUE(_ID)<100,"00"&_ID,IF(VALUE(_ID)<1000,"0"&_ID,_ID)))
This code pads the number up to a four digits with leading 0 like this:
A-0001
A-0002
...
A-0012
A-0013
...
A-0123
A-0124
...
A-1234
A-1235
Let me know if you have any questions,
Alexander