PostRank Topblogs 2009 - #20 in Sharepoint

Windows Live Alerts web tracker
Chat with me if I'm online!
search blog
most popular
MCP MCTS MCT MVP

SP 2010: Getting started with the Client Object Model in SharePoint 2010 

Author: Tobias Zimmergren
http://www.zimmergren.net | http://www.tozit.com | @zimmergren

Introduction

In this article I will talk about how you can get started with using the Client Object Model in SharePoint 2010. This new object model is introduced in SharePoint Foundation 2010, and does not require SharePoint Server 2010 to be installed.

The Client Object Model is a new object model introduced in SharePoint 2010 which is aimed at making things easier for the developer when developing client-side applications for SharePoint 2010.

Three new client APIs

With the Client Object Model, Microsoft has introduced a set of new APIs to work with to ease the daily tasks of developers.

  • .NET Managed Applications (using the .NET CLR)
  • Silverlight Applications
  • ECMAScript (JavaScript, JScript)

The client APIs provide you as a developer with a subset of the Microsoft.SharePoint namespace which is based on the server-side object model.

Naming conventions

As a small side-note I would like to mention that the objects you are comfortable with calling “SPWeb”, “SPSite”, “SPList” and so on are now named “Web”, “Site”, “List”.

So to conclude the naming convention, the “SP” in the objects has been dropped. Easy enough to remember, right?

Good, let’s move on to some actual code samples to get things fired up!

SharePoint 2010 Client Object Model – Creating your first client-side application

With some fundamental knowledge about the Client OM mentioned above, I would like to take this one step further and actually demonstrate how you can utilize the Client OM in order to create your first client-side application and work with data from SharePoint, instead of using the built-in Web Services.

Step 1 – Creating the Visual Studio project

To be able to follow along with this article, the prerequisites are that you have SharePoint 2010 and Visual Studio 2010 installed in a development environment.

  • Launch Visual Studio 2010
  • Create a new project
  • Choose “Windows Application” and apply the following settings to your project:
    • Target Framework: .NET 3.5
    • Build output: AnyCpu (or x64)

As you can see we are specifically targeting .NET 3.5 since this is the version of the framework that SharePoint 2010 is based on.

Also notice that we are changing the build output from x86 (unsupported!) to AnyCpu or x64 in order to be able to compile and run this project properly.

You should now have an empty Windows Application project like so:   
ScreenShot001

 

Step 2 – Adding the Client Object Model references

Next, we need to add references to the Client Object Model in order to be able to work with the client-side APIs from our awesome Windows Application.

  1. Right click the “References” node and choose “Add Reference”  
    ScreenShot002
  2. Choose “Browse
    1. Navigate to this folder:
      C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI
    2. Select these two files:
      1. Microsoft.SharePoint.Client.dll
      2. Microsoft.SharePoint.Client.Runtime.dll 

         ScreenShot004
  3. Make sure they are included in the references of your project: 
    ScreenShot005
  • Add the following using-statement in your class (note the last line!): 
    ScreenShot008

Alright – that was easy enough. Adding the references shouldn’t be any problems. Moving on the the more interesting part – getting our first client object model application up and running!

Step 3 – Make our Windows Application pull out some data from SharePoint

Now that you’ve created your Windows Application, targeted the correct framework and platform, added the Client object model references – we’re ready to take on some Client Object Model action!

Adding the necessary controls to the form
  • Add the following controls to your Windows Application:
    • A new ListBox control
    • A new Button control
    • A new TextBox control (Set multiline = true, readonly = true etc to make it look like mine if you want..)

ScreenShot006

Adding some code-logic to fetch the lists on a particular site and populate the ListBox
  • Double-click the Button that you’ve created and add the following code logic:

private void btnGetLists_Click(object sender, EventArgs e)
{
    lbLists.Items.Clear(); 

    using (SP.ClientContext ctx = new SP.ClientContext("http://zimmer"))
    {
        var web = ctx.Web; 

        ctx.Load(web);
        ctx.Load(web.Lists); 

        ctx.ExecuteQuery(); 

        foreach (SP.List list in web.Lists)
        {
            lbLists.Items.Add(list.Title);
        }
    }
}

  • Double-click the ListBox that you’ve created and add the following code logic:

private void lbLists_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SP.ClientContext ctx = new SP.ClientContext("http://zimmer"))
            {
                var list = ctx.Web.Lists.GetByTitle(lbLists.SelectedItem.ToString());

                ctx.Load(list);
                ctx.ExecuteQuery(); 

                string infoAboutList =
                    string.Format("Title: {0}ItemCount: {1}IsHidden: {2}",
                    list.Title + Environment.NewLine,
                    list.ItemCount + Environment.NewLine,
                    list.Hidden.ToString()); 

                textBox1.Text = infoAboutList;
            }
        }

Things to note:

Running the simple application

When you decide to run this awesomely simple application, it will look something like this: 
ScreenShot009

With a few simple lines of code you have created your first Client-side application for SharePoint 2010.
Of course we could elaborate this sample to make it way more complex, but this is a teaser for you to get started with the client object model.

Step 4 – Authentication Options for our application

Quite naturally, the question of how you can authenticate to SharePoint often pops up when I do my SP 2010 training and seminars. With that said, I’ll walk you through the different authentication options you’ve got for your client application.

Basically there are three (3) authentication options you can use when you’re working with the Client Object Model in SharePoint 2010:

  • Anonymous
  • Default
  • FormsAuthentication

ScreenShot010

If you do not choose an authentication method in your code, the application will default to using the client’s Windows Credentials (DefaultCredentials)

Example 1: Using anonymous authentication

ctx.AuthenticationMode = SP.ClientAuthenticationMode.Anonymous;

Example 2: Using Forms Authentication with the Client Object Model

SP.FormsAuthenticationLoginInfo formsLoginInfo = 
new SP.FormsAuthenticationLoginInfo("TobiasZimmergren", "SecretPassword");

ctx.AuthenticationMode = SP.ClientAuthenticationMode.FormsAuthentication;

ctx.FormsAuthenticationLoginInfo = formsLoginInfo;

When you call the Forms Authentication, it will automatically call the Authentication Web Service for you and return the authenticated cookie.

Summary & Downloads

With very little effort, we have created a new Windows Form application that can be run as a client-side application – without the need of SharePoint to be installed on that machine.

Easy enough we have done so without calling any Web Services.

With that said, you should now at least be updated on how you create your first few Client Object Model applications, and be able to start your own project(s) using this new awesome OM.

ENJOY!

Download my sample project

Download my awesome sample project here

 
Posted on 30-Nov-09 by Tobias Zimmergren
25 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
Tags: 2010, How To, SP2010
 

Links to this post (Trackbacks/Pingbacks)

Comments

Monday, 30 Nov 2009 08:57 by Adam
hell yes! when ur twitter-links was right i could access it hehe. this is good sample to start the client om stff with. thankz

Tuesday, 1 Dec 2009 01:07 by Anders Berntsson
I was missing the extra step for Forms Authentication when trying this out - will give that a go. Super article, Tobias. Keep this up!

Wednesday, 2 Dec 2009 03:46 by Chris
Great article..however are you aware of anyway to provide anonymous access for the silverlight version of the client obkect model. there is no authenticationmode in this assembly?? Thanks

Tuesday, 15 Dec 2009 08:08 by R Stevensson
Interesting! Looks like the ClientOM is something I will be using alot from now on. Great cover of gtting it working!!

Tuesday, 5 Jan 2010 11:01 by Rickard
"awesome" as the Zimmergren would have said himself ;) i think the client objectmodel is going to grow big

Monday, 18 Jan 2010 09:18 by Bryce Wilson
now THATS what im talking about!! thanks

Thursday, 21 Jan 2010 08:34 by Erickster
cool article, good points about authentication. nobody said how to do this before. thx

Wednesday, 3 Feb 2010 04:02 by ucsharp
Thanks Tobias. Aren't there 4 ways to authenticate? I want to use Windows cred that's different from those I'm logged in with. I tried passing a new NetworkCredential object to client contex.Credentials but it does not seem to work. Ideas?

Sunday, 21 Feb 2010 09:58 by Adam
gr8 article here tobias. always to the poing.

Wednesday, 10 Mar 2010 06:28 by Praveen
Good post. I also written some nice articles on SharePoint 2010 articles series. You can find them here. http://praveenbattula.blogspot.com/search/label/SharePoint%202010

Friday, 19 Mar 2010 11:55 by venkat
Hi, Nice Post,thanks a lot. have a query - You are saying in the beginning of the article that the dev environment neds to have sps 2010 installed. but in the summary,you are saying that, this windows appln uses client object model,without having sps 2010 installed in the machine. can you plese clarify? Thanks, Venkat

Sunday, 2 May 2010 04:18 by Stuart Starrs
Great article, I too wrote a similar article except using Silverlight : http://www.intheknow.it/clientomsilverlight.ashx

Wednesday, 12 May 2010 11:00 by Nalwi
Does the silverlight client object model support forms authentication? I am thinking of out of browser scenario

Thursday, 13 May 2010 10:24 by Nalwi
Does the silverlight client object model support forms authentication? I am thinking of out of browser scenario

Tuesday, 1 Jun 2010 09:06 by Shailesh
Great article, I have some queries reagarding the access server. Query: how we can access remote sharepoint server using this client object model?

Wednesday, 2 Jun 2010 09:23 by flash game programming
That is a wonderfully structed blog and the content is just awesome. I will get all my family soon to read your lovely posts altogether.

Friday, 4 Jun 2010 06:58 by Jay
What are the limitations of the Client OM ? Does the Client OM allow to do all those things that can be done using Server OM?

Thursday, 24 Jun 2010 06:22 by Karthik
HI You have explained each point of Client OM very clearly. I thank you and wish to read your tutorials ..

Friday, 2 Jul 2010 12:03 by Jaishree
Excellent! A good start up.

Monday, 19 Jul 2010 11:54 by

Friday, 23 Jul 2010 08:49 by Tonny Leino
thanks for this article. Superb!

Wednesday, 28 Jul 2010 12:30 by guru
hi, I am writing a add in for outlook..I am able to move files to sharepoint site.. but i want to show FloderBrowserDialog box to choose the document library folders. how can i do it?

Name:
URL:
Email:
Comments: