This project has moved and is read-only. For the latest updates, please go here.

getvalue in display form

Nov 2, 2015 at 1:56 PM
I try to get the value of a boolean (checkbox)
This works ok in edit and new form but doesn't work in display form.

console says " Uncaught TypeError: Cannot read property 'checked' of null "

How to get this working in display form?
Nov 4, 2015 at 4:10 PM
SPUtility doesn't support GetValue on the DispForm right now. I'll take a look and see what I'd need to do to make this work.

In the meantime, you might be able to get this working using a workaround:
// get the field
var field = SPUtility.GetSPField('Yes/No');
// get the dispform value from the row
var value = $(field.LabelRow).children('td.ms-formbody').text().trim();
// value = "Yes"
Nov 5, 2015 at 9:26 AM
Thank you verry much. The work around got things working :-)
Jun 1, 2016 at 9:22 AM
Hi Kit,
According to the change log GetValue on the DispForm is now supported for fields which display as text. However, it always returns undefined for me, e.g. in the code below, which I ran both from the $(document).ready(..) function and when the user presses a button above the form --> am I missing something here?
Many thanks, Chris
var field = SPUtility.GetSPField("MyField");
alert("type = " + field.Type + "\nvalue = " + field.GetValue());  // shows:  type = SPFieldText value = undefined, while field is filled and shown on display form.
Jun 2, 2016 at 2:22 AM
Chris,
Yep it should work for fields like single line of text, choice, and others which display as text only. A few questions:
  • What version of SharePoint are you using?
  • What is the type of the field?
  • Did you try the getting started page?
Thanks,
Kit
Jun 3, 2016 at 12:19 PM
Edited Jun 3, 2016 at 12:20 PM
Hi Kit, thanks for your swift response. To answer your questions:
  • SharePoint 2010.
  • Type of field I'm going for is Single line of text, and SPUtility has no trouble finding this (it shows type = SPFieldText in the alert, as commented in my code). I've tried several other types as well, same problem -> SPUtility retrieves the type, but value is always undefined.
  • I hadn't, but did now: in a newly created document library (which has fields Name and Title) gettingstarted.html shows
    'OK ! jQuery was loaded!
    'OK ! SPUtility was loaded!
    Found a total of 0 fields!
    Field list:
    When used with a new custom list (only field is Title) using a content editor linked to gettingstarted.html above the display form, I get an error message: Error creating field named null: Unknown SPField type'.
I have used other features of SPUtility, e.g. hiding/disabling fields, successfully for some time now, so I really don't know what's going wrong here :s

Hope you have some tips!
Thanks, Chris
Jun 9, 2016 at 2:09 AM
Chris,
Sorry for taking a while to get back to you, been busy. I've been working with a few other people on similar issues caused by a couple of different reasons. Unfortunately I don't have a SharePoint 2010 environment to test with but if you'd like you can try the DEV version of SPUtility.js that I've been working on.

Dev version is here:
https://github.com/kitmenke/sputility/blob/master/src/sputility.js

Please let me know if that works for you.

If it doesn't work, you can send me some of the sample HTML for one of your fields that would help. Here is some jQuery code you can use to print the HTML for the first field to the console. Just press F12 and run:
$('.ms-formtable').find('tr').first().html()

Thanks,
Kit
Jun 14, 2016 at 1:54 PM
Hi Kit,
Thanks again for the response. I tried with the DEV version, but same result: GetValue on any type of field on a DispForm returns undefined.. :(
Here's the HTML code for the first field of my DispForm, retrieved with the jQuery code you suggested:
<TD class=ms-formlabel vAlign=top width=165 noWrap>
<H3 class=ms-standardheader><A name=SPBookmark_Organisatie></A>Organisatie-eenheid</H3></TD>
<TD id=SPFieldText class=ms-formbody vAlign=top width=450><!-- FieldName="Organisatie-eenheid"
                                               FieldInternalName="Organisatie"
                                               FieldType="SPFieldText"
                                 -->CDC/NLDA/KIM </TD>
Btw, I had a quick look at the code of the DEV version, and it does seem to make sense that GetValue returns undefined, e.g. for a SPTextField:
SPTextField.prototype.GetValue uses this.Textbox, but this is null, as getInputControl(this) in the function SPTextField(..) returns null.
Also, the code for getInputControl(spField) contains a comment if running on DispForm.aspx Controls will be null, so this seems to be intentional.

I'm probably not fully understanding your code, but this did seem worth pointing out, hope it helps!

Kind regards,

Chris
Aug 11, 2016 at 9:00 AM
Hi Kit,

Any updates on this issue? I just ran into the same problem: GetValue does not work on display forms.. And I really need it to! :)

Hope to hear from you, kind regards,

Chris
Aug 14, 2016 at 3:42 PM
Chris,
Sorry, haven't had much time to dig into this... not doing SharePoint much anymore. Looking at your code above... seems ok... but it should not be using the normal classes on DispForm. There are two special classes: SPDispFormTextField and SPDispFormField.

It must be a problem with how SPUtility is trying to detect the DispForm. Right now SPUtility.js tries to guess based on the presence of the close button on the form:
$("table.ms-formtoolbar input[value='Close']").length >= 1

I'm guessing, in german the value is not Close but Schliessen (?) which is why it is not working. If you change line 38 of the Dev version you could probably get it working:
https://github.com/kitmenke/sputility/blob/master/src/sputility.js#L38

I hope this helps.
Thanks,
Kit
Aug 19, 2016 at 9:27 AM
Hi Kit, many thanks for your response.

First, detecting the DispForm indeed went wrong. Changing line 38 to $("table.ms-formtoolbar input[value='Sluiten']").length >= 1 (I'm in Holland :) did the trick, but I guess you could also use the window's URL for a language independent solution:
_isDispForm = ~window.location.href.indexOf('DispForm.aspx'); works for me.

After resolving this, GetValue still returned undefined however. I did some debugging and I think the problem is in getSPFieldFromType. That is, changing the index to the controls array in line 1815 from 2 to 1 solved the issue for me:
return new SPDispFormTextField(spFieldParams, controls[1]);

Could this be a bug?

Best regards,

Chris
Feb 14 at 11:33 PM
Edited Feb 14 at 11:36 PM
I was having the same issue on SharePoint 2010 running in IE 8.
The "GetValue" function did not work for any field types, but it worked fine in IE9 (up) and in Chrome.

After some digging I found that the issue is due to the "Controls" object (line 1809) is returned in a different format in IE8. From what I can see, those that normally have "controls.length" of 5, have a length of 3 in IE8.

I made the following changes and they seem to be working for me (not the most elegant, but they work for me so far)...

Dev Version, replace:
// fields which have a text node element
     return new SPDispFormTextField(spFieldParams, controls[2]);
with:
// fields which have a text node element
if(typeof controls[2] === "undefined" || (controls.length === 3 && typeof controls[1].innerText !== "undefined")) {
    console.log(controls);
    return new SPDispFormTextField(spFieldParams, controls[1]);
} else {
    console.log(controls);
    return new SPDispFormTextField(spFieldParams, controls[2]);
}

Minified file, replace:
5===d.length?new S(c,d[3]):new R(c,d[2])
with
    5===d.length?new S(c,d[3]):(typeof d[2]==="undefined" || (d.length===3 && typeof d[1].innerText !== "undefined")?new R(c,d[1]):new R(c,d[2]))
I tested the changes (SP 2010) in IE8-11 and Chrome 56.

I also tested the field types: Single text, multi-line text, choice, number, currency, date/time, lookup, yes/no, person/group, hyperlink, and metadata.