This project has moved. For the latest updates, please go here.

Hide form in SP 2013

Jun 13, 2014 at 1:32 PM
I have field:
<SharePoint:FormField runat="server" id="ff1{$Pos}" ControlMode="New" FieldName="Title" __designer:bind="{ddwrt:DataBind('i',concat('ff1',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Title')}"/>
And want hide another when FieldName="Title" is empty.
<SharePoint:FormField runat="server" id="ff3{$Pos}" ControlMode="New" FieldName="Miasto" __designer:bind="{ddwrt:DataBind('i',concat('ff3',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Miasto')}"/>
I used code from another discussion and past it after <asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">.
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="../../javascript/sputility.min.js"></script>
<script>
function OnCountryFieldDropdownChange(spfield) {
    var countryFieldValue = spfield.GetValue(); 
    // Hide the City field if the selected value is Other 
    if(countryFieldValue === 'Polska') { 
        SPUtility.GetSPField('Miasto').Hide(); 
    } else{ 
        SPUtility.GetSPField('Miasto').Show(); 
    } 
}

$(document).ready(function(){ 
    // Get a single select dropdown field 
    var countryField = SPUtility.GetSPField('Title');
    
    // whenever the field's dropdown changes, check whether we need to show or hide
    $(countryField.Dropdown).on('change', function() { 
        OnCountryFieldDropdownChange(countryField);
    });
    
    // run it once when the form loads to show/hide the field
    OnCountryFieldDropdownChange(countryField);
}); 
 </script>
But it dont work, can i someone help?
Coordinator
Jun 22, 2014 at 7:06 PM
Karol,
Seems like you are creating a custom form in SharePoint designer? Unfortunately, SPUtility won't work with custom forms. It will only work with the out of the box SharePoint forms.
Thanks,
Kit
Jun 23, 2014 at 8:56 AM
Hello KitMenke,
did you mean the script will not work if we added it in the list forms with SharePoint designer? I had the same issue as Karol, when I tried to added the script to the list forms with designer, it didn't work. And I also tried to added the customized code with the content editor web part in the list forms, but it cannot be saved due to some must filled columns defined in the list before. How can we handle this? Could you please give us some ideas? Thanks in advance.
Jun 23, 2014 at 10:37 AM
Yep, I made new form (add form) in designer because in this way is easier for make own graphic than in infopath. Like jhyshuixin I prefers add code at page, when I edit form and so i have to edit source.
Too bad it does not work, its lovley and simple but very useful script. Keey finger for update :)
Coordinator
Jun 23, 2014 at 11:20 PM
Edited Jun 23, 2014 at 11:21 PM
If you can use the Content Editor Web Part that is ideal because you can just add it on the same page along with the form.

To get around the required field problem, you can try just putting a value in the field (to make the validation happy) and then "stop editing". It should save your page then without actually creating/editing an item.

For the graphics, you can also put that in a CEWP at the top/bottom of your form rather than use SharePoint Designer. However, I understand that it doesn't give you as much control as SPD does. You can still use JavaScript on your form using jQuery but SPUtility won't work because it is coded to work with the default forms. :)