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

SPUtility.GetSPField returns NULL when using in DispForm.aspx

Feb 15, 2012 at 6:52 PM
Edited Feb 15, 2012 at 7:43 PM

Hi,

SPUtility.GetSPField() function worked fine if I use it in Content Editor Webpart in EditForm.aspx

But When I am trying to use it in the DispForm.aspx, SPUtility.GetSPField() is returning null. I tried with different fields, it is not working on any fields.

If anybody has experienced this, please let me know what is the issue and how to fix it.

Thank You

 

Feb 16, 2012 at 6:10 PM

Some things to check:

  1. Are you including Prototype.js before SPUtility.js?
  2. Are you using _spBodyOnLoadFunctionNames or Event.observe to run after the page has loaded?

Can you paste the code you're including at the top of the page using SharePoint designer?

Mar 8, 2013 at 1:25 PM
I am having the same problem.
  1. Yes, including Prototype.js before SPUtility.js... just like the documentation...
  2. I've tried both methods _spBodyOnLoadFunctionNames and Event.observe. Both methods work for EditForm.aspx, but fail for DispForm.aspx.
  3. Code Snip:
    var fldCurr;
    fldCurr = SPUtility.GetSPField('Skill Level');
    if (fldCurr == null) alert("Skill Level is null");
    else fldCurr.Hide();
    
Pops alert "Skill Level is null" on DispForm.aspx and works fine on EditForm.aspx.

Full sample:
~~~~~~~~~~
function MyCustomExecuteFunction()
{
try {
    // TODO: Put your code here...
    //SPUtility.GetSPField('Name').SetValue('Hello world!');
    var fldCurr;
    //fldCurr = SPUtility.GetSPField('Skill Level');
    //if (fldCurr == null) alert("Skill Level is null");
    //else fldCurr.Hide();

    fldCurr = SPUtility.GetSPField('Specific Skills').Hide();
    //if (fldCurr == null) alert("Specific Skills is null");
    //else fldCurr.Hide();

    SPUtility.GetSPField('Current SOW').Hide();
    SPUtility.GetSPField('Current Rate').Hide();
    SPUtility.GetSPField('New SOW Rate').Hide();
    SPUtility.GetSPField('New Discounted Rate').Hide();
    SPUtility.GetSPField('18 Month Date').Hide();
    SPUtility.GetSPField('Mandatory Roll Off Date').Hide();
    SPUtility.GetSPField('Potential Candidates').Hide();
} catch (ex) {
    alert(ex.toString());
}
}

_spBodyOnLoadFunctionNames.push("MyCustomExecuteFunction")
~~~~~~~~~~~~

I believe SharePoint is 2007, WSS 3.0.
SPUtility.js Version: 0.8.1
Prototype.js Version: '1.7.1',


Thanks!
Mar 9, 2013 at 6:06 PM
Thank you for the detailed info... it helps a lot. I am able to reproduce this in my test list on DispForm.aspx.

I'll take a look and let you know what I find out.
Mar 9, 2013 at 7:45 PM
Hey guys,
I have implemented a fix, tested it against SP2007, and incorporated it into my newest bug fix release (0.8.2).

You can download 0.8.2 here: http://sputility.codeplex.com/releases/view/65209. Let me know if you have any more issues!
Thanks,
Kit
Aug 20, 2013 at 11:54 AM
Edited Aug 20, 2013 at 11:59 AM
Hello KitMenke,
I am facing same issue in the Sharepoint 2010 environment. In display form I am not able to hide or show the fields. It is giving me script error like below,
> Unable to get value of the property 'Show': object is null or undefined
My code is as follows.
function TestInForms(){
    try {
var tempSelection =SPUtility.GetSPField('Criar Proximo Evento').LabelRow.cells[1].innerText;

if(tempSelection=='Yes ')
{
   ShowHideFormFields('Show');
}
else
{
   ShowHideFormFields('Hide');
        
    } catch (ex) {
        alert(ex.toString());
    }
}

function ShowHideFormFields(selOptn){

if(selOptn=='Show')
{
                SPUtility.GetSPField('Email Subject').Show();
        SPUtility.GetSPField('Email Address').Show();
        SPUtility.GetSPField('Email Content').Show();
        SPUtility.GetSPField('Documents').Show();

}
else if(selOptn=='Hide'){

                SPUtility.GetSPField('Email Subject').Hide();
                SPUtility.GetSPField('Email Address').Hide();
        SPUtility.GetSPField('Email Content').Hide();
        SPUtility.GetSPField('Documents').Hide();

}

}
_spBodyOnLoadFunctionNames.push('TestInForms');
Can you please check that. The fix what you given may worked on 2007 but not working in 2010. I want to utilize this same in 2013 version also. Please help me as early as possible.
Aug 20, 2013 at 5:22 PM
Edited Aug 20, 2013 at 5:23 PM
ymihir,
Some bad news... I don't have access to a SharePoint 2010 environment right now so I'm not going to able to look at (or fix) this until I find a 2010 site somewhere to test.

I do have access to a 2013 environment and right now SPUtility does NOT work there because of its dependence on Prototype.js (prototype conflicts with out of the box sharepoint scripts). I'm working on a jQuery version but that is not ready.

I would recommend the following in the meantime:
  1. Download jQuery (version 1.X because that supports older IE browsers)
  2. Write some quick JavaScript to hide the rows. Here is something I threw together real quick for SharePoint 2013:
function SPDispForm() {
    var self = this;
    self.fields = {};
    $('h3.ms-standardheader').each(function(index, elem) {
        var name = $.trim($(elem).text());
        self.fields[name] = $(elem);
    });
    
    self.hideRow = function(fieldName) {
        if (fieldName in self.fields) {
            self.fields[fieldName].closest('tr').hide();
            return self.fields[fieldName];
        }
        throw 'Field named ' + fieldName + ' was not found';
    };
}
Usage:
$(window).load(function() {
    var myform = new SPDispForm();
    myform.hideRow('Email Subject');
    myform.hideRow('Email Address');
    myform.hideRow('Email Content');
    myform.hideRow('Documents');
});
Hopefully this helps you.
Thanks,
Kit
Aug 22, 2013 at 7:19 AM
Thank you so much Kit. I also achieved in the similar way for the display form. That is fine. But another issue i am facing is
 IsRequired property 
not working.
I am using as
SPUtility.GetSPField('Email Address').IsRequired = true;
But it is not validating for mandatory. How can i get out from this issue?
Aug 22, 2013 at 2:31 PM
Hey ymihir,
SPUtility.js doesn't currently have any features for turning on/off validation. I would recommend checking out using something called "PreSaveAction". Here is a pretty good post which outlines how to use it:
http://developing-dummy.blogspot.com/2012/05/inject-javascript-code-to-sharepoint.html
Thanks,
Kit
Aug 22, 2013 at 2:54 PM
Ok..
But in the following url specified that there is a property called IsRequired. thats why i am asking.
// IsRequired is a boolean value for whether or not the field is required.
myField.IsRequired;
Aug 22, 2013 at 4:50 PM
Ah I see where the confusion is. That property will be true if the field is required and false if it is not required. Right now changing it won't do anything... it was meant to be an easier way to tell via code if the field was set to be required in the list's settings.

For example, if the Title field was set to be required in the list settings, then it would have the red asterisk (Title *) and IsRequired would return true.
Sep 6, 2014 at 1:48 AM
FWIW, I'm having the same problem on an SP2013 Enterprise environment for a "Name" field on a folder.

My code:
var nameField = SPUtility.GetSPField('Name');
Error:
Uncaught Error creating field named Name: Unable to retrieve the input control for Name


I've worked around it for now with some simple JS, but just wanted to let you know.

Dave
Jan 20, 2015 at 5:11 PM
Hi,

I'm having a problem using "isrequired" property on the following sharepoint fields: dropdown, person or group, number. That property isn't working. Could you please help me??

Regards,
Jan 21, 2015 at 11:18 PM
fvega,
I will take a look, can you create a new discussion with your issue please?
Thanks,
Kit