Pages

Sunday, 21 September 2014

Creating Fields or Columns in SharePoint List Programmatically using C# (Single Line Test, Multiline Text, Choice, Number, Currency, Date Time, Lookup, Person or Group)

Today, in this article let's play around with one of the interesting and most useful concepts in SharePoint List

Question:Creating Fields or Columns in SharePoint List Programmatically using C#(Single Line Test, Multiline Text, Choice, Number, Currency, Date Time, Lookup, Person or Group)

It is very easy to add a column in a SharePoint List from website front-end, we just need to click "List Settings -> Create Column" and enter the title and type of the field, and click "OK" button. That's it, new field is created.

In simple terms "When you create a custom list, a new empty list is created with just two columns - Title and Attachments. The list contains a single default view. Once you create the list, you can add more columns, views, and so on"

We can use SharePoint API to create these fields and associate the corresponding attributes programmatically within an existing List. Provided below is a code snippet in C#.Net.

using (SPWeb web = new SPSite("http://kapplesofts/").OpenWeb())
            {
                web.AllowUnsafeUpdates = true;

                SPList list = web.Lists["KapplesSofts List"];

                //Creating List Fields or Columns

                //single Line Text Field
                string SLTInternalName = list.Fields.Add("Single Line Text", SPFieldType.Text, false);
                //'false' means Required field not mandatory
                //'true' means Required field mandatory 

                //Multiline Text Field
                string MLTInternalName = list.Fields.Add("Multiline Text", SPFieldType.Note, false);

                //Choice Field
                string fieldStatusName = list.Fields.Add("Status", SPFieldType.Choice, false);
                SPFieldChoice fieldStatus = (SPFieldChoice)list.Fields.GetFieldByInternalName(fieldStatusName);
                string[] activityTypes = { "Active", "Closed" };
                fieldStatus.Choices.AddRange(activityTypes);
                fieldStatus.DefaultValue = "Active";
                fieldStatus.Update();

                //or Choice Field Dropdown
                SPField fieldCategory = new SPField(list.Fields, "Choice", "Category");
                list.Fields.Add(fieldCategory);
                list.Update();
                SPFieldChoice CategoryField = (SPFieldChoice)list.Fields["Category"];
                CategoryField.EditFormat = SPChoiceFormatType.Dropdown;
                CategoryField.Choices.Add("Non Compliance");
                CategoryField.Choices.Add("Observation");
                CategoryField.Choices.Add("Suggestion");
                CategoryField.Choices.Add("Improvement");
                CategoryField.DefaultValue = "Observation";
                CategoryField.Update();

                //or Choice Field RadioButtons
                SPField field = new SPField(list.Fields, "Choice", "Siverity");
                list.Fields.Add(field);
                list.Update();
                SPFieldChoice rdField = (SPFieldChoice)list.Fields["Siverity"];
                rdField.EditFormat = SPChoiceFormatType.RadioButtons;
                rdField.Choices.Add("Major Finding");
                rdField.Choices.Add("Minor Finding");
                //DefaultValue empty
                rdField.Update();

                //Number Field
                string NumberInternalName = list.Fields.Add("Number", SPFieldType.Number, false);
                SPFieldNumber fieldTimeSpent = new SPFieldNumber(list.Fields, NumberInternalName);
                fieldTimeSpent.DisplayFormat = SPNumberFormatTypes.NoDecimal;
                fieldTimeSpent.Update();

                // Currency field
                string CurrencyInternalName = list.Fields.Add("Salary", SPFieldType.Currency, false);
                SPFieldCurrency currencyTimeSpent = new SPFieldCurrency(list.Fields, CurrencyInternalName);
                currencyTimeSpent.Currency = SPCurrencyFieldFormats.India;
                currencyTimeSpent.DisplayFormat = SPNumberFormatTypes.TwoDecimals;
                currencyTimeSpent.Update();
        

                //Date Time Field
                string fieldInternalName = list.Fields.Add("DOB", SPFieldType.DateTime, false);
                SPFieldDateTime fieldDateTime = new SPFieldDateTime(list.Fields, fieldInternalName);
                fieldDateTime.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
                fieldDateTime.DefaultValue = "[today]";
                fieldDateTime.Update();

                //Lookup Field
                SPField titleField = list.Fields.GetField("Title");
                titleField.Title = "Employee Name";
                titleField.Update();                              
                SPList lookupList = web.Lists["Sample List"];
                list.Fields.AddLookup("Employee FirstName", lookupList.ID, false);
                SPFieldLookup lkp = (SPFieldLookup)list.Fields["Employee FirstName"];
                lkp.LookupField = list.Fields["Employee Name"].InternalName;
                lkp.Update();

                //Boolean Field
                string BooleanInternalName = list.Fields.Add("Boolean", SPFieldType.Boolean, false);

                //Person or Group Field
                string fieldUserName = list.Fields.Add("Employee Name", SPFieldType.User, false);
                SPFieldUser userField = new SPFieldUser(list.Fields, fieldUserName);
                userField.AllowMultipleValues = false;
                userField.Required = false;
                userField.SelectionMode = SPFieldUserSelectionMode.PeopleOnly;
                userField.LookupField = "Title";
                userField.Update();

                list.Update();

                web.AllowUnsafeUpdates = false;
            }


Out Put list new form:

List setting:

1 comment:

  1. how to add newly created column to default content type?

    ReplyDelete