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

Set dropdown field in EditForm

Aug 25, 2015 at 11:44 PM
I have a custom EditForm that launched from a button where I want to set the dropdown value to 'Yes' when the form loads (and once working also hide the field). Once the form is updated and saved, this 'respond' value is then read by the workflow to send a specific email and the workflow then resets the 'respond' value to 'blank'. The script in my form is not working: <script type="text/javascript" src="http://HomeSite/Subsite/Billing/BillingScripts/sputility.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { var myChoiceField = SPUtility.GetSPField('Respond'); var SetChoiceField = function () { myChoiceField.SetValue('Yes'); } }; SetChoiceField(); }); </script> I get the error 'jquery is not defined' I'm not a javascript writer by any stretch of the imagination so any assistance would be most appreciated.
Aug 26, 2015 at 3:15 AM
Edited Aug 26, 2015 at 3:15 AM
Lynn,
Try this code:
<!-- jquery is a required dependency for sputility -->
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="http://HomeSite/Subsite/Billing/BillingScripts/sputility.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(document).ready(function () { 
    var choiceField = SPUtility.GetSPField('Respond'); 
    choiceField.SetValue('Yes');
    // when you're ready, uncomment the next line
    //choiceField.Hide();
});
</script>
From your code, looks like you may be missing jQuery. I've included the line for that and also fixed up your code a bit.

Also, what do you mean by "custom EditForm"? If you've customized the page using SharePoint Designer then SPUtility.js will not work. I recommend using the Script web part on the default Edit Form page or using a Content Editor web part referencing your code.
Thanks,
Kit
Aug 26, 2015 at 7:52 PM
That did the trick! Many thanks! I didn't realize the code wasn't going work if added directly to the form rather than in a separate web part. I went the route of the Content Editor web part and the field is now populating properly.
Aug 31, 2015 at 6:20 PM
Well, I spoke too soon! I have the following in an html file stored in the same subsite library 'KYBillingScripts' as the jquery and sputility files and titled 'SetRespond'. The contents of 'SetRespond' are: <script src="http://HomeSite/Subsite/KYBilling/KYBillingScripts/jquery-1.11.3.min.js"></script> <script type="text/javascript" src="http://HomeSite/Subsite/KYBilling/KYBillingScripts/sputility.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { var myChoiceField = SPUtility.GetSPField('Respond'); myChoiceField.SetValue('Yes'); //choiceField.Hide(); }); </script> I added a Content Editor webpart below the dataformwebpart and added the content link = http://HomeSite/Subsite/KYBilling/KYBilingScripts/SetRespond.html I note that even when I click 'OK' and then 'Stop Editing' the initial part of the url is stripped to leave me with /Subsite/KYBilling/KYBilingScripts/SetRespond.html. Sometimes, when I initially load the page it will set the value to 'Yes' as I want but then it will stop working after that. I can see that it is throwing the error: Exception thrown and not caught followed by 'sputility.min.js, line 10 character 21741' which points to the function. Essentially, the field is not being set at all. Lynn
Sep 8, 2015 at 1:01 AM
Sorry to hear it isn't working still. The URL getting stripped is OK.. SharePoint is converting it to a relative URL automatically.

Your code looks OK. Do you notice any pattern with when it doesn't work? Do you have the web part set up on the NewForm and the EditForm?
Sep 10, 2015 at 5:50 PM
I have the code set up on the EditForm and it is referenced from a Content Editor Webpart just below the DataView part. Today it doesn't seem to be working at all.

I get the error:

SCRIPT5022: Exception thrown and not caught
sputility.min.js, line 10 character 21741
,Y.GetSPField=function(a){
    s();
    var b=S[a];
    if(c(b))throw"Unable to get a SPField named "+a;
    return null===b.spField&&(b.spField=q(b)),b.spField
}
I have checked (yet again) that the list column is called 'Respond' and that is what it is called on the form as well.
Sep 11, 2015 at 11:01 PM
Unfortunately that error message doesn't give me much information as to why it is failing.

Another option worth trying is to try a Script web part instead of a Content Editor web part.

You can take the contents of your "SetRespond" file and just dump the entire thing into the Script web part.