SharePoint 2010 - Introduction to Client Object Model

SharePoint 2010 provides a new client object model that enables you to create SharePoint solutions that run remotely from the SharePoint server farm. For example, the client object model enables you to consume and manipulate SharePoint data in Windows Forms applications, Windows Presentation Framework application, console applications, Silverlight applications, and ASP.NET Web applications.
 
For working with client object model you need to get two master dlls:
 
·         Microsoft.SharePoint.Client.dll
·         Microsoft.SharePoint.Client.Runtime.dll
 
These dlls you can find under following path from machine on which you have installed SharePoint 2010.
 
Path to get DLL's: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI
 
The SharePoint Foundation 2010 managed client object model consists of two assemblies that contain five namespaces. If you look at the classes available in those namespaces, you see many classes. Primarily classes that have direct counterparts to some familiar classes in the SharePoint Foundation server object model.
 
Following table shows you Client Side classes those are similar to Server Object Model:
 
 
Client Object Model Classes Server Object Model Classes
ClientContext SPContext
Site SPSite
Web SPWeb
List SPList
ListItem SPListItem
Field SPField
  1. Code snippets for How to Load List and List items:

public void LoadList()

        {

            using (SP.ClientContext ctx = new SP.ClientContext("SharePointSiteURL"))

            {

                var web = ctx.Web;

 

               // Let's only work with the specific List object and save resources

                // by not fetching any other objects

                var list = ctx.Web.Lists.GetByTitle("ListName");

 

                // Load only the list variable and execute the query

                ctx.Load(list);

                ctx.ExecuteQuery();

 

                SP.CamlQuery camlQuery = new SP.CamlQuery();

                 camlQuery.ViewXml =

                            @"<View>

                        <Query>

                                  <Where>

                                <Eq>

                                  <FieldRef Name='Name'/>

                                  <Value Type='Text'>Shailesh</Value>

                                </Eq>

                                  </Where>

                        </Query>

                              </View>";

                SP.ListItemCollection listItems = list.GetItems(camlQuery);

                ctx.Load(listItems);

                ctx.ExecuteQuery();

 

                //Now you can iterate listitems collection and can get listitems using foreach loop

 

                foreach (SP.ListItem listItem in listItems)

                {

                            //you can get value of list column from listitem as follow:

                            //listitem["ID"]

                        //listitem["Name"]

                }

            }

        }

  2. Code snippets for How to insert  List items to a List:

public void SaveListItem()

        {

            using (SP.ClientContext ctx = new SP.ClientContext("SharePointSiteURL"))

            {

                var web = ctx.Web;

                var list = ctx.Web.Lists.GetByTitle("ListName");

                ctx.Load(list);

                ctx.ExecuteQuery();

                SP.ListItemCreationInformation itemCreateInfo = new SP.ListItemCreationInformation();

                SP.ListItem listItem = list.AddItem(itemCreateInfo);

                listItem["Name"] = "Shailesh";

                listItem["Surname"] = "Soni";

                listItem.Update();

               ctx.ExecuteQuery();

            }

        }

  3. Code snippets for update List items in List:

Public  void UpdateListItem()

        {

            using (SP.ClientContext ctx = new SP.ClientContext("SharePointSiteURL"))

            {

                var web = ctx.Web;

 

               // Let's only work with the specific List object and save resources

                // by not fetching any other objects

                var list = ctx.Web.Lists.GetByTitle("ListName");

 

                // Load only the list variable and execute the query

                ctx.Load(list);

                ctx.ExecuteQuery();

 

                SP.CamlQuery camlQuery = new SP.CamlQuery();

                 camlQuery.ViewXml =

                            @"<View>

                        <Query>

                                  <Where>

                                <Eq>

                                  <FieldRef Name='Name'/>

                                  <Value Type='Text'>Shailesh</Value>

                                </Eq>

                                  </Where>

                        </Query>

                              </View>";

                SP.ListItemCollection listItems = list.GetItems(camlQuery);

                ctx.Load(listItems);

                ctx.ExecuteQuery();

 

                //Now you can iterate listitems collection and can get listitems using foreach loop

 

                foreach (SP.ListItem listItem in listItems)

                {

                             listitem["Address"] = “blah blah blah”;

                            listitem.Update();

                }

                ctx.ExecuteQuery();

            }

        }

  4. Code snippets for delete List items in List:

Public  void DeleteListItem()

        {

            using (SP.ClientContext ctx = new SP.ClientContext("SharePointSiteURL"))

            {

                var web = ctx.Web;

 

               // Let's only work with the specific List object and save resources

                // by not fetching any other objects

                var list = ctx.Web.Lists.GetByTitle("ListName");

 

                // Load only the list variable and execute the query

                ctx.Load(list);

                ctx.ExecuteQuery();

 

                SP.CamlQuery camlQuery = new SP.CamlQuery();

                 camlQuery.ViewXml =

                            @"<View>

                        <Query>

                                  <Where>

                                <Eq>

                                  <FieldRef Name='Name'/>

                                  <Value Type='Text'>Shailesh</Value>

                                </Eq>

                                  </Where>

                        </Query>

                              </View>";

                SP.ListItemCollection listItems = list.GetItems(camlQuery);

                ctx.Load(listItems);

                ctx.ExecuteQuery();

 

                //Now you can iterate listitems collection and can get listitems using foreach loop

                foreach (SP.ListItem listItem in listItems)

                            listItem.DeleteObject();

                ctx.ExecuteQuery();

            }

        }

 Conclusion: The above code snippets and detail description will help you to understand Client Object Model to begin with SharePoint 2010. 

9 Comments

Comments have been disabled for this content.