Skip to main content

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

What is SharePoint RPC Protocols? Part 1 This reference includes information about the methods and usage of SharePoint Foundation Remote Procedure Call (RPC) protocol. This protocol can be used in Win32-based applications or in ASPX applications to make HTTP POST requests to the server. Methods in this protocol that do not modify the contents of the database can also be used in URL protocol to make HTTP GET requests. Definition taken from http://msdn.microsoft.com/en-us/library/ms448359.aspx You will find the OWSSVR.DLL in SharePoint 2010 Server Physical Path: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI and MOSS C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI OWSSVR.DLL List of commands DialogView  Display ExportList GetProjSchema GetUsageBlob HitCounter RenderView To read more about the OWSSVR.DLL command Please read the URL Protocol from Microsoft Blog having a URL http://msdn.microsoft.com/en...

SharePoint 2013 Search Database Part 1

SharePoint 2013 Search architecture drastically change, as earlier in FS4SP 2010 we have 2 search applications “FASTContentSSA” and “FASTQuerySSA” and 7 total database in use. Following are the List of DB (FS4SP) FASTContentSSA Search Service Application DB Search Service Application Crawl Store DB Search Service Application Property Store DB FASTQuerySSA Search Service Application DB Search Service Application Crawl Store DB Search Service Application Property Store DB FASTSearchAdminDatabase : Fast Search Admin Database  In SharePoint 2013 search has only 1 Search Service application and 4 database in use. No property store database need any more, now the properties are directly stored inside the index component and all the index directly indexed to the physical system where FS4SP data comes from database as well as from the File system now data directly stored and indexed/ retried from the Physical disk because of this performance increase and search experien...

STS CryptographicException Error : Key set does not exist

Common mistakes Both SharePoint Site and SSO Site NOT running on the same application pool. Application pool identity user doesn’t have permission to access the certification.  Solution to this problem Set the same identity pool to  : 2. Be sure to grant rights to the certificate for the App Pool running the web service Start -> Run -> MMC File -> Add/Remove Snapin Add the Certificates Snap In Select Computer Account, then hit next Select Local Computer (the default), then click Finish On the left panel from Console Root, navigate to Certificates (Local Computer) -> Personal -> Certificates You're certificate will most likely be here. Right click on your certificate -> All Tasks -> Manage Private Keys Set you're private key settings here. Add app pool account Reset iis