Peterson Yap

Peterson Yap

  • NA
  • 35
  • 66.9k

Programmatically Add SharePoint Columns and Formatting theses Column's properties

Sep 14 2010 1:34 AM
Hello everyone, i'm new here! =)

I need some help to create Sharepoint columns and setting up the column's format or properties programmatically.

I am able to create columns programmatically already, but am not able to set where i want radio buttons, dropdownlist for eg. Choice Type column. Any one can give me a suggestion or example?


This is what i used for creating columns:

 private void btnCreateColumn_Click(object sender, EventArgs e)
        {
            SPSite mainsite = new SPSite("http://moss:3602");
            SPWeb web = mainsite.AllWebs[0];
            string ListSelected;
            ListSelected = cmbLstNames.SelectedItem.ToString();
            SPList taskList = web.Lists[ListSelected];
            SPFieldCollection Fields = taskList.Fields;
            SPFieldType UserChosenFieldType = SPFieldType.Text;
            string FieldType = "";
            FieldType = cmbListType.SelectedItem.ToString();
            try
            {
                #region SPField Types Definition
                /*UserChosenFieldType is optional instead you can use any one of the case UserChosenFieldType such as "AllDayEven" to create only one type of Site Template without Option*/

                switch (FieldType)
                {
                    case "AllDayEvent": UserChosenFieldType = SPFieldType.AllDayEvent; break;
                    case "Attachments": UserChosenFieldType = SPFieldType.Attachments; break;
                    case "Boolean": UserChosenFieldType = SPFieldType.Boolean; break;
                    case "Calculated": UserChosenFieldType = SPFieldType.Calculated; break;
                    case "Choice": UserChosenFieldType = SPFieldType.Choice; break;
                    case "Computed": UserChosenFieldType = SPFieldType.Computed; break;
                    case "ContentTypeId": UserChosenFieldType = SPFieldType.ContentTypeId; break;
                    case "Counter": UserChosenFieldType = SPFieldType.Counter; break;
                    case "CrossProjectLink": UserChosenFieldType = SPFieldType.CrossProjectLink; break;
                    case "Currency": UserChosenFieldType = SPFieldType.Currency; break;
                    case "DateTime": UserChosenFieldType = SPFieldType.DateTime; break;
                    case "Error": UserChosenFieldType = SPFieldType.Error; break;
                    case "File": UserChosenFieldType = SPFieldType.File; break;
                    case "GridChoice": UserChosenFieldType = SPFieldType.GridChoice; break;
                    case "Guid": UserChosenFieldType = SPFieldType.Guid; break;
                    case "Integer": UserChosenFieldType = SPFieldType.Integer; break;
                    case "Invalid": UserChosenFieldType = SPFieldType.Invalid; break;
                    case "Lookup": UserChosenFieldType = SPFieldType.Lookup; break;
                    case "MaxItems": UserChosenFieldType = SPFieldType.MaxItems; break;
                    case "ModStat": UserChosenFieldType = SPFieldType.ModStat; break;
                    case "MultiChoice": UserChosenFieldType = SPFieldType.MultiChoice; break;
                    case "Note": UserChosenFieldType = SPFieldType.Note; break;
                    case "Number": UserChosenFieldType = SPFieldType.Number; break;
                    case "PageSeparator": UserChosenFieldType = SPFieldType.PageSeparator; break;
                    case "Recurrence": UserChosenFieldType = SPFieldType.Recurrence; break;
                    case "Text": UserChosenFieldType = SPFieldType.Text; break;
                    case "ThreadIndex": UserChosenFieldType = SPFieldType.ThreadIndex; break;
                    case "Threading": UserChosenFieldType = SPFieldType.Threading; break;
                    case "URL": UserChosenFieldType = SPFieldType.URL; break;
                    case "User": UserChosenFieldType = SPFieldType.User; break;
                    case "WorkflowEventType": UserChosenFieldType = SPFieldType.WorkflowEventType; break;
                    case "WorkflowStatus": UserChosenFieldType = SPFieldType.WorkflowStatus; break;

                }
                #endregion
        
                if (rdbtnYes.Checked)
                    Fields.Add(txtColumnName.Text, UserChosenFieldType, true);

                else
                    Fields.Add(txtColumnName.Text, UserChosenFieldType, false);
                MessageBox.Show("Custom field created dynamically");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error encountered attempting to add a new field" + Environment.NewLine + ex.Message);
            }
        }
      
        private void loadColumnTypes()
        {
            cmbListType.Items.Add("AllDayEvent");
            cmbListType.Items.Add("Attachments");
            cmbListType.Items.Add("Boolean");
            cmbListType.Items.Add("Calculated");
            cmbListType.Items.Add("Choice");
        }
        #endregion


Answers (6)