Thursday, August 28, 2008

How to Add Multi Line Text Box Field Programmatically in MOSS?

Hi,

I found very interesting stuff today. I was in a need to add a Multi Line Text through code.
I started adding it with calling the Fields.Add Method of list object and passing the parameter, but as soon as i started adding the Field, i found that there is no direct way to add it.

I figured it out and come to know that we have to use the XML of the field that we want to add.

XML of the field that we want to add is very simple. all you need to do is just take the list definition with the help of solution generator tool, open the schema.xml of that list and check for the field. here you will get the idea that how can field be added in the list. we need to use the same structure of the field and add it for the code.

Means simply try to add multi line text as a field in the list, take schema.xml and check that field inside schema, you will find one tag of <Field ..../> for the same field. that is it we only need to programmatically set it.

so here is the approach that i took,

String strXMLOfField = "<Field Type='Note' DisplayName='School Name' Required='FALSE' NumLines='20' RichText='FALSE' ";

strXMLOfField += " StaticName='School_Name' Name='School_Name'/>";

SPList objList = Web.Lists["<your_List>"];

Web.AllowUnsafeUpdates = true;

objList.Fields.AddFieldAsXml(strXMLOfField, false, SPAddFieldOptions.Default);

objList.Update();

Web.AllowUnsafeUpdates = false;


Now as you can see in XML, we have set every possible properties in that and you will also see the same applied in UI if you open that Field once it gets added in the list.

but..but.. Here is one problem. even you have set all properties right then also Internal Name and Static Name just does not make sense here. It should be School_Name as internal name and School Name as Display Name. What happened is School Name actually works fine but internal name will not come as School_Name which actually should come.

If you consider schema.xml of the list and deploy that list as a feature everything just works fine, but same XML if you add with the help of code, it does not work fine!!!!!!!!!!!!!

so i found the way that you need to tweak the Code. so here is the tweaking part of it.

After Adding the field that its returning string id.

so Modify the XML,

String strXMLOfField = "<Field Type='Note' DisplayName='School_Name' Required='FALSE' NumLines='20' RichText='FALSE' ";

//AT the time of adding Field through code, It actually makes the Static Name in no matter what you have given, it will add space in that which you do not want. so keep StaticName, Display Name, and Name same.

strXMLOfField += " StaticName='School_Name' Name='School_Name'/>";

string strFieldID = objList.Fields.AddFieldAsXml(strXMLOfField, false, SPAddFieldOptions.Default);

SPField objfield = objList.Fields[strFieldID];

//Now update the Display Name

objfield.Title = "School Name";
objfield.Update();

Here is a complete code to use.


String strXMLOfField = "<Field Type='Note' DisplayName='School_Name' Required='FALSE' NumLines='20' RichText='FALSE' ";

strXMLOfField += " StaticName='School_Name' Name='School_Name'/>";

Web.AllowUnsafeUpdates = true;

string strFieldID = objList.Fields.AddFieldAsXml(strXMLOfField, false, SPAddFieldOptions.Default);

SPField objfield = objList.Fields[strFieldID];

objfield.Title = "School Name";
objfield.Update();

Web.AllowUnsafeUpdates = false;


so, There you go.. now you have finally added the multi line text field programmatically.

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 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