Client Object Model of SharePoint 2013

Sagarp
Posted by in SharePoint category on for Beginner level | Points: 250 | Views : 2709 red flag

You can perform all SharePoint website related tasks, ie- Read and write all website related properties, create new SharePoint web site, SharePoint List operations(create new SharePoint lists, Retrieve all SharePoint lists in a SharePoint website, insert, update and delete in SharePoint lists),SharePoint document library operations (same as SharePoint List operations).

Introduction

In this Article We explore to start the development in the managed client object model, you will need to add references of Microsoft.SharePoint.Client.dll ,Microsoft.SharePoint.ClientRuntime.dll into the assemblies and add the using statement for Microsoft.SharePoint.Client  namespace and start writing code.

Basic operations using Managed Client Object Model:

managed client object,  you can perform all SharePoint website related tasks, ie- Read and write all website related properties, create new SharePoint web site, SharePoint List operations(create new SharePoint lists, Retrieve all SharePoint lists in a SharePoint website, insert, update and delete in  SharePoint lists),SharePoint document library operations (same as SharePoint List operations).

Much like the Server -side object model, which is the other framework for development in SharePoint, CSOM also needs a starting point in the form of a central object which will instantiate and provide access to the client object model.  This central object is called the Client Context. The Client Context object orchestrates requests and initiates actions within a site collection. Once the Client Context Object is initialized, it provides information to the site collection or website through which we can access other SharePoint client object remotely as depicted in the below code.

Using the code

Add the references Microsoft.SharePoint.dll and Microsoft.SharePoint.Client.dll.

Create List Item:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
namespace CreateListItem
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://servername:2525/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("TestList");
            ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();
            ListItem oListItem = oList.AddItem(listCreationInformation);
            oListItem["Title"] = "Hello World";
            oListItem.Update();
            clientContext.ExecuteQuery();
        }
    }
}
Update List Item:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;

namespace UpdateListItem
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://servername:2525/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("TestList");
            ListItem oListItem = oList.GetItemById(5);
            oListItem["Title"] = "Hello World Updated!!!";
            oListItem.Update();
            clientContext.ExecuteQuery();
        }
    }
}
Delete List Item:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;

namespace UpdateListItem
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "http://servername:2525/";
            ClientContext clientContext = new ClientContext(siteUrl);
            List oList = clientContext.Web.Lists.GetByTitle("TestList");
            ListItem oListItem = oList.GetItemById();         
            oListItem.DeleteObject();
            clientContext.ExecuteQuery();
        }
    }
}

Conclusion

Using the SharePoint Client object model, developers can work with the SharePoint Web Application remotely.

Page copy protected against web site content infringement by Copyscape

About the Author

Sagarp
Full Name: S P
Member Level: Bronze
Member Status: Member
Member Since: 10/30/2009 5:28:35 AM
Country: India
Thanks SagarP http://www.emanonsolutions.net http://emanonsolutions.blogspot.com/
http://www.emanonsolutions.net
I have 4+ years of experience in Sharepoint technologies (Sharepoint 2010,MOSS 2007 & WSS 3.0)i m MCTS,MCPD,MCP

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)