Add custom field type to sharepoint list and set CustomProperty using c#

Object: To add custom column type in sharepoint list and set property (Say: MyProperty1) of custom column type.
Solution: We can add column easily using c# but there is no direct way to set custom property of custom column type, Because SetCustomProperty is not working. Also, there is no error if we use it.

See following example to add a column (CustomFieldType1 type) with property “MyProperty1”. I used AddFieldAsXml method and column name & property value are assigned from the controls.

string pageurl = @"http://server/site/Forms/AllItems.aspx";
string lstguid = "{fe4b12b6-1032-409d-84a3-63647cf7b770}";
using (SPWeb oWebsite = new SPSite(pageurl).OpenWeb())
SPList oList = oWebsite.Lists[new Guid(lstguid)];
if (!oList.Fields.ContainsField(txt.Text))
string strxml = "<Field Type=\"CustomFieldType1\" DisplayName=\""+ txt.Text +"\" Name=\""+ txt.Text +"\"><Customization><ArrayOfProperty><Property><Name>MyProperty1</Name><Value xmlns:q1=\"\" p4:type=\"q1:string\" xmlns:p4=\"\">"+ ddl.SelectedValue.ToString() +"</Value></Property></ArrayOfProperty></Customization></Field>";
oWebsite.Site.WebApplication.FormDigestSettings.Enabled = false;
oList.Fields.AddFieldAsXml(strxml, true, SPAddFieldOptions.Default);
oWebsite.Site.WebApplication.FormDigestSettings.Enabled = true;


No comments:

Post a Comment