1

One of the projects I am working on, I am developing a JSOM solution to do certain things. Part of the solution I have to read the "ID" that appears on the SharePoint list's edit form (in the browser url). Please see below screenshot.

Edit Form As seen in the above screenshot, I want to read that ID value which equals to 4 and based on that value i have to further call other functions. I googled a lot but could not find a way to read that ID. Can someone please help me on this, thanks. I am working with SharePoint/office 365. Please provide code for this using JS or Jquery.

Thanks in advance.

1

The following code for your reference:

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript">

$(document).ready(function() {  
    var itemID = getParameterByName('ID'); 
    alert(itemID);     
});

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, " "));
}

</script>

The Result is: enter image description here

  • thanks a bunch, I almost spent a day digging for this, it worked. – mdevm Apr 12 '18 at 13:47
  • asking one more help, now that I am capturing the ID, will it be possible to capture the URL portion from the Asset Link hyperlink column by passing the ID? Thanks. – mdevm Apr 12 '18 at 14:03
  • I have replied your another thread about this similar issue, you can refer to it: sharepoint.stackexchange.com/questions/239787/… – Kally_MSFT Apr 13 '18 at 1:38
1

You can use SharePoint's internal utility class to grab information from a query string.

<script type="text/javascript">
    JSRequest.EnsureSetup();
    itemId = JSRequest.QueryString["ID"];
</script>
0

Use below in script editor:

$(document).ready(function () {
  var ID = GetUrlKeyValue('ID');
});
0

There should also be an object created by SharePoint on classic forms: WPQ1FormCtx. I have seen this been named WPQ2FormCtx too for some reasons I don't know.

So you could get it like this: WPQ1FormCtx.ItemAttributes.Id. There is also alot of other nice props in the object that could be useful.

Guess you could do it like this:

(window["WPQ1FormCtx"] || window["WPQ2FormCtx"]).ItemAttributes.Id

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.