Monday, July 14, 2008

How to get user created columns from a sharepoint list - Part 2

Hi All,

This is again regarding that earlier post of sharepoint user defined columns only. Lets continue the same scenario,but with different challenge.

In first post, we discussed about the getting only user defined columns and get rid of all the other columns returned by the sharepoint list.

Read Part-1 of the series

But the now there is some requirements like updating and making the batch process for all the user defined columns even if some columns are not visible in edit mode and visible in new mode, then we need to pickup only those columns
which are visible in New but not visible in Edit OR sometime visible in Edit and not visible in New.

Before getting in to this scenario, let's dive in to understanding of fields behavious for sharepoint list/ document Library.

When you create user defined columns in Sharepoint List and when you try to access it as fieldCollection and when you try to add record or even modify the record, all fields are visible. But if you access it by code and try to
check the ShowInNew,ShowInEdit property of each user defined field, it will show you 'null'.

Now understand that before you apply these properties to the fields, these all fields are null by default. Only after applying the attribute makes them change.

That means first by default null and then when you set the properties, it will set
it is. for example if i add the field called Country by default ShowInNew, ShowInEdit all will be null, but later if i decide that we want only those fields that are having visible in NewForm only OR visible in EdirForm only then
this is the approach to go for this. so if i set that Field.ShowInEdit = false Or ShowInNew = false (Where Field is Country) then if you see the ShowInEdit OR ShowInNew Property it will return you false, and then if you set it to true, it will return you true.

So bottom line is First by default for all user defined columns ShowInEdit, ShowInNew etc will return null by default,and then according to the properties set they will return false /true depending upon properties being set on them.

Here is a code to get all user defined fields (as shown in part-1, this is extension to it) plus only those fields that are having properties ShowInNewform and ShowInEditform set. So it return all the fields that are
visible in Newform only and/or visible in Editform only.

Example also demonstrate for ViewForm,DisplayForms as well.

SPWeb objWeb = SPContext.Current.Web;
SPList objList = objWeb.Lists["Source List"];

ArrayList strListFields = new ArrayList();
ArrayList strDisplayFormFields = new ArrayList();
ArrayList strEditFormFields = new ArrayList();
ArrayList strNewFormFields = new ArrayList();
ArrayList strViewFormFields = new ArrayList();

for (int Cnt = 0; Cnt <= objList.Fields.Count - 1; Cnt++)
if (!objList.Fields[Cnt].Hidden && !objList.Fields[Cnt].ReadOnlyField &&
objList.Fields[Cnt].Type != SPFieldType.Attachments)

if (objList.Fields[Cnt].ShowInDisplayForm == true )

if (objList.Fields[Cnt].ShowInEditForm == true)

if (objList.Fields[Cnt].ShowInViewForms == true)

if (objList.Fields[Cnt].ShowInNewForm == true)



Soon i am going to come up with one more utility that will be part-3 of the series.

No comments:

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to