0

I want to hide 2 fields on DispForm.aspx of a custom template.

Following are the lines of code written for customizing DispForm.aspx using ListFieldIterator SP control.

<SharePoint:ListFieldIterator runat="server" ExcludeFields="PublishingStartDate">
                                        <CustomTemplate>
                                            <tr>
                                                <td class="fp-ms-vb2">
                                                    <SharePoint:FormField ControlMode="Display" DisableInputFieldLabel="true" runat="server">
                                                </td>
                                            </tr>
                                            <tr style="height:5px;"><td></td></tr>
                                        </CustomTemplate>
                                    </SharePoint:ListFieldIterator>

those 2 fields are also generated using ListFieldIterator control.

Can someone suggest me how to hide specific fields.

1

i created a scriptblock which allows you to hide specific fields based on its internal fieldnames. You simply call the function "hideField" with the internal name of the field you want to hide. In my case AssignedTo and DueDate. The fields are still accessible from code behind but are not visible for the user.

<script language="javascript" type="text/javascript">

_spBodyOnLoadFunctionNames.push("hideFields");

function findacontrol(FieldName) {

    var arr = document.getElementsByTagName("!");
    // get all comments
    for (var i = 0; i < arr.length; i++) {
        // now match the field name
        if (arr[i].innerHTML.indexOf(FieldName) > 0)
        { return arr[i]; }
    }
}
function hideField(fieldName) {
    var control = findacontrol(fieldName);
    if (control) {
        var parent1 = control.parentNode;
        if (parent1) {
            var parent2 = parent1.parentNode;
            if (parent2) {
                parent2.style.display = "none";
            }
        }
    }
}

function hideFields() {
    hideField("AssignedTo");
    hideField("DueDate");
}
</script>

I use this on Editform's but it should work on DisplayForms too. Both use the ListFieldIterator control.

0

It looks like that you want to hide this part for specific users. If you want to hide it for non priviliged users you could do something like this:

<!-- Don't render if user has no rights -->
<Sharepoint:SPSecurityTrimmedControl ID="SPSecurityTrimmedControl1" runat="server" PermissionsString="AddAndCustomizePages">
<SharePoint:ListFieldIterator runat="server" ExcludeFields="PublishingStartDate">
                                        <CustomTemplate>
                                            <tr>
                                                <td class="fp-ms-vb2">
                                                    <SharePoint:FormField ControlMode="Display" DisableInputFieldLabel="true" runat="server">
                                                </td>
                                            </tr>
                                            <tr style="height:5px;"><td></td></tr>
                                        </CustomTemplate>
                                    </SharePoint:ListFieldIterator>
</Sharepoint:SPSecurityTrimmedControl>  

Another solution would be to use CSS to hide your elements in general. Notice "!important" is not best practice ;) In order to find out the #IDFORYOURELEMENT hit F12 in Chrome and search for it.

    <style type="text/css">
       #IDFORYOURELEMENT 
       {
            display: none !important;
       }
    </style>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.