Sunday, 2 March 2014

Sharepoint 2010 add columns to list programmatically

using System;
using Microsoft.SharePoint.Client;

class Program
{
    static void Main()
    {
        ClientContext clientContext =
            new ClientContext("http://intranet.contoso.com");
        Web site = clientContext.Web;

        // Create a list.
        ListCreationInformation listCreationInfo =
            new ListCreationInformation();
        listCreationInfo.Title = "Client API Test List";
        listCreationInfo.TemplateType = (int)ListTemplateType.GenericList;
        List list = site.Lists.Add(listCreationInfo);

        // Add fields to the list.
        Field field1 = list.Fields.AddFieldAsXml(
            @"<Field Type='Choice'
                     DisplayName='Category'
                     Format='Dropdown'>
                <Default>Specification</Default>
                <CHOICES>
                  <CHOICE>Specification</CHOICE>
                  <CHOICE>Development</CHOICE>
                  <CHOICE>Test</CHOICE>
                  <CHOICE>Documentation</CHOICE>
                </CHOICES>
              </Field>",
            true, AddFieldOptions.DefaultValue);
        Field field2 = list.Fields.AddFieldAsXml(
            @"<Field Type='Number'
                     DisplayName='Estimate'/>",
            true, AddFieldOptions.DefaultValue);

        // Add some data.
        ListItemCreationInformation itemCreateInfo =
            new ListItemCreationInformation();
        ListItem listItem = list.AddItem(itemCreateInfo);
        listItem["Title"] = "Write specs for user interface.";
        listItem["Category"] = "Specification";
        listItem["Estimate"] = "20";
        listItem.Update();

        listItem = list.AddItem(itemCreateInfo);
        listItem["Title"] = "Develop proof-of-concept.";
        listItem["Category"] = "Development";
        listItem["Estimate"] = "42";
        listItem.Update();

        listItem = list.AddItem(itemCreateInfo);
        listItem["Title"] = "Write test plan for user interface.";
        listItem["Category"] = "Test";
        listItem["Estimate"] = "16";
        listItem.Update();

        listItem = list.AddItem(itemCreateInfo);
        listItem["Title"] = "Validate SharePoint interaction.";
        listItem["Category"] = "Test";
        listItem["Estimate"] = "18";
        listItem.Update();

        listItem = list.AddItem(itemCreateInfo);
        listItem["Title"] = "Develop user interface.";
        listItem["Category"] = "Development";
        listItem["Estimate"] = "18";
        listItem.Update();

        clientContext.ExecuteQuery();
    }
}
----------------------------------------------------------------
Or use below code
----------------------------------------------------------------
SPSite objSite;
SPWeb objWeb;
bool isTemplateExists = false;
string strList = “ListName”;
objSite = new SPSite(@”SiteUrl”);
objWeb = objSite.OpenWeb();
//check Whether list already exist or not.
if (IsListExists(objWeb, strList) == false)
{
CreateNewList(strList);
}

public bool IsListExists(SPWeb pWeb, string pListName)
{
try
{
SPList list = pWeb.Lists[pListName];
if (list == null)
return false;
else
return true;
}
catch (Exception ex)
{
return false;
}
}

public void CreateNewList(string pListName)
{
try
{
objWeb.AllowUnsafeUpdates = true;
SPListCollection lists = objWeb.Lists;

// create new Generic list called ListName
lists.Add(“ListName”, “List Description”, SPListTemplateType.GenericList);
SPList list = objWeb.Lists["ListName"];
// create Text type new column called “Title”
SPFieldText fldTitle = (SPFieldText)list.Fields.CreateNewField(
SPFieldType.Text.ToString(), ” Title “);
fldTitle.Required = false;
// create DateTime type new column called “DateTime”
SPFieldDateTime fldDateTime = (SPFieldDateTime)list.Fields.CreateNewField(
SPFieldType.DateTime.ToString(), “DateTime”);
fldDateTime.DisplayFormat = SPDateTimeFieldFormatType.DateTime;

/* add the new fields to the list */
list.Fields.Add(fldTitle);
list.Fields.Add(fldDateTime);

/* finally update list */
list.Update();

/*add the new fields to view */
SPView view = list.DefaultView;
view.ViewFields.Add(“Title”);
view.ViewFields.Add(“DateTime”);
view.Update();

objWeb.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
}

}