0

Is it possible to display the default ID of a SharePoint list in the edit form. I have used the mentioned JavaScript code but it still doesn't work. My work environment is SharePoint O365.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
  // Get the ID from the query string
  var id = getQueryString()["ID"];

  // Find the form's main table
  var table = $('table.ms-formtable');

  // Add a row with the ID in
  table.prepend("<tr><td class='ms-formlabel'><h3 class='ms-standardheader'>ID</h3></td>" +
            "<td class='ms-formbody'>" + id + "&nbsp;</td></tr>");
})

function getQueryString() {
  var assoc = new Array();
  var queryString = unescape(location.search.substring(1));
  var keyValues = queryString.split('&');
  for (var i in keyValues) {
    var key = keyValues[i].split('=');
    assoc[key[0]] = key[1];
    }
  return assoc;
}
</script>
0

The listID should be exposed in _spPageContextInfo.listId. No need to dissect the query-string.

0

You logic is correct, you just need to put call to function in document.ready.

$( document ).ready(function() {
 // Get the ID from the query 
string var id = getQueryString()["ID"]; 
// Find the form's main table 
var table = $('table.ms-formtable'); 
// Add a row with the ID in 
  table.prepend("<tr><td class='ms-formlabel'><h3 class='ms-standardheader'>ID</h3></td>" +
            "<td class='ms-formbody'>" + id + "&nbsp;</td></tr>");
});

function getQueryString() { 
var assoc = new Array(); 
var queryString = unescape(location.search.substring(1)); 
var keyValues = queryString.split('&'); 
for (var i in keyValues) { 
var key = keyValues[i].split('='); assoc[key[0]] = key[1]; 
} 
return assoc; 
}

Hope this helps...Happy coding!!!!

  • Hi, thank you for this solution. I tried but still doesn't work. I'm not sure if it is an issue with my jquery. Anyways thanks again. :) – Imesha Siriwardane May 23 at 6:11
  • did you tried to debug? is var table = $('table.ms-formtable'); object returning element? did you put code between document.ready in console after page is loaded ? – Siddharth Vaghasia May 23 at 9:07
0

I tested your code snippet in original question and it could return the current list item Id in edit form as expected:

enter image description here

enter image description here

For current list Id, it should be a guid format, you could use _spPageContextInfo.listId directly: enter image description here

<script type="text/javascript">
$(function() {
  // Find the form's main table
  var table = $('table.ms-formtable');

  // Add a row with the ID in
  table.prepend("<tr><td class='ms-formlabel'><h3 class='ms-standardheader'>ID</h3></td>" +
            "<td class='ms-formbody'>" + _spPageContextInfo.listId  + "&nbsp;</td></tr>");
})
</script>

enter image description here

  • Hi, Thanks a lot but my code still doesn't seem to work. Quick question. Where should I use _spPageContextInfo.listId ? I'm sorry I'm quite new to SharePoint and have used only SharePoint online. – Imesha Siriwardane May 23 at 6:13
  • Will add the modiftied code in the answer, please check – Jerry_MSFT May 23 at 6:18
  • @ImeshaSiriwardane, is there any updates ? – Jerry_MSFT May 29 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.