SharePoint 2010 Tip: Selectively Showing/Hiding Columns in a SharePoint 2010 list

 
Stephen Velasco's picture

Introduction

I referenced this demo in an earlier demo entitled "Autopopulate List", where I showed how to automatically pull data from our AD to the SharePoint 2010 list. The missing step was that the columns that should be filled in automatically after clicking "OK" were available to the users from the "newform.aspx" and "editform.aspx" which kind of defeated the purpose of "autopopulate".  

 

So, the challenge for today is:

Our current “Autopopulate” form has 4 columns that should autopopulate from AD (Active Directory). How to best prevent these columns from being view/modified by users when filling out the form?The solution presented here is to modify the “newform.aspx” and "editform.aspx". This solution requires a list and the use of some javascript embedded into the forms. There was no cost involved in this solution, and total turnaround time was approximately 2 hours. This solution could be adapted to other situations where users do not need to see all the columns in a list, but all columns need to be available to the “back office”.What you will need

  • A list with columns for modifying the "newform.aspx" and "editform.aspx" pages attached to the list with the needed javascript.
  • 2 hidden Content Editor Webparts (CEWP) to hold the javascript.

Procedure

Create a blank list for the new team site requests on the desired site, and add your columns including those columns which need to be hidden from the users at runtime (in this demo I will be using my "autopopulate" list as the example). Open the “newform.aspx” page attached to this list by clicking on "Default New Form", which is found under "List Tools" - "List" - "Modify Form Webparts" on the ribbon menu.

 

The screen will change to show you an empty view of the "newform.aspx" which is the form that the user will fill out every time they click on "Add new item". You can see the "Business Unit", "Department", etc. columns are still available to the user, and these are the ones we will "hide" so that the workflow can do its autopopulate magic.

 
 

So, click on "Add a Web Part" so that we can add a "Content Editor" web part to the form. This is where our javascript code will go instructing sharepoint to hide the columns of our choice.

Once the Content Editor webpart has been added to the page, it needs to be "hidden" so that it does not show up when the form is presented onscreen for the user. Modify the Content Editor, and choose "Hidden" from the "Layout" menu.

 

Now we get to a point where the code needs to be inserted. BUT, since we are going to repeat this exercise on the "editform.aspx" (the form where a user can edit the information) - we will actually *link* to the javascript code, rather than inserting it directly to each form. In this way, if the code requires any changes, you only have to modify the code in one place and the changes are reflected automatically on all the linked forms (this is actually rather neat, I think).

Open your windows notepad (if you really want to use a proper tool, I can highly recommend notepad++. Get it here), and just copy&paste the following code:

 ___________________

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

<!--

// This is custom functionality that hides selected columns in the a list.

 

_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 hideFields() {

   var control = findacontrol("Business Unit");

   control.parentNode.parentNode.style.display="none";

   control = findacontrol("Department");

   control.parentNode.parentNode.style.display="none";

   control = findacontrol("Phone");

   control.parentNode.parentNode.style.display="none";

   control = findacontrol("E-mail");

   control.parentNode.parentNode.style.display="none";

}

//end custom functionality-->

</script>

_______________________

 

This script goes through all the columns on the list and when it finds those that have been specified, it changes their status to display=”none”. In the script above you can see that the fields that should not be visible to the user are “Business Unit”, “Department”, "Phone", and “E-mail”. The advantage of using this script is that the actual column names can be used, so no extra item IDs need to be found. If more columns need to be hidden then just add an extra

 

control = findacontrol("<!-- column name here between the quote marks -->");

   control.parentNode.parentNode.style.display="none";

 

for each column. After checking that all columns names have been added, save the document and call it "hidecolumns.txt". Close the document and upload it to "Site Assets".

 

Now, go to your hidden content editor webpart and at the top of the edit menu you can find the "Content link" field. Enter the path to the "hidecolumns.txt" you have just uploaded. It will look something like: /sites/[Your site name]/SiteAssets/hidecolumns.txt.

 
 

Click "OK", and then "Stop Editing". Now the "Business  Unit" (and the others specified before) column should be gone from the form when the user clicks on "add new item". Repeat the process above for the "Default Edit Form" (editform.aspx), and you are all done!

 

Conclusion

This demo was to show how to selectively hide columns from the different form attached to a list by using simple javascript within a hidden Content Editor web part. All of the work done was completed by using OOTB sharepoint and, other than the javascript, no custom coding was needed. Sharepoint 2010 made this process easier by allowing direct access to the newform.aspx and editform.aspx, which in MOSS 2007 were only available via SharePoint Designer. 

sorry but I should have mentioned

Stephen Velasco's picture

that the solutioin is only for SP2010 since that's what I have to play with...

Unable to get result

Gæst's picture

Hi! I used this script, however, I'm unable to get the result. Below is the script I used. I'm using SP 2013 (Online). Any help would greatly be appreciated.

Nice Step by Step Article but Not able to get the result

Kushal Kamra's picture

 

Hi,

 

I used the article to hide the fields but didn't got the result. This is the js code I used:

 

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

// This is custom functionality that hides selected columns in the a list.

_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 hideFields() {
   var control = findacontrol("TestId");
   control.parentNode.parentNode.style.display="none";
}

//end custom functionality-->

</script>
 

My List Column name is 'TestId'. I uploaded this to site assets and used the link in CEWB. Also, tested the link, the desired file is opening but the hide effect is not getting implemented.

 

Version of SharePoint is SP2013.

 

Any would help would be great.

compare field visibility on username

Ravnesh Kumar's picture

Hi Stephen

how can we hide column based on userID/logged user.

appreciate your advise. 

I would think so...

Stephen Velasco's picture

... by using, as you write, If/Then statements in the code that would compare a value and then set it. I am not a javascript coder, but a bit of testing with if/then's should put you on the right track. Please note that this would only work for either the view or edit forms, as the fields that need to be compared have to have some data - so this means that your idea could not be implemented on the new form.

Modification

danielP's picture

Hi Stephen,

Great tutorial this worked great, I would like to extend this a bit how can I make it so that I can show a field based on the value of another field like the below:

When adding a new item we hide the "Business Unit" column with your javascript, then if the Title column is not null and if contains the value "TestItem" then unhide or show the "Business Unit" column

Can this be achieved?

Thanks,

 

well...

Stephen Velasco's picture

well, yes.... since i'm the one that wrote about it :) What seems to be the trouble? Which SP platform are you using? what are the steps you've tried?

I have followed through the

Gæst's picture

I have followed through the steps, but haven't been able to hide the colulns from the forms..did you guys implement thus successfully?

 

You are right about the

 Chul-Soo Gert Rim 's picture

You are right about the "hidden" option will remove the column in all forms, I guess I misunderstood the main subject of your article. Sorry about that.

sorry for the late reply,

Stephen Velasco's picture

If I recall correctly using the "hidden (will not appear in forms)" will actually hide the columns in *every* form ("new form", "display form", and "edit form") - so, for this example, the "autopopulate" information would be hidden in the "display" form and, thereby, invisible to everyone. What I want to accomplish is to limit access to specific columns in specific forms, and that cannot be done by the column setting you refer to.

I do use the method you refer to as one way of hiding the, sometimes obtrusive, "title" column from a list as I don't always need to use it and it can't be deleted within "list settings", or when I need to use "calculated columns" and don't want them exposed in the forms.

Edit content type settings...

 Chul-Soo Gert Rim 's picture

Hi Stephen,

I like your tip about using Javascript for manipulating the outcome of SharePoint forms. I have been using the same methods for some time, but I have also used a more "simple" method, which I find easier and requires no code references at all. And it works in MOSS and SP2010:

1. Go to:  List settings->Advanced settings.

2. Select "Allow management of content types".

3. Back in the list settings: Select you content type used in the list...

4. Click on the column name that you would like to hide/show.

5. In the column settings, select the option: "Hidden (Will not appear in forms)". And clik "OK".

I don't know if I am way of here, but it seems like the above configuration steps will do the same as your script.

/Chul-Soo

 

 

I'm glad I can help. Hope

Stephen Velasco's picture

I'm glad I can help. Hope you find them useful :)

you read my mind

Anthony Liberto's picture

exactly what I needed: thank you :)

I am always a little suprised that functionality like this is not native to SP.

Very useful tip Stephan thx

Joanna Maria Lewkowicz's picture

Very useful tip Stephan thx again for sharing. keep them coming  :-)

 

 

Feedback Form