Create List in SharePoint 2010 Programmatically Using VS 2010 Via COM

Introduction

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

Question: What is creating a list via COM?

In simple terms "To create a new list template using the client object model".
 

asp.net-web-application.jpg


Step 1: Open Visual Studio 2010 and select web forms application

 

Now two references need to be added to the application, they are:

  • Microsoft.SharePoint.Client

  • Microsoft.SharePoint.Client.Runtime

Step 2: The complete code of webform1.aspx looks like this:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CreateListCOMApp._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <center>

        <div>

            <table style="text-align: center">

                <tr>

                    <td colspan="2">

                        <asp:Label ID="Label1" runat="server" Text="Client Object Model List Creation - SharePoint 2010 Programatically"

                            Font-Bold="true" Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label4" runat="server" Text="Please Enter List Name" Font-Size="Large"

                            Font-Names="Verdana" Font-Italic="true"></asp:Label>

                    </td>

                    <td>

                        <asp:TextBox ID="TextBox4" runat="server" Width="117px"></asp:TextBox>

                    </td>

                </tr>

                <tr>

                    <td colspan="2">

                        <br />

                        <asp:Button ID="Button1" runat="server" Text="Create List" Font-Names="Verdana" Width="282px"

                            BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />

                    </td>

                </tr>

                <tr>

                    <td colspan="2">

                        <br />

                        <asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana"></asp:Label>

                    </td>

                </tr>

            </table>

        </div>

    </center>

    </form>

</body>

</html>

 

Step 3: The complete code of webform1.aspx.cs looks like this:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Microsoft.SharePoint.Client;

namespace CreateListCOMApp

{

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            TextBox4.Focus();

        }

        protected void Button1_Click(object sender, EventArgs e)

        {

            if (string.IsNullOrEmpty(TextBox4.Text))

            {

                Label5.Text = "Please Enter Some Values";

                Label5.ForeColor = System.Drawing.Color.Red;

            }

            else

            {

                ClientContext objContext = new ClientContext("http://win-kv3bo1rqqf7:25339/");

                Web objWeb = objContext.Web; ListCreationInformation createInfo = new ListCreationInformation();

                createInfo.Title = TextBox4.Text;

                createInfo.TemplateType = (int)ListTemplateType.CustomGrid;

                List objList = objWeb.Lists.Add(createInfo);

                objList.OnQuickLaunch = true; objList.Update(); objContext.ExecuteQuery();

                Label5.Text = "List Created Successfully";

                Label5.ForeColor = System.Drawing.Color.Green;

                TextBox4.Text = string.Empty;

            }

        }

    }

}

 

Step 4: The output of the application looks like this:

 
client-object-model-list-sharepoint2010.jpg
 

Step 5: Data entering output of the application looks like this:

 

creation-client-object-model-list-sharepoint2010.jpg
 

Step 6: The output of the field created SharePoint application looks like this:

 

output-field-created-sharepoint-application.jpg

I hope this article is useful for you.