Solution - Using the ID of a list item in a calculated column

Oct 4, 2011 at 3:57 AM

Many of us had the experience during our early days in SharePoint of trying to use the ID of a list item in a calculated column. It seems to work at first but fails after the item is edited even though the ID remains the same. After seeing Marc Anderson's SPServices.SPRedirectWithID I realized I could use the same approach to fill a text column in my list with the "static" ID of the item. I could then use the static ID in calculated columns.

I used part of the code from the SPServices.SPRedirectWithID function and developed the following script which is used by a CEWP on the newform page. My list is named "Work Products" and my text column is "StaticID".

I have found this static ID to be very useful in many other ways. Thank you Marc!! (Marc - maybe this idea should be incorporated as a new function in the next SPServices release.)

<script type="text/javascript" src="/Projects/Javascript/jquery-1.6.2.min.cfjs"></script>

<script type="text/javascript" src="/Projects/Javascript/jquery.SPServices-0.6.2.min.cfjs"></script>

<script type="text/javascript">

$(document).ready(function() {

var thisList = $().SPServices.SPListNameFromUrl();
  var queryStringVals = $().SPServices.SPGetQueryString();
  var lastID = queryStringVals.ID;
  var QSList = queryStringVals.List;
  var QSRootFolder = queryStringVals.RootFolder;
  var QSContentTypeId = queryStringVals.ContentTypeId;


  // On first load, change the form actions to redirect back to this page with the current lastID for this user and the
  // original Source.
  if(typeof queryStringVals.ID === 'undefined') {
   lastID = $().SPServices.SPGetLastItemId({
    listName: thisList
   $("form[name='aspnetForm']").each(function() {
    // This page...
  var thisUrl = (location.href.indexOf("?") > 0) ? location.href.substring(0, location.href.indexOf("?")) : location.href;
    // ... plus the Source if it exists
    var thisSource = (typeof queryStringVals.Source === "string") ?
     "Source=" + queryStringVals.Source.replace(/\//g, "%2f").replace(/:/g, "%3a") : "";
    var newQS = [];
    if(typeof QSList !== 'undefined') {
     newQS.push("List=" + QSList);
    if(typeof QSRootFolder !== 'undefined') {
     newQS.push("RootFolder=" + QSRootFolder);
    if(typeof QSContentTypeId !== 'undefined') {
     newQS.push("ContentTypeId=" + QSContentTypeId);

    var newAction = thisUrl +
     ((newQS.length > 0) ? ("?" + newQS.join("&") + "&") : "?") +
     // Set the Source to point back to this page with the lastID this user has added
     "Source=" + thisUrl +
     "?ID=" + lastID +
     // Pass the original source as RealSource, if present
     ((thisSource.length > 0) ? ("%26RealSource=" + queryStringVals.Source) : "") +
     // Pass the override RedirectURL, if present
     ((typeof queryStringVals.RedirectURL === "string") ? ("%26RedirectURL=" + queryStringVals.RedirectURL) : "");
                                   $(this).attr("action", newAction);
  // If this is the load after the item is saved, wait until the new item has been saved (commits are asynchronous),
  // then do the redirect to redirectUrl with the new lastID, passing along the original Source.
  } else {
   while(queryStringVals.ID === lastID) {
    lastID = $().SPServices.SPGetLastItemId({
     listName: thisList
              // put write lastID to staticID column here
             var orgSource = queryStringVals.RealSource;
             //alert (lastID);
            pasteID(lastID, orgSource);

// end of doc ready statement   

function pasteID(itemID, orginalSource){
        operation: "UpdateListItems",
        async: false,
        listName: "Work Products",
        ID: itemID,
        valuepairs: [["StaticID", itemID]],
        completefunc: function (xData, Status) {
        location.href = orginalSource;


Oct 14, 2011 at 12:58 AM


Interesting approach. You could also use a simple SPD workflow to populate the StaticID, but I think you mentioned in another post that you weren't able to use SPD in your current environment.


Oct 14, 2011 at 12:59 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.