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.

Arsalan Adam Khatri
  • 14,531
  • 3
  • 36
  • 59
Flying Hope
  • 357
  • 2
  • 8
  • 20
  • 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-AIS Mar 03 '14 at 07: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-AIS Mar 03 '14 at 11:25

2 Answers2

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.

mbauer
  • 597
  • 1
  • 7
  • 21
  • 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 03 '14 at 07:13
  • I updated my answer. You can also look here: http://sharepoint.stackexchange.com/questions/11169/how-do-you-get-the-current-list-item-in-javascript?rq=1 – mbauer Mar 03 '14 at 08: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 03 '14 at 08:54
  • the string myItems contains all IDs of the selected documents/items – mbauer Mar 03 '14 at 09:38
  • understood mbauer... the how can we separate current item id from those all id's? – Flying Hope Mar 03 '14 at 09:40
  • please be more specific. do you need all ids of the selected documents/items or do you need all ids of a list? – mbauer Mar 03 '14 at 09:45
  • ya Mbauer..I want to get id of selected list item . From the above we can get all list item id's but not selected list item... right? – Flying Hope Mar 03 '14 at 10:46
  • the code in my answer will return the id of the selected elements. it works fine for me. is it possible that the sp.js isn't loaded when the code above runs? – mbauer Mar 04 '14 at 06:32
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');
Vadim Gremyachev
  • 42,498
  • 3
  • 86
  • 167