ARTICLE

Client OM Insert in SharePoint 2010 Programmatically Using VS 2012

Posted by Vijay Prativadi Articles | SharePoint 2010 August 29, 2012
Today, in this article let’s play around with one of the interesting and most useful concepts in SharePoint 2010.
Reader Level:


Introduction

 

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

 

Question: What is ClientOMInsert?

 

In simple terms "To insert the data from client side environment to SharePoint applications, this is used".

 

Step 1: Open Visual Studio 2012 and select "ASP.Net Web Forms Application", as in:

 

OpenASPNETSharepoint2010.jpg

 

Now two references are needed to add 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="WebForm1.aspx.cs" Inherits="ClientOMInsert.WebForm1" %>

 

<!DOCTYPE html>

<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 Insertion - 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>

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

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

                    </td>

                    <td>

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

                    </td>

                </tr>

                <tr>

                    <td>

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

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

                    </td>

                    <td>

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

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label6" runat="server" Text="Please Enter Age" Font-Size="Large" Font-Names="Verdana"

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

                    </td>

                    <td>

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

                    </td>

                </tr>

                <tr>

                    <td colspan="2">

                        <br />

                        <asp:Button ID="Button1" runat="server" Text="Insert Data" 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 Microsoft.SharePoint.Client;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace ClientOMInsert

{

    public partial class WebForm1 : 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(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text) || string.IsNullOrEmpty(TextBox3.Text) || string.IsNullOrEmpty(TextBox4.Text))

            {

                Label5.Text = "Please Enter Some Values";

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

            }

            else

            {

                ClientContext objContext = new ClientContext("http://win-5c3g1lanj3k:46908/");

                List list = objContext.Web.Lists.GetByTitle(TextBox4.Text);

                ListItemCreationInformation creationInfo = new ListItemCreationInformation();

                Microsoft.SharePoint.Client.ListItem item = list.AddItem(creationInfo);

                item["Title"] = TextBox1.Text;

                item["LastName"] = TextBox2.Text;

                item["Age"] = int.Parse(TextBox3.Text);

                item.Update();

                objContext.ExecuteQuery();

                Label5.Text = "Data inserted Successfully";

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

                TextBox1.Text = string.Empty;

                TextBox2.Text = string.Empty;

                TextBox3.Text = string.Empty;

                TextBox4.Text = string.Empty;

            }

        }

    }

}

 

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

 

OutputSharepoint2010.jpg
 

 

Step 5: The nothing entered output of the application looks like this:

 


 EnteredOutputSharepoint2010.jpg

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

 

DataEnteringOutputSharepoint2010.jpg

DataEnteringOutputSharepoint2010.1.jpg
 


 

Step 7: The output of the list on SharePoint application looks like this:

 

ListOutputSharepoint2010.jpg
 

I hope this article is useful for you.

COMMENT USING