Get the field value from dispform.aspx in SharePoint 2010 – 2013 via jQuery

I was trying to find a quick way to get the field value from dispform.aspx in SharePoint. Apparently, the internet is filled with .. let us say “less-interesting methods” to get that value..(with a big jquery function or SPServices or inline c#)

Well, standard SharePoint gives you this:

<tr>
<td nowrap="true" valign="top" width="165px" class="ms-formlabel"><h3 class="ms-standardheader">
<a name="SPBookmark_Field1"></a>Field1</h3></td>
<td valign="top" class="ms-formbody" width="450px" id="SPFieldText">
<!-- FieldName=" Field1"
FieldInternalName=" Field1"
FieldType=" Field1"
-->
This is the text of Field1

</td>
</tr>

A tr line, with 2 td fields. You can get the first field (via the H3 tag), but there is no way to get the second field directly.
So you use the JQuery .next() functionality!

So the code will be:

var sField1 = $('h3:contains("Field1")').closest('td').next('td').text();

What this code does is create a variable, sField1. It searches the h3 tag on your page, gets the enclosing td tag. Then we take the next tag, and the text of that td. Like the French say: “Simple comme bonjour”.

And that is how you get the field value from dispform.aspx in SharePoint via jQuery!

About: Marijn

Marijn Somers (MVP) has over 14 years experience in the SharePoint world, starting out with SP2007. Over the years the focus has grown to Office 365, with a focus on collaboration and document management. He is a business consultant at Balestra and Principal Content Provider for "Mijn 365 Coach" that offers dutch employee video training. His main work tracks are around user adoption, training and coaching and governance. He is also not afraid to dig deeper in the technicalities with PowerShell, adaptive cards or custom formatting in lists and libraries. You can listen to him on the biweekly "Office 365 Distilled" podcast.


20 thoughts on “Get the field value from dispform.aspx in SharePoint 2010 – 2013 via jQuery”

  1. This does make sense but didn't work for me so I must be doing something wrong. I used added the script to the CEWP and placed it below the table displaying the information.

    this is what I have:

    Is this not correct? As you can see the Field name is DashboardKey with a calculated text value in it (which is actually the [Created] value.

  2. Nilioq, I cannot see your comment ( my bad, you need to change the < in a < character.

    Your jquery should be:
    Var sField1 = $('h3:contains("DashboardKey")').closest('td').next('td').text();

    If you add an alert to the code, does that work ?

  3. No alert happens. I even added a document.write with some text and that doesn't appear either, almost like the script is not being read.

    I was also advised to use the get parent().parent() but I have never used that before and your solution makes more sense.

  4. Then I think something is wrong on where you call the jquery script!

    If the alert doesn't show, something is wrong before. I don't know the whole code you pasted, but it seems to me your jquery script isn't read!

  5. So I removed calling the jquery script (put it above the table is dispForm) and just have this below the table (in between the script tag):

    Var sField1 = $('h3:contains("DashboardKey")').closest('td').next('td').text();
    alert("Hello");
    alert("KeyValue : " + sField1);

    The "Hello" does not alert either

  6. Nilios, I think the reference to the jquery file is wrong.

    Try downloading the jquery script from jquery.com and upload it in a document library or something.

    Then add a reference to that specific file instead of to the api at google.
    Something like this:
    <script src="http://yourlocation/jquery.min.js"></script&gt;

    Then see if you can get the alert("Hello") working. That is the basis..if that works, then you can focus on the sharepoint field.

  7. Thanks Marjin,

    I tried that and it still doesn't seem to work. I will have to troubleshoot more and maybe do some trial and error (put an alert below calling of jquery and that pops up). It seems that nothing between the script tags in the code below the table in dispForm is happening (put an alert above the var sfield line and it doesn't pop up).

    Appreciate the help though.

  8. I think the issue is for dispForm, no javascript seems to be running when placed below the table. I will have to analyze source.

    Appreciate all your help.

  9. So I have troubleshooted as much as I could, maybe my SP2007 page is not taking the code you posted because everything seems to work except for that line (commented the line and everything else runs even below the table).

    It is a calculated value that I am trying to get. Would that make a difference?

  10. look at the source code of the page and locate the field you are trying to get.
    Is that also 2 TD items in a TR ?

  11. Yes – I have

    tr
    td fieldname1
    td FieldName="DashboardKey" FieldInternalName="DashboardKey"
    FieldType="SPFieldCalculated"
    calculated value
    end tr

  12. go figure, I changed the capital v in "Var" to "var" and it worked… no idea why that made a difference but it works now!

Leave a Reply

Your email address will not be published. Required fields are marked *