-->

Monday, 21 May 2012

Sharepoint 2010 Client Object Model - Part 2

In my previous post we discussed about the basic's of the Client Object Model in Sharepoint 2010 and it's architecture.


As we discussed COM can be implemented using 3 client API's viz
  1. .NET managed application (Console application/Windows Forms Application) 
  2. Silverlight 2.0 application
  3. ECMAScript (JavaScript, JScript)

This Article we will discuss it with the use of the .Net Managed Application


Create a console application with Microsoft .Net Framework 3.5 and use the following code snippet in the Main File
Note : You will be required to add reference to the Sharepoint Client Assemblies located under C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll

static void Main(string[] args)
        {
            var _ctx = new ClientContext("<sitecolleciton_url>");
            var _web = _ctx.Web;
            var _lists = _web.Lists;

            _ctx.Load(_lists, c => c.Include(l => l.Title, l => l.Description)
                  .Where(l => l.Hidden == false)
                );

            // Call to the Load method does not actually load anything.
            // Instead, it informs the client object model that when the
            // application calls the ExecuteQuery method, you want to load
            // the property values of the siteCollection object.
            _ctx.ExecuteQuery();

            foreach (var list in _lists)
            {
                Console.WriteLine(list.Title + " *** " + ((list.Description.Length > 0) ? list.Description.Substring(0, 25) + "..." : String.Empty));
            }
            Console.ReadLine();
        }



ClientContext : ClientContext object instantiates the context object for a specific site collection. It serves as the main entry point for accessing the client object model.
Load : This method is called to retrieve the properties of a specified client object, such as the ListCollection collection. The properties are stored in the client object. Request call to the sharepoint is not made yet during this call, it will just store the properties that has to be retreived.
ExecuteQuery : The call to the ExecuteQuery method causes the SharePoint Foundation 2010 managed client object model to send the request to the server. There is no network traffic until the application calls the ExecuteQuery method.

Note : To optimize data retrieval, the Client OM queue all requests to the SharePoint server till an invocation to the ExecuteQuery is made. So no data will be available from the server till the ExecuteQuery method is invoked. If you want to use a property that you are not asked to load in the ClientContext.Load method, you’ll get PropertyOrFieldNotInitializedException is thrown.

In our other example we will use the LoadQuery method

static void Main(string[] args)
        {
            var _ctx = new ClientContext(" <sitecolleciton_url> ");
            var _web = _ctx.Web;
            var _lists = _web.Lists;

             var _query = from list in _lists select list;
            // The LoadQuery method has different semantics than the Load method.
            // Whereas the Load method populates the client object 
            // (or client object collection) with data from the server, the LoadQuery                                                   
            // method populates and returns a new collection.
            // This means that you can query the same object collection multiple times
            // and keep separate result sets for each query.
            IEnumerable<List> myLists = _ctx.LoadQuery(_query);
            _ctx.ExecuteQuery();

            foreach (var list in myLists)
            {
                Console.WriteLine(list.Title + " ***** " + list.Description);
            } 
            Console.ReadLine();
        }


 LoadQuery : The LoadQuery method is similar in functionality to the Load method, except that LoadQuery method populates and returns a new collection while Load method populates the client object. This means that you can query the same object collection multiple times and keep separate result sets for each query. Additionally you can filter the returned result set.

Below example depicts how to create a list, In this case We have created the document library "My Docs"

static void Main(string[] args)
        {
            var _ctx = new ClientContext(" <sitecolleciton_url> ");
            var _web = _ctx.Web;
            var _lists = _web.Lists;

            //Create a document library
            ListCreationInformation listCreationInfo = new ListCreationInformation();
            listCreationInfo.Title = "My Docs";
            listCreationInfo.TemplateType = (int)ListTemplateType.DocumentLibrary;

            List oList = _web.Lists.Add(listCreationInfo);

            _ctx.ExecuteQuery();

        }


Same way we can update the properties of the lists, See the code-snippet below

static void Main(string[] args)
        {
            var _ctx = new ClientContext(" <sitecolleciton_url> ");
            var _web = _ctx.Web;
            var _lists = _web.Lists;

            //Update the list information
            List oList = _web.Lists.GetByTitle("My Docs");

            oList.Description = "My Document reporsitory";

            oList.Update();

            _ctx.ExecuteQuery();

        }



In our subsequent articles we will describe how to implement each of the COM using different client.

I Hope this article was informative. Happy Sharepointing !

Please do Share/Like/Comment if this article was helpful.