COM – ClientContext and ExecuteQuery

Console app that retrieves a site’s URL:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint.Client;

   
 

namespace COMHelloWorld3

{

class
Program

{

static
void Main(string[] args)

{

string siteUrl = http://bruce-pc/sites/DST”;

using (ClientContext myContext = new
ClientContext(siteUrl))

{

myContext.Load(myContext.Site);

myContext.ExecuteQuery();

Console.WriteLine(myContext.Site.URL);

Console.ReadKey();

}

}

}

}

   
 

In your console application, if you were to comment out the Load and ExecuteQuery statements, and then try and access site.Url, you would get a PropertyOrFieldNotInitializedException.

The Load method allows you to specify to the ClientContext what you’re interested in retrieving. But it isn’t until you execute the ExecuteQuery
method that the object is actually retrieved. Another method similar to Load is the LoadQuery method.

———————————————————————————————————————————

* Use Linq to Sharepoint to retrieve info

   
 

namespace COMHelloWorld3

{

class
Program

{

static
void Main(string[] args)

{

string siteUrl = http://bruce-pc/sites/DST”;

using (ClientContext myContext = new
ClientContext(siteUrl))

{

var query = from list in myContext.Web.Lists

where list.Hidden != false
select list;

var lists = myContext.LoadQuery(query);

myContext.ExecuteQuery();

   
 

foreach (var list in lists)

{

Console.WriteLine(list.Title);

}

Console.ReadKey();

}

}

}

}

——————————————————————————————————————————
Updating data from the client object model is rather simple. The following code snippet updates the title

of the site:

myContext.Load(myContext.Web);

myContext.ExecuteQuery();

myContext.Web.Title = “New title”;

myContext.Web.Update();

myContext.ExecuteQuery();
——————————————————————————————————————————
// Add a new list into the SharePoint site with Client Object Model

List newlyAddedList = myContext.Web.Lists.Add(

new ListCreationInformation()

{

Title = “A New List”,

Description = “Some Description”,

TemplateType = (int) ListTemplateType.Announcements

}

);

newlyAddedList.Update();

myContext.ExecuteQuery();

——————————————————————————————————————————–
Silverlight and Javascript can also access COM.

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: