Monday, 16 June 2014

SharePoint 2010 Client Object Model


Server object model
  • Microsoft.SharePoint.SPSite
  • Microsoft.SharePoint.SPWeb
  • Microsoft.SharePoint.SPList
In the client object model you have:
  • Microsoft.SharePoint.Client.Site
  • Microsoft.SharePoint.Client.Web
  • Microsoft.SharePoint.Client.List

  1. 1.       Load All Lists from SP Server to label
Add new references from server.
Choose “Browse”
Navigate to this folder:
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI

Select these two files:
Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll 
Add using SP = Microsoft.SharePoint.Client;

using System;
using System.Linq;

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SP = Microsoft.SharePoint.Client;


namespace SP2010ClientObjectModel.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//Load all lists using Client objecy model and LINQ

string myUrl = "http://jiteng:42204";
using (SP.ClientContext ctx = new SP.ClientContext(myUrl))
{
SP.Web site = ctx.Web;

ctx.Load(site);
ctx.Load(site.Lists);
ctx.Load(site, x => x.Lists.Where(l => l.Title != null));
ctx.ExecuteQuery();

Label1.Text += "Lists:";
foreach (SP.List list in site.Lists)
{
Label1.Text += list.Title + "<br>";
}

}

}

}
}


  1. 2.       Create a Visual web part
Add new references from server.
Choose “Browse”
Navigate to this folder:
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI

Select these two files:
Microsoft.SharePoint.Client.dll
Microsoft.SharePoint.Client.Runtime.dll 
Add using SP = Microsoft.SharePoint.Client;


Add List box, Text box and button controls.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using SP = Microsoft.SharePoint.Client;

namespace SP2010ClientObjectModel.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}

        protected void Button1_Click(object sender, EventArgs e)
        {
            lbLists.Items.Clear();

            using (SP.ClientContext ctx = new SP.ClientContext("http://jiteng:42204"))
            {
                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);
                }
            }

        }

        protected void lbLists_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SP.ClientContext ctx = new SP.ClientContext("http://jiteng:42204"))
            {
                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;
            }

        }
}
}



You use the ClientContext object to connect and work with the SharePoint server
You use ClientContext.Load() to load the objects you need to work with
You use the ClientContext.ExecuteQuery() to execute the query and fetch the objects