0

I am customize the DispForm.aspx with some javascript code. I would like to get in javascript the path of the current listitem. How can I do that?

UPDATE:

I have now this code to get the path/url of the current listItem. Is it OK or can it be better?

                // get list ID
                var listId = _spPageContextInfo.pageListId;
                var cleanListId = listId.replace('{','').replace('}','');

                // get item ID
                JSRequest.EnsureSetup(); 
                var itemid = JSRequest.QueryString["ID"];

                // get current site collection url
                var currentSiteCollectionFullUrl = window.location.protocol + '//' + window.location.host + _spPageContextInfo.siteServerRelativeUrl;

                // generate path of current listitem
                var path = currentSiteCollectionFullUrl + '_layouts/15/listform.aspx?PageType=4&ListId=' + cleanListId + '&ID=' + itemid;
0
3

How to get List Item Id in Display Form page:

//Source: http://stackoverflow.com/a/901144/1375553
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, " "));
}

var listItemId = parseInt(getParameterByName('ID'));
console.log(listItemId);
1

You may like to use the information stored in _spPageContextInfo. It contains information about list item Id, List id etc. Example:

 _spPageContextInfo 
{
    webServerRelativeUrl : "/ProjectWeb",
    webLanguage : 1033,
    currentLanguage : 1033,
    webUIVersion : 4,
    pageListId : "{c1d7b89f-f07b-4e2e-b89c-76c315831d59}",
    pageItemId : 5,
    userId : 68,
    alertsEnabled : true,
    siteServerRelativeUrl : "/",
    allowSilverlightPrompt : "True"
} 

See this for more information: http://johnliu.net/blog/2012/2/3/sharepoint-javascript-current-page-context-info.html

UPDATE

Just checked it using View source of display page. pageItemId is not present there. However, you can use SharePoint’s GetUrlKeyValue() function to get item id. Example:

var thisItemId = GetUrlKeyValue("ID", false, location.href);
1
  • I am trying this code but pageListId is working, but pageItemId is undefined: <script type="text/javascript"> ExecuteOrDelayUntilScriptLoaded(init,'sp.js'); function init(){ this.clientContext = new SP.ClientContext.get_current(); var listId = _spPageContextInfo.pageListId; var itemId = _spPageContextInfo.pageItemId; alert(itemId); } </script> – Ola May 9 '14 at 7:07

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.