Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

SharePoint 2010 Client Object Model: Introduction

, 6 Jun 2012
An overview of the Client Object Model in SharePoint 2010.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;
using System.Net;

namespace ClientObjectModel.Examples
{
    public partial class MainForm : System.Windows.Forms.Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void SiteInfoButton_Click(object sender, EventArgs e)
        {
            ClientContext context = new ClientContext(ServerText.Text);

            context.Load(context.Web);

            context.ExecuteQuery();

            MessageBox.Show("Title: " + context.Web.Title + Environment.NewLine + " Description: " + context.Web.Description);
        }

        private void ListInfoButton_Click(object sender, EventArgs e)
        {
            ClientContext context = new ClientContext(ServerText.Text);
            context.Load(context.Web);
            context.Load(context.Web.Lists);

            context.ExecuteQuery();

            Web web = context.Web;
            ListCollection lists = context.Web.Lists;

            StringBuilder builder = new StringBuilder("The site " + web.Title + " has " + lists.Count.ToString() + " lists" + Environment.NewLine);
            foreach (List list in lists)
                builder.Append(list.Title + Environment.NewLine);

            MessageBox.Show(builder.ToString());
        }

        private void GetListDataButton_Click(object sender, EventArgs e)
        {
            ClientContext context = new ClientContext(ServerText.Text);
            List list = context.Web.Lists.GetByTitle("Tasks");
            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View/>";
            ListItemCollection items = list.GetItems(query);

            context.Load(list);
            context.Load(items);

            context.ExecuteQuery();

            // Show the data
            DataTable table = new DataTable();
            table.Columns.Add("Id");
            table.Columns.Add("Title");
            table.Columns.Add("Status");

            foreach (ListItem item in items)
                table.Rows.Add(item.Id, item["Title"], item["Status"]);

            datagrid.DataSource = table;
        }

        private void UpdateTitleButton_Click(object sender, EventArgs e)
        {
            ClientContext context = new ClientContext(ServerText.Text);
            List list = context.Web.Lists.GetByTitle("Tasks");
            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View/>";
            ListItemCollection items = list.GetItems(query);

            context.Load(items);

            context.ExecuteQuery();

            foreach (ListItem item in items)
                if ((item.Id % 2) == 0)
                {
                    item["Title"] += "**";
                    item.Update();
                }

            context.ExecuteQuery(); // this method is required to send the update() calls to server

            // now try refreshing the list using Get Data button
        }

        private void ReduceResultSizeButton_Click(object sender, EventArgs e)
        {
            ClientContext context = new ClientContext(ServerText.Text);
            Web web = context.Web;

            context.Load(web, w => w.Title);

            context.ExecuteQuery();

            MessageBox.Show("The title is: " + web.Title);
            MessageBox.Show("Now trying to access a field not in Result Set (expect exception)");

            try
            {
                MessageBox.Show(web.Description); // You will get Error here
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ReduceSizeListItemsButton_Click(object sender, EventArgs e)
        {
            ClientContext context = new ClientContext(ServerText.Text);
            List list = context.Web.Lists.GetByTitle("Tasks");

            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View/>";
            ListItemCollection listItems = list.GetItems(query);

            context.Load(listItems, items => items.Include(item => item["Id"]));

            context.ExecuteQuery();

            // Show the data
            DataTable table = new DataTable();
            table.Columns.Add("Id");

            foreach (ListItem item in listItems)
                table.Rows.Add(item.Id);

            datagrid.DataSource = table;
        }

        private void InsertButton_Click(object sender, EventArgs e)
        {
            ClientContext context = new ClientContext(ServerText.Text);
            Web web = context.Web;

            List list = web.Lists.GetByTitle("Tasks");

            ListItemCreationInformation newItem = new ListItemCreationInformation();
            ListItem listItem = list.AddItem(newItem);
            listItem["Title"] = "New Item Created through C#";
            listItem.Update();

            context.ExecuteQuery();
        }

        private void UpdateButton_Click(object sender, EventArgs e)
        {
            ClientContext context = new ClientContext(ServerText.Text);
            List list = context.Web.Lists.GetByTitle("Tasks");
            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View/>";

            ListItemCollection listItems = list.GetItems(query);

            context.Load(listItems);

            context.ExecuteQuery();

            ListItem item = listItems[listItems.Count - 1];
            item["Status"] = "In Progress";
            item.Update();

            context.ExecuteQuery();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            ClientContext context = new ClientContext(ServerText.Text);
            List list = context.Web.Lists.GetByTitle("Tasks");

            ListItemCollection listItems = list.GetItems(new CamlQuery() { ViewXml = "<View/>" });
            context.Load(listItems);
            context.ExecuteQuery();

            listItems[listItems.Count - 1].DeleteObject();
            context.ExecuteQuery();
        }

        private void GetByPageSizeButton_Click(object sender, EventArgs e)
        {
            ClientContext context = new ClientContext(ServerText.Text);
            Web web = context.Web;

            List list = web.Lists.GetByTitle("Tasks");

            CamlQuery query = new CamlQuery();
            query.ViewXml = "<View><RowLimit>3</RowLimit></View>";

            ListItemCollection listItems = list.GetItems(query);

            context.Load(listItems);
            context.ExecuteQuery();

            ShowItems(listItems);
        }

        private void ShowItems(ListItemCollection listItems)
        {
            DataTable table = new DataTable();
            table.Columns.Add("Id");
            table.Columns.Add("Title");
            table.Columns.Add("Status");

            foreach (ListItem item in listItems)
                table.Rows.Add(item.Id, item["Title"], item["Status"]);

            datagrid.DataSource = table;
        }

        private void CriteriaButton_Click(object sender, EventArgs e)
        {
            ClientContext context = new ClientContext(ServerText.Text);
            List list = context.Web.Lists.GetByTitle("Tasks");

            CamlQuery query = new CamlQuery();
            query.ViewXml = @"<View>
    <Query>
        <Where>
        <Eq>
            <FieldRef Name='Status'/>
            <Value Type='Text'>In Progress</Value>
        </Eq>
        </Where>
    </Query>
    </View>";

            ListItemCollection listItems = list.GetItems(query);
            context.Load(listItems, items => items.Include(
                                                            item => item["Id"],
                                                            item => item["Title"],
                                                            item => item["Status"]
                                                            ));
            context.ExecuteQuery();

            ShowItems(listItems);
        }

        private void CredentialsButton_Click(object sender, EventArgs e)
        {
            ClientContext context = new ClientContext(ServerText.Text);
            context.Credentials = new NetworkCredential("user", "password");

            Web web = context.Web;
            context.Load(web);

            context.ExecuteQuery();
            MessageBox.Show(web.Title);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jean Paul V.A
Architect
United States United States
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.
 
In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.
 
Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.
 
You can find some of his work over here. He blogs at http://jeanpaulva.com
Follow on   Twitter

| Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 6 Jun 2012
Article Copyright 2012 by Jean Paul V.A
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid