Programmatically adding a List with custom fields

Programmatically adding a List with custom fields

  • Comments 9

When a list is created programmatically using a custom template, the custom fields do not get displayed in the UI. These fields do not become a part of the displayed fields of the default view.

 

The Problem - A common task like uploading a file to a list that was created programmatically using a custom template.

When you try creating the list using the Object Model, you would most likely get a “Exception Occurred’ error. But still the list would get created but without the custom fields.

On trouble shooting you would find that the while picking up the custom template the schemaXML for the template was not being populated. Theoretically, the schemaXML should have contained the definition for the custom fields. However the schemaXML property would throw a SPException.

This is why the code was throwing the error “Exception occurred”, and even though the list was being created the fields were not being created. The resolution in this case is to modify the schemaXML property to ensure that the fields are created.

The first line of attack is to modify the schemaXML property through string manipulations to get the property set and the list getting rendered properly. This is possible for the SPList object but not for the SPView object (schemaXML is readonly for SPView). By doing this, I was able to get the fields added, but they were still not being displayed.

Modifying the schemaXML to include “Hidden=’false’” or for that matter “Hidden=’FALSE’” did not help.

The solution that did work was as follows

-          create a new list using a blank template

-          programmatically add the custom fields to the new list

-          Update the list to commit the addition of the new fields

-          Create a new view based on the default “All Items” view

-          Add the same custom fields to the new view

-          Delete the original view

-          Make the custom view as the new default view

 

Code Snippet

 

SPSite site = new SPSite("http://localhost/sites/Sergey/default.aspx");

       SPWeb web = site.OpenWeb();

 

       SPListCollection coll = web.Lists;

       SPListTemplateCollection tmplcoll = web.ListTemplates;

 

       SPFieldCollection fieldcoll;

       SPListTemplate temp = tmplcoll[0];

       Guid gd = coll.Add("NewList9","NewList9",temp);

       coll[gd].Fields.Add("Test1",SPFieldType.Text,false);

       coll[gd].Fields.Add("Test2",SPFieldType.Text,false);

       coll[gd].Update();

                                               

       string defaultquery = coll[gd].Views[0].Query;

       SPViewCollection viewcoll = coll[gd].Views;

       Guid anothergd = coll[gd].Views[0].ID;

       viewcoll.Delete(anothergd);

                                                

       System.Collections.Specialized.StringCollection viewfields = new System.Collections.Specialized.StringCollection();

       viewfields.Add("Title");

       viewfields.Add("Test1");

       viewfields.Add("Test2");

                                               

       coll[gd].Views.Add("All Items",viewfields,defaultquery,100,true,true);

       coll[gd].Update();

 

 

/Harsh

Leave a Comment
  • Please add 1 and 3 and type the answer here:
  • Post
  • I tried to use the code for portal. Everything works fine, but when it comes to My Site, it throws exception "The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again."

    In the url, I used http://[server]/personal/[username]

    Any ideas?

    Thank you in advance!
  • I tried to use the code for portal. Everything works fine, but when it comes to My Site, it throws exception "The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again."

    In the url, I used http://[server]/personal/[username]

    Any ideas?

    Thank you in advance!
  • I tried to use the code for portal. Everything works fine, but when it comes to My Site, it throws exception "The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again."

    In the url, I used http://[server]/personal/[username]

    Any ideas?
  • Did you set allowunsafeupdate to true?
  • Hey james,
    let me check back on that!
  • Thanks for the code - works perfectly. How do I make the Title field link to the item?

  • Hi All

    I am creating a custom field type for sharepoint list.

    I created the custom field by inheriting the sharepoint base field SPFieldMultiLineText.  I‘ve also created a custom field property

    “SearchPath” as follows…

    private string _searchPath

           public string SearchPath

           {

               get

               {

                   return _ searchPath;

               }

               set

               {

                   this._ searchPath = value;

               }

           }

    for rendering custom property I have created an .ascx file along with a code-behind inherited from User Control and  IFieldEditor.

    I could able to access and modify the custom property from within the  methods InitializeWithField()  and OnSaveChange().

    I have created another .ascx file and a code-behind file as Field rendering Usercontrol for providing UI while adding data into the list. I want to implement some logic during this data entry based on the custom field’s additional property value.

    But my problem is I could not access the custom property of my custom field class from within the Field Rendering User control.

    How I can access the custom field’s custom property values within the field rendering usercontrol?

    Please help me.

    Arun

  • I have created custom list programmically with fields. it running successfully on my local server. But when i run it on  remote server it is throwing error. It is creating list but not the fields .. !!!!!!!!!!!!!!!!!

  • I have created custom list programmically with fields. it running successfully on my local server. When i upload it on remote server.it is creating custom list first time.and second time when i delete list and create it back programmically. it is throwing error.. below is the code i have implemented..

    oWeb = SPControl.GetContextWeb(Context);

    string docLib = "List name";

    Guid docguid = oWeb.Lists.Add(docLib,"", SPListTemplateType.GenericList);

    SPList olist1 = oWeb.Lists[docguid];

    olist1.EnableVersioning = true;

    olist1.OnQuickLaunch = true;

    olist1.ReadSecurity = 1;

    olist1.WriteSecurity = 2;

    olist1.Update();

    SPFieldCollection oFiledCollection = olist1.Fields;

    string field1 = olist1.Fields.AddFieldAsXml("<Field Type=\"Note\" DisplayName=\"Application\" Name=\"Application\" Required=\"FALSE\" />");

    SPField ofield1 = oFiledCollection.GetField(field1);

    SPView oView = olist1.DefaultView;

                       SPViewFieldCollection oViewFieldCollection = oView.ViewFields;

    oViewFieldCollection.Add(ofield1);

    oView.Update();

    this it what i implement !!!!!!!!!!!!

    so can u track it down what would error !!!!!

Page 1 of 1 (9 items)