Friday, July 11, 2008

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

Hi All,

This is all about doing something which is very useful and something very interesting stuff. Ever wondered you need only those columns or fields that are defined only by user. When you are trying to get the Fields collection of a list,
what you will get is list of all columns around 52 columns from a list or document library.

One more interesting post available Hiding columns in Sharepoint List in different mode(edit/new/display)

Go for reading on part2 for more stuff of fields in the list in different modes

Some or the other day, you need only those columns or fields that are only defined by user.

Let me give you a scenario:

Scenario 1 :

One time one of our friends come across to a requirement where he needs to update and migrate the data of sharepoint site from one system to other system.

He was having a requirement of running a timer job which runs at night at 12:00 and makes the copy of Sharepoint database to other system's database for the purpose of moving the data and backing up. Those data are used by some other web application then.

so challenge was the data that should go in that system's database was only those which are user created.that is Lets say you have 10 user defined field in the List and the same fields in the database table that can be used by some other system.

so the challenge was to extract only those fields that are User created not the default ones which is return back by sharepoint list fields which are almost 52 fields.

So first we thought of making the View so that we get only those fields that are in view not all, we can always add fields in that. but that will change the View and client will not agree on that change. So making or changing a view is not a solution.
so there must be some other work around for this.

So we though to make a small stub that will return us fields that are user created only not all the fields.

ArrayList strNonHiddenFields = new ArrayList();

SPWeb objWeb = SPContext.Current.Web;
SPList objList = objWeb.Lists["Source List"];
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)
{
strNonHiddenFields.Add(objList.Fields[Cnt].Title);
}
}

You must be thinking, why only hidden property does not work in this. why you need the combination of three different properties for this. Let me explain. By checking hidden only will not give you the perfect result as
Created,CreateBy,Modified,ModifiedBy and many more are not hidden, but these fields are also not part of Newform , Editform etc. So??? this alone will surely not work. But yes these fields are read only. so by adding one more attribute to the condition will still reduce returning columns. Now what about the last condition. Its about attachment,Attachment is not a field that we want, so it will also come in that list which we do not want. Its all different ball game. So we are checking type of the field.

one of the columns from the fieldcollection returned by list is Attachments and if you check the type of that field, it will return as Attachments, so here we are checking that and also excluding it.

And there you go, you have final list of only those columns that are user defined.

Isn't it great.!!!!

Wondering why its Part 1 ...well soon i am going to post a part-2 where i needed to enhance this soltion to other extent as i am going to share it with you all soon....

7 comments:

Piercy said...

I would like to thank you. This code has really helped me. Works perfectly.

Thanks again,
Piercy

Anonymous said...

Thanks alot dear, really helped me.

Anonymous said...

Thanks !!!! Really helped me, very useful information

Anonymous said...

thanks,
this just worked fine.
I need to know how to apply same technique and collect columns specific to a view that I have created

Qazi Arfeen said...

thanks,
this just worked fine.
I need to know how to apply same technique and collect columns specific to a view that I have created

Parth Patel said...

Qazi Arfeen,

similar code will apply for the view also but for that you have to
go through spsite-->spweb-->List-->view-->field.

other field property of field will be the same.

Anonymous said...

Thanks !!!! Really helped me, very useful information.
Shree.




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 sharepointkings@gmail.com 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 sharepointkings@gmail.com