Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I open list item for editing and want to execute some javascript for this but i cann't retrieve item Id in Edit form.

I use following code:

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

But items collection is empty. On "View all" List form this code work well. Throubles only in EditForm. Can anyone help me?

share|improve this question
add comment

4 Answers

up vote 6 down vote accepted

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.

share|improve this answer
    
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. –  Fobos Jan 30 '13 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 '13 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. –  Fobos Jan 30 '13 at 15:18
    
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 '13 at 16:05
    
_spGetQueryParam('id') works well, thanks! –  Fobos Jan 31 '13 at 7:42
show 1 more comment

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"];
share|improve this answer
add comment
var pairs = window.location.search.substring(1).split('&');
var pair = pairs[0].split('=');
var ID = pair[1];
share|improve this answer
    
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? –  Robert Lindgren Sep 10 '13 at 18:22
add comment

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.

share|improve this answer
    
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? –  Robert Lindgren Sep 10 '13 at 18:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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