1

I tried so many ways to get list item id but could not. Ofcourse i got one solution but it is not much proper solution. Can anyone give smart solution to get list item id by using js. I'm using sp 2010.

I tried this solution:

function getQuerystring(key, default_)
{

  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");

  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);

  if(qs == null)
    return default_;

  else
    return qs[1];
 }
 var itmID = getQuerystring('ID');

But it is not good way to get list item id,,.. Please help me to solve this. Thank you.

  • Where is the information about this ItemID? Is it in query string. If it is there, can you share how the url looks like? – Nadeem Yousuf Mar 3 '14 at 7:21
  • 1
    Not sure what you are looking for. In the comments of one of the answers it looks you want to get the list item id of the item which is selected in List View Webpart. And in other comment, it looks like you want to get it from query string. If you want to get a proper answer, you should try to explain your question properly. – Nadeem Yousuf Mar 3 '14 at 11:25
1

If you want to get the id of the document/item which is selected you can use this(source):

var ctx = SP.ClientContext.get_current();
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
var myItems = '';
var i;

for (i in items){
    myItems += '|' + items[i].id; //String with all the IDs of the selected documents/items
}

And here is another way to get the selected items.

  • Thanks for your reply Mbauer.. I placed ur code and i took" var itmID=items[i].id " in for loop but didn't get id. – Flying Hope Mar 3 '14 at 7:13
  • I updated my answer. You can also look here: sharepoint.stackexchange.com/questions/11169/… – mbauer Mar 3 '14 at 8:08
  • I seen your post but unable to get current list item id. And you said in the above, myItems has string with all id's.. here how can i retrieve current list item id from those id's?? – Flying Hope Mar 3 '14 at 8:54
  • the string myItems contains all IDs of the selected documents/items – mbauer Mar 3 '14 at 9:38
  • understood mbauer... the how can we separate current item id from those all id's? – Flying Hope Mar 3 '14 at 9:40
0

I assume you need to get Item Id from query string:

How to get query string values:

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

For more details refer How can I get query string values in JavaScript?

Example:

console.log(getParameterByName('ID')); //print List Item Id

The function SP.ScriptHelpers.getDocumentQueryPairs could be used to parse the query string in SharePoint 2013. The following example demonstrates how to retrieve List Item Id:

ExecuteOrDelayUntilScriptLoaded(
    function()
    {
         var pairs = SP.ScriptHelpers.getDocumentQueryPairs();
         var itemId = pairs.ID; //get Item Id
         console.log(itemId);  

      }, 'SP.init.js');
  • Hi vadim.. it is also same like the way what im currently using . Any alternative techniques to get list item id? Thank you. – Flying Hope Mar 3 '14 at 11:14
  • Hi praveen, the answer has been updated with example how to retrieve list item id using built-in SharePoint capabilities – Vadim Gremyachev Mar 3 '14 at 12:30
  • Thanks for that Vadim... but which is not working on sp 2010. – Flying Hope Mar 3 '14 at 13:16

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.