Search This Blog

Tuesday, February 10, 2015

SharePoint 2010 Client Object Model(Pragmatically)

       Client object model to access share point server resources in 32-bit environment. weakly typed objects.

       3 Types in client object model

  1. Microsoft.SharePoint.Client class
  2. Javascript lirary
  3. Silverlight Library.
 End user can use any of these resources to connect,query and update resources in the sharepoint site.

download client object model
http://www.microsoft.com/en-us/download/details.aspx?id=12323

Example 1)

display site collection URL
display website Title


            ClientContext ctx = new ClientContext("http://sp2010:21016");
            Site site = ctx.Site;
            ctx.Load(site);
            ctx.ExecuteQuery();
            Console.WriteLine(site.Url);

            Web web = ctx.Web;
            ctx.Load(web);
            ctx.ExecuteQuery();
            Console.WriteLine(web.Title);


Display all lists in toplevel site

ClientContext ctx = new ClientContext("http://sp2010:21016");


            Web web = ctx.Web;
           // ctx.Load(web);
           // ctx.ExecuteQuery();


            ctx.Load(web.Lists);
            ctx.ExecuteQuery();
            foreach(List list in web.Lists)
            Console.WriteLine(list.Title);


Get List title and itemcount 

            ClientContext ctx = new ClientContext("http://sp2010:21016");

            Web web = ctx.Web;
            //ctx.Load(web);
            //ctx.ExecuteQuery();
            var query = from l in web.Lists
                        where l.Title == "Sales-Data"
                        select l;
            //prepare the statement
            IEnumerable<List> sales_list = ctx.LoadQuery(query);
            ctx.ExecuteQuery();//execute the statement

            foreach (List list in sales_list)
            {
                Console.WriteLine(list.Title+"count="+list.ItemCount);
            }

Get Share documents title and itemcount 

            ClientContext ctx = new ClientContext("http://sp2010:21016");

            Web web = ctx.Web;
            //ctx.Load(web);
            //ctx.ExecuteQuery();
            var query = from l in web.Lists
                        where l.Title == "Shared Documents"          
                        select l;
           
            IEnumerable<List> sales_list = ctx.LoadQuery(query);
            ctx.ExecuteQuery();

            foreach (List list in sales_list)
            {
                Console.WriteLine(list.Title+"count="+list.ItemCount);
            }


No comments:

Post a Comment