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

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 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".
.png)
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.
.png)
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.
.png)
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.
.png)
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.
.png)
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.
.png)
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!
.png)
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.
- Log ind eller opret gratis konto for at skrive kommentarer





You are right about the
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,
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...
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
I'm glad I can help. Hope you find them useful :)
you read my mind
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
Very useful tip Stephan thx again for sharing. keep them coming :-)