One of the very first limit you hit when you are customizing a SharePoint list is the NewForm, EditForm, DispForm aspx pages’ missing edit functionality. First you want to do it from the UI, without any success then you will probably find some methods on the Internet to customize these forms.
I decided to sum the methods you can use to get rid of some fields or make them read only, etc.
I created a custom list and added a few columns, now my vanilla New Item page looks like this:
1. Using content types
It is possible to customize a list’s content types to hide some columns. Actually this method only needs a few clicks on the UI. Go to the Settings -> List Settings page and click on Advanced settings. Set Yes under the Allow management of content types section and save the settings. After this a new section will appear on the settings page above the Columns area.
Currently the list allows only one content type entitled, Item. Every new item will follow the settings of this content type including workflows, columns, etc. To customize this click on the name of the item, and select a column.
Select Hidden (Will not appear in forms) and click OK. Now the column named Author will not appear on any form including DispForm. This is useful if you need a field for temporary storage, calculation, special formatting or you want to manipulate it using event receivers.
- easy to do in the list’s advanced settings
- adds another content type
- hides it from every Form
2. With SharePoint Designer 2007
Good ol’ SharePoint Designer! I think this is one of the most popular methods because the WYSIWYG method. Personally I am not a big fan of this software, the workflow creation part is great but otherwise it is a bit sloppy. Since the software is free and you can find plenty of articles on this topic it is very easy to implement so I won’t write just another post with screenshots and detailed steps here.
Instead I want to mention a few drawbacks:
In some scenarios you may receive “Invalid page URL:” error message when try to create a new item in the list or you receive “An unexpected error has occurred” if you click an existing item in the list. According to Microsoft the solution is to delete the list and re-create it, which is not funny. (http://support.microsoft.com/?id=935504)
Another very common problem is that you cannot attach a file to a listitem because you get the following error message: “This form was customized not working with attachment” The following article contains the solution: http://support.microsoft.com/kb/953271/
- easy with the WYSIWYG tool
- plenty of resources, tutorials on the web
- have to be done carefully
- attachment problem
- invalid page url/unexpected error can occur
Our goal is to add a JS code to the page and manipulate the form elements based on their names, hierarchy, etc. I will show you how to hide the Year row on the above mentioned NewForm.aspx page.
Then in the source editor add some code.
For this example I use Jquery library and reference it from the Internet. It is also possible to download it to a document library on this site and use it from there just don’t forget to give enough permission for your users.
After this point JQuery selectors and methods can be used.
After the page has been loaded the script searches for all input fields with a title starts with ISBN then gets the third parent element and hides it. In this example it is a tr, a single row of a table. Unfortuantely you have to be familiar with the page’s source code for this method.
You can do almost the same things with JS as usual, the possibilities are endless.
One of the main drawback is that this solution is not secure at all. There are plenty of ways to by-pass this trick so never try to hide important fields from your users this way because it cannot hold up an eager user for more than 2 minutes.
- with this method the page can also be customized
- not a secure way to hide a field
- involves coding
- administrative overhead
4. Writing code
Finally let’s check the Microsoft.SharePoint namespace. Our goal is to hide a row on the NewForm.aspx page. Till now I referenced to these items as columns but to achieve our goal take a look at the SPField class. There are a few very promising properties:
According to the documentation this is exactly what I need! Let’s change these values and see what happens. You can write a console application in visual studio to change the boolean values or you can use powershell. For this example let’s write a powershell code:
[System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") $site = new-object Microsoft.SharePoint.SPSite("http://localhost") $web = $site.AllWebs["letitknow"] $fields = $web.Lists["Library"].Fields $field = $fields["Year"] $field.ShowInNewForm = $false $field.Update()
I tried to make it small, but for future use put it in a function so it’s reusable.
If we check the $field variable this should be the result:
After running the script voila, the Year field is also hidden.
- uses the Microsoft.SharePoint namespace
- clean solution
- can be set individually on the 3 mentioned forms
- involves coding (but with powershell it’s really easy!)
Are you interested in what you can do in SharePoint 2010? In a future article I will show you. Stay tuned.