Archive for March, 2012

Creating List Fields/Columns Programmatically in Sharepoint 2010

Here’s the starting point for creating custom fields in a SharePoint list programmatically, there’s of course much more to do in order to make them behave according to you requirements;

 

Single Line Text Field

        string fieldInternalName = list.Fields.Add(“fieldname”, SPFieldType.Text, false);

        list.Fields[fieldInternalName].Description = “field description”;

        list.Fields[fieldInternalName].Update();

 

Multiline Text Field

        string fieldInternalName = list.Fields.Add(“fieldname”, SPFieldType.Note, false);

 

Boolean Field

        string fieldInternalName = list.Fields.Add(“fieldname”, SPFieldType.Boolean, false);

 

Number Field

        string fieldInternalName = list.Fields.Add(“fieldname”, SPFieldType.Number, false);

        SPFieldNumber fieldTimeSpent = new SPFieldNumber(list.Fields, fieldTimeSpentName);

        fieldTimeSpent.DisplayFormat = SPNumberFormatTypes.NoDecimal;

        fieldTimeSpent.Update();

       

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();

       

User Field

        string fieldUserName = list.Fields.Add(“User”, SPFieldType.User, false);

        SPFieldUser userField = new SPFieldUser(list.Fields, fieldUserName);

        userField.AllowMultipleValues = false;

        userField.Required = true;

        userField.SelectionMode = SPFieldUserSelectionMode.PeopleOnly;

        userField.LookupField = “Title”;

        userField.Update();

 

Date Time Field

        string fieldInternalName = list.Fields.Add(“fieldname”, SPFieldType.DateTime, false);

        SPFieldDateTime fieldDateTime = new SPFieldDateTime(list.Fields, fieldInternalName);

        fieldDateTime.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;

        fieldDateTime.DefaultValue = “[today]”;

        fieldDateTime.Update();

 

Lookup Field

        SPList listMain = currentWeb.Lists.TryGetList(“MainList”);

        SPList listActivity = currentWeb.Lists.TryGetList(“Activities”);

 

        string fieldActivityName = listMain.Fields.AddLookup(“Activity”, listActivity.ID, false);

        SPFieldLookup activityField = new SPFieldLookup(listMain.Fields, fieldActivityName);

        activityField.AllowMultipleValues = false;

        activityField.LookupField = “Title”;

        activityField.Indexed = true;

        activityField.RelationshipDeleteBehavior = SPRelationshipDeleteBehavior.Restrict;

        activityField.Update();

 

Lookup Additional Field

        string fieldActivityIdName = listMain.Fields.AddDependentLookup(“Activity:ID”, activityField.Id);

        SPFieldLookup fieldLookup = (SPFieldLookup)listMain.Fields.GetFieldByInternalName(fieldActivityIdName);

        fieldLookup.LookupField = listActivity.Fields[“ID”].InternalName;

        fieldLookup.Update();

 

Modifying Default View

        var view = list.DefaultView;

        view.ViewFields.Delete(“Attachments”);

 

        view.ViewFields.Add(“ID”);

        view.ViewFields.Add(“field1”);

        view.ViewFields.Add(“field2”);

       

        view.ViewFields.MoveFieldTo(“ID”, 0);

        view.ViewFields.MoveFieldTo(“field1”, 2);

        view.ViewFields.MoveFieldTo(“field2”, 3);

        view.Update();

Advertisements

1 Comment