Wednesday, November 21, 2012

Hiding certain fields from document properties using jQuery

I have a document library that uses a few "hidden" fields during its various automated workflows.  However, for some of these fields, I can't mark them as hidden fields in the document library properties, possibly because they're related to a content type.  So I need to find another way to hide them from the user when viewing or editing document properties.

I found a solution on the blog SharePoint Sherpa - SharePoint 2007 – Hiding fields on NewForm.aspx and EditForm.aspx, the easy way - but wanted to use jQuery, so adapted it slightly.  Here is what I came up with.

1. Find the document library's DispForm.aspx and/or EditForm.aspx in the hidden Forms folder. This can be found in SharePoint Designer, selecting All Files for that site, and finding the Forms folder in the correct library.
2. Find the line:
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
3. Add the following code:


<script type="text/javascript">

_spBodyOnLoadFunctionNames.push("hideFields");

function hideFields() {

$('.ms-formlabel:contains("Field name 1")').parent().hide();
$('.ms-formlabel:contains("Field name 2")').parent().hide();
$('.ms-formlabel:contains("Field name 3")').parent().hide();
$('.ms-formlabel:contains("Document ID")').parent().hide();

}
</script>

4. Change "Field name 1" to the name of the first field you want to hide.  Do the same for the other lines, and add more lines as required.  (I've chosen to hide Document ID - it's in use throughout our SharePoint site, but I don't want the users to see it in this library)

I'm sure there are ways to further improve this, perhaps by putting the field names into an array, but this will do for now.

No comments:

Post a Comment