Follow Us

Skip Navigation LinksHome > Articles > Sharepoint > SharePoint 2010 Client Object Model : Managed Code - 1

SharePoint 2010 Client Object Model : Managed Code - 1

This article explains Managed Code in SharePoint Client Object Model..

By Project 9X   On   Tuesday, 05 October 2010

Page Views : 17073   |   Technologies : Sharepoint

Rating : Rated :
| More..

SharePoint 2010 client Object Model is written based on .Net CLR, so we can write custom code in C# or VB to write code for SharePoint 2010 platform.

For this we need following dlls , located at 14/ISAPI folder.

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

Now To see client object model in action and to learn more about it, we will create on web part project in visual studio 2010.

1. Open Visual studio 2010 and create Visual web part type project.

2. Open SharePoint 2010 and create one web part page on your site. Open Project properties and set "start browser with URL" to this SharePoint page. In my case it’s: http://splab10/SitePages/Webpart Test Page 2.aspx. In 2010 we can easily debug the web part by putting breakpoint. Later, add this web part to SharePoint page so we can hit the breakpoint.

[Talking little bit about project structure, List datawebpart.cs is the native web part code behind, which simply has code to load user control. Currently I will right code in user control code behind file. This is “ListDataWebPartUserControl.ascx.cs”. I'll explain more about project structure in future articles about deployment process.]

3. Add SharePoint client dlls to the project and following reference to usercontrol.ascx.cs file.

using Microsoft.SharePoint.Client;

4. Now coming to the point, SharePoint client object model, data loads on demand, improving performance by less data transfer on wire.

Following is the example code which displays name of the Lists in the current site and then displays list items from specific list called "Mylist" which is my custom list.

ClientContext ctx;
        protected void Page_Load(object sender, EventArgs e)
            ctx = new ClientContext(SPContext.Current.Web.Url);
            Web web = ctx.Web;
            ctx.Load(web, oweb => oweb.Title);
            ctx.Load(web, oweb => oweb.Lists);

            LiteralControl li = new LiteralControl();
           li.Text ="web Title: " +  web.Title + "
"; foreach (var item in web.Lists) { li.Text += item.Title + "
"; } li.Text += "
"; Microsoft.SharePoint.Client.ListItemCollection Items; List oMylist = ctx.Web.Lists.GetByTitle("MyList"); CamlQuery query = new Microsoft.SharePoint.Client.CamlQuery(); string camlQueryXml = "" + ""; query.ViewXml = camlQueryXml; Items = oMylist.GetItems(query); ctx.Load(Items); ctx.ExecuteQuery(); foreach (var item in Items) { li.Text += item["Title"].ToString() + "
"; } this.Controls.Add(li); ctx.Dispose(); }

By pressing F5, it deploys the web part and opens the webpage that we created. Now if you don’t have that web part on this page, edit page and drop web part.

Click on edit page.

Find your web part and add.

Now, As we discussed earlier, It loads data on demand. Here we prepared our object that what we need. In image below, we haven’t executed ctx.ExecuteQuery(); statement. See properties in ctx.web, since properties are not loaded, it says 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException' and for collections it says 'The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.'

After ctx.ExecuteQuery(); it fetches data from server for requested objects, here web title and lists collection.

Here, in ctx = new ClientContext(SPContext.Current.Web.Url); SPContext.Current.Web.Url will get the url of the site on which our webpart is placed.

ClientContext Class

public class ClientContext : ClientRuntimeContext

It Represents the context for SharePoint objects and operations.

Similarly to programming against server objects in the server context, which involves instantiating or returning a central object that provides access to the object model, the new client-side object models use a ClientContext object (ECMAScript: ClientContext) as the "center of gravity" for all operations. The process of obtaining and working with sites and data begins by retrieving a context object.

The ClientContext object (ECMAScript: ClientContext) serves as the main entry point for accessing the client object model and as the central object through which to orchestrate requests and initiate actions within a site collection. Use object properties of the ClientContext object (ECMAScript: ClientContext) to obtain site collections or Web sites through which to access other SharePoint Foundation client objects remotely.

Use the [ClientContext] class to return context information about such objects as the current Web application, site, site collection, or server version.

In the server object model, when you return a SPWeb object, all of its properties become available for use in code. But to improve performance in the client object model, when you return a Web object certain properties are not included, and you must explicitly retrieve them.

Here, Once we have all lists loaded, that doesnt mean we have list items for all loaded recursively.

we have to load list items now.

for that, prepare CAML query object which is also from client object model.

CamlQuery query = new Microsoft.SharePoint.Client.CamlQuery(); 
            string camlQueryXml = "" + 
query.ViewXml = camlQueryXml; 

then load this in context as shown in previous code block, and perform executeQuery operation.

It generates output as shown below.

By this now you will have some idea of Client Object Model. We will explore more examples and concepts about this now.


Keywords : using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.Runtime;

Tags : SharePoint 2010SharePoint Client OM Object Model Managed Client Code
Rate This Article :

Comments :

# 1 Coltrane Wrote on 11/02/2010

Excellent article! Code worked correctly, and well written.

blog comments powered by Disqus
User Login
Username :
Password :
Register Login

Forgot Password

Related Articles