5

I open a list item for editing and want to execute some JavaScript for this but I can't retrieve the item Id in the Edit form.

I use following code:

var context = SP.ClientContext.get_current();
var items = SP.ListOperation.Selection.getSelectedItems(context);

But the items collection is empty. On "View all" List form this code works well. The problem exists only in the EditForm. Can anyone help me?

6 Answers 6

8

The Item ID is passed via url to the Edit Form. so you cab get it using this : _spGetQueryParam('id') Let me know if this works.

5
  • When i open EditForm and execute command in javascript console 'window.location' i see 'sharepoint/TestSite/Lists/ListForUser3/ListAll.aspx' There is no anyone params. I'm helpless. Jan 30, 2013 at 15:03
  • Because you open your edit form in a popup, the context refers to the host page (ListAll.aspx). To see the parameters of your Edit Form do the following : 1- Go to the list settings and open the Advanced Settings Section. 2- Change the Parameter : Open the pages in a popup to No. come back to the list and open the edit form. now the you can test if your id is here
    – must19
    Jan 30, 2013 at 15:11
  • I want to allow user additional functionality when he works with my list. If he selects any item in list and press standart sharepoint "Edit button" standart popup edit window will be presented. And I must to work with that window and somehow retrieve item ID in him. Jan 30, 2013 at 15:18
  • 1
    To Add functionnalities you must add your code in the EditForm.aspx page and in your code use _spGetQueryParam('id') to retrieve the ID of the current item.
    – must19
    Jan 30, 2013 at 16:05
  • Nice. Used this function to pull a parameter for a redirect.... <script type="text/javascript">window.location.replace('xxxinc.sharepoint.com/connect/EMEA/Operations/ProjectPortfolio/…' + _spGetQueryParam('id'));</script>
    – 1c1cle
    May 14, 2013 at 21:11
6

The JSRequest class is a JavaScript object that lives on all SharePoint Pages, and allows us to simply and quickly get query string values using JavaScript.

//First we must call the EnsureSetup method
JSRequest.EnsureSetup(); 

//Get a query string parameter called Id. i.e - "page.aspx?Id=11" will return 11
var itemId = JSRequest.QueryString["ID"];
1
  • 2
    +1, _spGetQueryParam is not available in SP 2013. Dec 22, 2014 at 8:49
1

This is my answer with jQuery.

 $(document).ready(function() {
   var vars = [], hash;  
   var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
   for(var i = 0; i < hashes.length; i++){     
     hash = hashes[i].split('=');
     vars.push(hash[0]);
     vars[hash[0]] = hash[1];
  }   


   var Id = vars["ID"].toString();

   alert(Id);

Best Regards.

1
  • Why use this instead of the built in SharePoint method suggested above? Any performance differences? Or so you have another reason for choosing not to use _spGetQueryParam? Sep 10, 2013 at 18:23
0
var pairs = window.location.search.substring(1).split('&');
var pair = pairs[0].split('=');
var ID = pair[1];
2
  • 1
    Why use this instead of the built in SharePoint method suggested above? Any performance differences? Or so you have another reason for choosing not to use _spGetQueryParam? Sep 10, 2013 at 18:22
  • you are right Robert. I have used the SharePoint way: _spGetQueryParam and it works.
    – Ola
    May 14, 2014 at 7:37
0
var itemID = getParameterByName('ID');    

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

JavaScript will parse the URL for you.

const url = new URL(location.href);
url.searchParams.get('ID');

Here's a good reference for using the output of the URL() constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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