SharePoint2010 Client Model With Example


Working with SharePoint2010 Client Model With Example.


The Client Object Model (OM) is a new programming interface for SharePoint 2010 where code runs on a user’s client machine against a local object model and interacts with data on the SharePoint Server. Client OM methods can be called from JavaScript, .NET code or Silverlight code and makes building rich client applications for SharePoint easy. One API to rule them all – Yep, whether its WPF or Windows Forms or Silverlight or Javascript – your code uses Client Object Model to interact with the SharePoint site to access the data. Now, there is something common that everybody can use instead of creating their own wrapper services to access SharePoint data!


DLL used.
1.        Microsoft.SharePoint.Client
2.        Microsoft.SharePoint.Client.Runtime
DLL Path:

Path: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\

Create new window application and include give the ref. to above mentioned file. The following code I explain the different-2 ways to fetching the data using SharePoint2010 Client model.

To fetching the data using Client object model we have to create the client context using the ClientContext(@"http://home:8082");. ClientContext(@"http://home:8082") takes the URL of the site as constructor.

The bettor approach fetch the data you needs.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;

namespace ClientObMO
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //GettextPass1();
            //GetLambdaexvalues();
            // GetLambdaexForUpdatevalues();
            //GetvalueswithIqueryable();
            GetvalueswithIEnumerable();
        }

        // simple way
        public void GettextPass1()
        {
            //var ctx = new ClientContext(@"http://home:8082");
            var newtcx = new ClientContext(@"http://home:8082");
           
           // get the context of the web
            var web = newtcx.Web;

            List _List = web.Lists.GetByTitle("Site Pages");
           
            /// only load the list
            ///
           
            ///this will only load the list and it's property  you will not able to read the other values
            newtcx.Load(_List);
           
            //
            newtcx.ExecuteQuery();

            var title = _List.Title;

            var dis = _List.Description;

            MessageBox.Show("Title:" + title + " desc:" + dis);
        }

        // Lambda Ex.
        public void GetLambdaexvalues()
        {
            var newtcx = new ClientContext(@"http://home:8082");

            var web = newtcx.Web;
            // only get the title and discription  the values you want
            // rest of the values you will get error.
            newtcx.Load(web, X => X.Title, X => X.Description);
            newtcx.ExecuteQuery();

            var title = web.Title;
            var dis = web.Description;
           
            // following line give error becuase the value not load in lambda ex.
            //var content = web.ContentTypes;

            MessageBox.Show("Title:" + title + " desc:" + dis);
        }
        
        // Lambda Ex with update
        public void GetLambdaexForUpdatevalues()
        {
            var newtcx = new ClientContext(@"http://home:8082");
            var web = newtcx.Web;
            newtcx.Load(web, X => X.Title, X => X.Description);
            newtcx.ExecuteQuery();

            var title = web.Title;
            var dis = web.Description;
            // give error for getiing the values
            // MessageBox.Show(web.QuickLaunchEnabled.ToString());

            web.QuickLaunchEnabled = true;
            web.Update();
            newtcx.ExecuteQuery();
            MessageBox.Show("Title:" + title + " desc:" + dis);
        }

        // load the data Using  IQueryable
        public void GetvalueswithIQueryable()
        {
           
            var newtcx = new ClientContext(@"http://home:8082");
            var web = newtcx.Web;
            var lists = web.Lists;

            newtcx.Load(
             lists,
             list => list
                 .Include(
                     x => x.Title,
                     x => x.Hidden)
                 .Where(x => x.BaseType == BaseType.GenericList)
              );


            newtcx.ExecuteQuery();
            string str = "\n";
            foreach (var item in lists)
            {
                str =    str + item.Title + "\n";
            }

            MessageBox.Show(str);
        }

        // get values with using IEnumerable
        public void GetvalueswithIEnumerable()
        {
           
            var newtcx = new ClientContext(@"http://home:8082");
            var web = newtcx.Web;
            var lists = web.Lists;

            IEnumerable<List> _list = newtcx.LoadQuery(lists.Where(x => x.BaseType == BaseType.GenericList));
          
            newtcx.ExecuteQuery();
            string str = "\n";
            foreach (var item in _list)
            {
                str = str + item.Title + "\n";
            }

            MessageBox.Show(str);
        }
    }
}
Happy Coding  J

Comments

Popular posts from this blog

SharePoint RPC Protocols Examples Using OWSSVR.DLL

Types of Features in SharePoint 2013

Send Email using SharePoint Rest API