Client object model operations in SharePoint 2013
The below console application example shows common Client object model operations in SharePoint 2013using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoClientObjModelSol
{
class Program
{
static void Main(string[] args)
{
//DisplayListItems();
//AddList();
//AddFields();
AddSubSite();
Console.ReadLine();
}
private static void DisplayListItems()
{
try
{
using (ClientContext ctx = new ClientContext(@"<<siteUrl>>"))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
//ctx.Credentials = System.Net.CredentialCache.DefaultCredentials;
//ctx.Credentials = new System.Net.NetworkCredential()
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
Console.WriteLine(web.Title + " URL: " + web.Url);
List empList = web.Lists.GetByTitle("DemoCustomList");
ListItemCreationInformation itemInfo = new ListItemCreationInformation();
ListItem newItem = empList.AddItem(itemInfo);
newItem["Title"] = "sample123";
newItem["DemoSiteCol1"] = "sampleDemoSiteCol1123412";
newItem["DemoSiteCol2"] = "DemoSiteCol2Choice4";
newItem.Update();
ctx.ExecuteQuery();
ListItemCollection itemColl = empList.GetItems(new CamlQuery()
{
ViewXml = "<View></View>"
});
ctx.Load(itemColl);
ctx.ExecuteQuery();
if (null == itemColl)
{
return;
}
foreach (ListItem item in itemColl)
{
Console.WriteLine(item["Title"]);
}
}
}
catch (ClientRequestException spe)
{
Console.WriteLine(spe.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void AddList()
{
try
{
using (ClientContext ctx = new ClientContext(@"<<siteUrl>>"))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
//ctx.Credentials = System.Net.CredentialCache.DefaultCredentials;
//ctx.Credentials = new System.Net.NetworkCredential()
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
ListCreationInformation listInfo = new ListCreationInformation();
listInfo.Title = "DemoClientList";
listInfo.TemplateType = (int)ListTemplateType.GenericList;
List custList = web.Lists.Add(listInfo);
custList.EnableVersioning = true;
custList.Update();
ctx.ExecuteQuery();
}
}
catch (ClientRequestException ce)
{
Console.WriteLine(ce.Message);
}
}
private static void AddFields()
{
try
{
using (ClientContext ctx = new ClientContext(@"<<siteUrl>>"))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
//ctx.Credentials = System.Net.CredentialCache.De;
//ctx.Credentials = new System.Net.NetworkCredential()
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
List empList = web.Lists.GetByTitle("DemoCustomList");
Field demoField = empList.Fields.AddFieldAsXml(@"<Field DisplayName='DemoSiteCol123' Type='Text'
Required='FALSE'
Name='DemoSiteCol1' />", true, AddFieldOptions.AddToAllContentTypes);
ctx.ExecuteQuery();
}
}
catch (ClientRequestException ce)
{
Console.WriteLine(ce.Message);
}
}
private static void AddSubSite()
{
try
{
using (ClientContext ctx = new ClientContext(@"<<siteUrl>>"))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
//ctx.Credentials = System.Net.CredentialCache.De;
//ctx.Credentials = new System.Net.NetworkCredential()
Web web = ctx.Web;
WebCreationInformation webInfo = new WebCreationInformation();
webInfo.Title = "DemoSubSite";
webInfo.WebTemplate = "STS#1";
webInfo.Url = "DemoSubSite";
Web newWeb = web.Webs.Add(webInfo);
ctx.ExecuteQuery();
}
}
catch (ClientRequestException ce)
{
Console.WriteLine(ce.Message);
}
}
}
}
No comments:
Post a Comment