Skip to main content

Client Object Model Samples in SP2010


using (ClientContext myContext = new ClientContext(siteurl))
{

/// SAMPlE : 01
myContext.Load(myContext.Site);
myContext.ExecuteQuery();
Console.WriteLine(myContext.Site.Url);

/// SAMPlE : 02
/// Client Object Model Using the Load Method

/*
myContext.Load(myContext.Web, web => web.Lists.Include(list => list.Title).Where(field => field.Hidden == false));
myContext.ExecuteQuery();
foreach (List list in myContext.Web.Lists)
{
Console.WriteLine(list.Title);
}
*/


/// SAMPlE : 03
/// Client Object Model Using the LoadQuery Method

/*var query1 = from list1 in myContext.Web.Lists
select list1;*/

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.Read();
}


Get All List Names and the Corresponding Views Information

using (ClientContext myContext = new ClientContext(siteurl))
{
ClientObjectPrototype listProtoype = myContext.Web.Lists.RetrieveItems();
ClientObjectCollectionPrototype viewProtoType =
listProtoype.RetrieveCollectionObject(ListObjectPropertyNames.Views);
viewProtoType.RetrieveItems().Retrieve();
listProtoype.Retrieve();
myContext.ExecuteQuery();
foreach (var list in myContext.Web.Lists )
{
Console.WriteLine("The views in list: " + list.Title);
foreach (View view in list.Views)
{
Console.WriteLine("\t"+view.Title);
}
}
Console.Read();
}

Update Web title using client object model

using (ClientContext myContext = new ClientContext(siteurl))
{
myContext.Load(myContext.Web);
myContext.ExecuteQuery();
myContext.Web.Title = "SP2010 Development Demo Portal";
myContext.Web.Update();
myContext.ExecuteQuery();
}
}

// Add a new list into the SharePoint site.
List newlyAddedList = myContext.Web.Lists.Add(
new ListCreationInformation()
{
Title = "A New List",
Description = "Some Description",
TemplateType = (int) ListTemplateType.Announcements
}
);
newlyAddedList.Update();
myContext.ExecuteQuery();

Comments

Popular posts from this blog

SharePoint Game Webpart

http://spgameoflifewebpart.codeplex.com/ Goal: The Game of Life web part was created for a talk I presented at SharePoint Saturday New York in 2009. At the time, SharePoint 2010 was still in Beta and most people hadn't see the Enterprise Metadata Management (taxonomy) features. I used the Game of Life web part as an introduction to the new taxonomy API. I offered to provide the code, so here it is. Requirements ----------------------- - SharePoint Server 2010 with Enterprise Features - Visual Studio 2010 (inc. SharePoint tools) Setup ----------------------- 1. Add your account as a Term Store Administrator. This right is independent to Site Collection Administrator or Farm Administrator status. To open the Term Store Management Tool go to Site Actions > Site Settings > Site Administartion > Term Store Management. You can also  open the same interface from Central Administration > Manage Service Applications > Managed Metadata Service. 2. Run the sol...

CAML Query Helper Class

http://sharepointcamlhelper.codeplex.com/ https://camldotnet.svn.codeplex.com/svn/JohnHolliday.Caml.Net/CAML.cs CAMLManager mgr = new CAMLManager(); mgr.QueryGroups.Add(new QueryGroup("Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, "A Title")); mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, "Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, "Another Title")); mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.And, QueryGroup.MergeTypes.Contain, "Total", Types.FieldTypes.Number, Types.QueryTypes.Geq, "2")); string caml = mgr.GetCAML(); Will provide the following: <Where>   <And>     <Or>       <Eq>         <FieldRef Name="Title" /><Value Type="Text">A Title</Value>       </Eq>       <Eq>         <FieldRef Name="Title" /><Value Type="Text">Another Title</Value>     ...

Globally Reusable Workflow in SharePoint 2010

http://www.codeproject.com/KB/sharepoint/ReusableWFsSharePoint2010.aspx http://www.khamis.net/blog/Lists/Posts/Post.aspx?ID=18 Video: Publish globally reusable workflows in SharePoint Designer 2010