I have a practice of posting handy code snippets which takes less time to read, understand and save in mind with very little space of memory
Please look into few of my previous handy code snippet articles below:
A quick look on SharePoint object model programs
A quick look on SharePoint object model programs – Part 2
A quick look on SharePoint object model programs – Part 3
So in this article I’m going to post SharePoint 2010 Managed Client Object Model programs
Create Site:
[sourcecode language=”csharp”]
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
namespace CreateSite
{
class Program
{
static void Main(string[] args)
{
ClientContext context = new ClientContext("http://SharePointSiteURL");
Web web = context.Web;
WebCreationInformation myWebCreationInformation = new WebCreationInformation();
myWebCreationInformation.Title = "FiveNumber";
myWebCreationInformation.Description = "It’s all about SharePoint";
myWebCreationInformation.Url = "fivenumber";
myWebCreationInformation.Language = 1033; //locale ID that specifies the language the site example: English(US)-1033, Germany-1031, French(France)-1036.
myWebCreationInformation.UseSamePermissionsAsParentSite = true;
myWebCreationInformation.WebTemplate = "STS#0";
Web myNewWeb = web.Webs.Add(myWebCreationInformation);
context.Load(myNewWeb, website => website.Title);
context.ExecuteQuery();
Console.WriteLine("Website Created Successfully: " + myNewWeb.Title);
Console.WriteLine("Press any key to continue…..");
Console.ReadLine();
}
}
}
[/sourcecode]
Get Website Properties:
[sourcecode language=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
namespace Get_Web_Site_Properties
{
class Program
{
static void Main(string[] args)
{
ClientContext context = new ClientContext("http://SharePointSiteURL");
Web web = context.Web;
context.Load(web);
context.ExecuteQuery();
Console.WriteLine("Website site details: \n" + "=====================\n"+"Title: " + web.Title + "\nRelativeURL: " + web.ServerRelativeUrl + "\nDescription: " + web.Description + "\nCreated Date: " + web.Created + "\nLanguage: " + web.Language);
Console.WriteLine("Press any key to continue…..");
Console.ReadLine();
}
}
}
[/sourcecode]
Create List:
[sourcecode language=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
namespace Create_List
{
class Program
{
static void Main(string[] args)
{
ClientContext context = new ClientContext("http://SharePointSiteURL");
Web web = context.Web;
ListCreationInformation myListCreationInformation = new ListCreationInformation();
myListCreationInformation.Title = "MyList";
myListCreationInformation.TemplateType = (int)ListTemplateType.GenericList;
myListCreationInformation.Description = "List created using managed object model";
myListCreationInformation.QuickLaunchOption = QuickLaunchOptions.On;
List list = web.Lists.Add(myListCreationInformation);
context.ExecuteQuery();
Console.WriteLine("List Created Successfully");
Console.WriteLine("Press any key to continue…..");
Console.ReadLine();
}
}
}
[/sourcecode]
Create List Columns:
[sourcecode language=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using sp = Microsoft.SharePoint.Client;
namespace Create_List_Columns
{
class Program
{
static void Main(string[] args)
{
ClientContext context = new ClientContext("http://SharePointSiteURL");
Web web = context.Web;
List list = web.Lists.GetByTitle("MyList");
Field EmployeeID = list.Fields.AddFieldAsXml("", true, AddFieldOptions.DefaultValue);
Field EmployeeName = list.Fields.AddFieldAsXml("", true, AddFieldOptions.DefaultValue);
Field Designation = list.Fields.AddFieldAsXml("", true, AddFieldOptions.DefaultValue);
context.ExecuteQuery();
Console.WriteLine("List Fields Created Successfully");
Console.WriteLine("Press any key to continue…..");
Console.ReadLine();
}
}
}
[/sourcecode]
Add new List Items To Existing List:
[sourcecode language=”csharp”]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
namespace Add_New_List_Item
{
class Program
{
static void Main(string[] args)
{
ClientContext context = new ClientContext("http://SharePointSiteURL");
List myList = context.Web.Lists.GetByTitle("MyList");
ListItemCreationInformation myListItemCreationInformation= new ListItemCreationInformation();
ListItem myListItem = myList.AddItem(myListItemCreationInformation);
myListItem["Title"] = "Mr.";
myListItem["EmployeeID"] = 500;
myListItem["Name"] = "Raja";
myListItem["Designation"] = "SharePoint Developer";
myListItem.Update();
context.ExecuteQuery();
Console.WriteLine("New List Item Added Successfully");
Console.WriteLine("Press any key to continue…..");
Console.ReadLine();
}
}
}
[/sourcecode]