Monday, May 19, 2008

Adding Fields to List dynamically

Many times we requires to add a field in List. there are many alternative ways to add fields into a list. One of them is to add field dynamically through coding.

Here is a way how you can add field in list.

Assuming that tasks is a list object (SPlist) of your web and you want to add a field called "ApprovalStauts", then following line will add the field in list.

tasks.Fields.Add("ApprovalStauts", SPFieldType.Text, false);

Second parameter determines what type of field it is, i have supplied it ias simple text only that is equals to single line textbox,

Last parameter determines if the field that you are adding is required field or not. Here i supplied false, meaning its not required field.

and then just call tasks.Update() method to take effect. Just make sure that user has enough rights to add fields and also allowunsafeupdate is true on web level of that list.

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