The below console application shows the common operations using the SharePoint 2013 Server object model.
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoConsoleApplication
{
class Program
{
static void Main(string[] args)
{
//DisplayListItems();
//DisplayItemById(1);
//QueryList();
//CreateList();
//DisplayPagesLib();
//AddListFields();
//AddSiteContentType();
//AddView();
AddListItem();
Console.ReadLine();
}
private static void DisplayListItems()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPList empList = web.GetList("/Lists/EmpInfo");
SPListItemCollection items = empList.Items;
if (null == items)
{
return;
}
foreach (SPListItem item in items)
{
Console.WriteLine(item.Title);
}
}
}
}
catch (SPException spe)
{
Console.WriteLine(spe.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void DisplayItemById(int id)
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPList empList = web.GetList("/Lists/EmpInfo");
SPListItem item = empList.GetItemById(id);
if (null == item)
{
Console.WriteLine("Item with this id: {0} doesn't exists", id);
return;
}
Console.WriteLine(item.Title);
Console.WriteLine(item["EmpFirstName"]);
Console.WriteLine(item.ID);
Console.WriteLine(item["EmpLastName"]);
}
}
}
catch (SPException spe)
{
Console.WriteLine(spe.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void QueryList()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPList empList = web.GetList("/Lists/EmpInfo");
SPQuery query = new SPQuery();
query.RowLimit = 100;
query.Query = "<Where><Or><Eq><FieldRef Name='ID' /><Value Type='Counter'>1</Value></Eq><Eq><FieldRef Name='ID' /><Value Type='Counter'>2</Value></Eq></Or></Where>";
SPListItemCollection items = empList.GetItems(query);
foreach (SPListItem item in items)
{
Console.WriteLine("item : " + item.ID);
Console.WriteLine(item.Title);
Console.WriteLine(item["EmpFirstName"]);
Console.WriteLine(item["EmpLastName"]);
}
}
}
}
catch (SPException spe)
{
Console.WriteLine(spe.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void CreateList()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
Guid listId = web.Lists.Add("DemoCustomList", "DemoCustomList", SPListTemplateType.GenericList);
SPList list = web.Lists[listId];
list.ContentTypesEnabled = true;
list.EnableAttachments = false;
list.EnableVersioning = true;
list.Update();
}
}
}
catch (Exception ex) { }
}
private static void AddListFields()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPList demoList = web.GetList("/Lists/DemoCustomList");
//demoList.Fields.Add("DemoCol", SPFieldType.Text, false);
//demoList.Fields.Add("DemoChoice", SPFieldType.Choice, false);
//SPFieldChoice choiceField = demoList.Fields["DemoChoice"] as SPFieldChoice;
//choiceField.AddChoice("DemoChoice1");
//choiceField.AddChoice("DemoChoice2");
//choiceField.AddChoice("DemoChoice3");
//choiceField.AddChoice("DemoChoice4");
//choiceField.Update();
Guid empInfoListId = web.GetList("/Lists/Empinfo").ID;
demoList.Fields.AddLookup("DemoLkpCol", empInfoListId, false);
SPFieldLookup lkpDemoLookup = demoList.Fields["DemoLkpCol"] as SPFieldLookup;
lkpDemoLookup.LookupField = "Title";
lkpDemoLookup.Update();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void DisplayPagesLib()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPDocumentLibrary pagesLib = web.GetList("/Pages") as SPDocumentLibrary;
SPListItemCollection pagesColl = pagesLib.Items;
foreach (SPListItem item in pagesColl)
{
Console.WriteLine("Title: " + item.Title + " Url: " + SPUrlUtility.CombineUrl(web.Url, item.Url));
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void AddSiteContentType()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
//web.Fields.Add("DemoSiteCol1", SPFieldType.Text, false);
//StringCollection choiceColl = new StringCollection();
//choiceColl.Add("DemoSiteCol2Choice1");
//choiceColl.Add("DemoSiteCol2Choice2");
//choiceColl.Add("DemoSiteCol2Choice3");
//choiceColl.Add("DemoSiteCol2Choice4");
//web.Fields.Add("DemoSiteCol2", SPFieldType.Choice, false, false, choiceColl);
//SPField demoCol1 = web.Fields["DemoSiteCol1"];
//SPField demoCol2 = web.Fields["DemoSiteCol2"];
//SPContentType itemCT = web.ContentTypes["Item"];
//SPContentType demoCt = new SPContentType(itemCT, web.ContentTypes, "DemoCT1");
//web.ContentTypes.Add(demoCt);
//SPField field1 = web.Fields["DemoSiteCol1"];
//SPField field2 = web.Fields["DemoSiteCol2"];
//SPFieldLink link1 = new SPFieldLink(field1);
//SPFieldLink link2 = new SPFieldLink(field2);
//SPContentType demoCT1 = web.ContentTypes["DemoCT1"];
//demoCT1.FieldLinks.Add(link1);
//demoCT1.FieldLinks.Add(link2);
//demoCT1.Update();
SPContentType demoSiteCT = web.ContentTypes["DemoCT1"];
SPList demoList = web.GetList("/Lists/DemoCustomList");
demoList.ContentTypes.Add(demoSiteCT);
demoList.Update();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void AddView()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPList demoList = web.GetList("/Lists/DemoCustomList");
//StringCollection viewFields = new StringCollection();
//viewFields.Add("DemoSiteCol1");
//viewFields.Add("DemoSiteCol2");
//viewFields.Add("Title");
//viewFields.Add("ID");
//SPView view = demoList.Views.Add("Demoview", viewFields, "", 30, true, true);
SPView demoView = demoList.Views["Demoview"];
demoView.Query = "<OrderBy><FieldRef Name='DemoSiteCol1' /></OrderBy>";
demoView.Update();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void AddListItem()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPList demoList = web.GetList("/Lists/DemoCustomList");
SPListItem item= demoList.Items.Add();
item["Title"] = "demo123";
item["DemoSiteCol1"] = "DemoSiteCol1123412";
item["DemoSiteCol2"] = "DemoSiteCol2Choice4";
item.Update();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoConsoleApplication
{
class Program
{
static void Main(string[] args)
{
//DisplayListItems();
//DisplayItemById(1);
//QueryList();
//CreateList();
//DisplayPagesLib();
//AddListFields();
//AddSiteContentType();
//AddView();
AddListItem();
Console.ReadLine();
}
private static void DisplayListItems()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPList empList = web.GetList("/Lists/EmpInfo");
SPListItemCollection items = empList.Items;
if (null == items)
{
return;
}
foreach (SPListItem item in items)
{
Console.WriteLine(item.Title);
}
}
}
}
catch (SPException spe)
{
Console.WriteLine(spe.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void DisplayItemById(int id)
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPList empList = web.GetList("/Lists/EmpInfo");
SPListItem item = empList.GetItemById(id);
if (null == item)
{
Console.WriteLine("Item with this id: {0} doesn't exists", id);
return;
}
Console.WriteLine(item.Title);
Console.WriteLine(item["EmpFirstName"]);
Console.WriteLine(item.ID);
Console.WriteLine(item["EmpLastName"]);
}
}
}
catch (SPException spe)
{
Console.WriteLine(spe.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void QueryList()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPList empList = web.GetList("/Lists/EmpInfo");
SPQuery query = new SPQuery();
query.RowLimit = 100;
query.Query = "<Where><Or><Eq><FieldRef Name='ID' /><Value Type='Counter'>1</Value></Eq><Eq><FieldRef Name='ID' /><Value Type='Counter'>2</Value></Eq></Or></Where>";
SPListItemCollection items = empList.GetItems(query);
foreach (SPListItem item in items)
{
Console.WriteLine("item : " + item.ID);
Console.WriteLine(item.Title);
Console.WriteLine(item["EmpFirstName"]);
Console.WriteLine(item["EmpLastName"]);
}
}
}
}
catch (SPException spe)
{
Console.WriteLine(spe.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void CreateList()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
Guid listId = web.Lists.Add("DemoCustomList", "DemoCustomList", SPListTemplateType.GenericList);
SPList list = web.Lists[listId];
list.ContentTypesEnabled = true;
list.EnableAttachments = false;
list.EnableVersioning = true;
list.Update();
}
}
}
catch (Exception ex) { }
}
private static void AddListFields()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPList demoList = web.GetList("/Lists/DemoCustomList");
//demoList.Fields.Add("DemoCol", SPFieldType.Text, false);
//demoList.Fields.Add("DemoChoice", SPFieldType.Choice, false);
//SPFieldChoice choiceField = demoList.Fields["DemoChoice"] as SPFieldChoice;
//choiceField.AddChoice("DemoChoice1");
//choiceField.AddChoice("DemoChoice2");
//choiceField.AddChoice("DemoChoice3");
//choiceField.AddChoice("DemoChoice4");
//choiceField.Update();
Guid empInfoListId = web.GetList("/Lists/Empinfo").ID;
demoList.Fields.AddLookup("DemoLkpCol", empInfoListId, false);
SPFieldLookup lkpDemoLookup = demoList.Fields["DemoLkpCol"] as SPFieldLookup;
lkpDemoLookup.LookupField = "Title";
lkpDemoLookup.Update();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void DisplayPagesLib()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPDocumentLibrary pagesLib = web.GetList("/Pages") as SPDocumentLibrary;
SPListItemCollection pagesColl = pagesLib.Items;
foreach (SPListItem item in pagesColl)
{
Console.WriteLine("Title: " + item.Title + " Url: " + SPUrlUtility.CombineUrl(web.Url, item.Url));
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void AddSiteContentType()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
//web.Fields.Add("DemoSiteCol1", SPFieldType.Text, false);
//StringCollection choiceColl = new StringCollection();
//choiceColl.Add("DemoSiteCol2Choice1");
//choiceColl.Add("DemoSiteCol2Choice2");
//choiceColl.Add("DemoSiteCol2Choice3");
//choiceColl.Add("DemoSiteCol2Choice4");
//web.Fields.Add("DemoSiteCol2", SPFieldType.Choice, false, false, choiceColl);
//SPField demoCol1 = web.Fields["DemoSiteCol1"];
//SPField demoCol2 = web.Fields["DemoSiteCol2"];
//SPContentType itemCT = web.ContentTypes["Item"];
//SPContentType demoCt = new SPContentType(itemCT, web.ContentTypes, "DemoCT1");
//web.ContentTypes.Add(demoCt);
//SPField field1 = web.Fields["DemoSiteCol1"];
//SPField field2 = web.Fields["DemoSiteCol2"];
//SPFieldLink link1 = new SPFieldLink(field1);
//SPFieldLink link2 = new SPFieldLink(field2);
//SPContentType demoCT1 = web.ContentTypes["DemoCT1"];
//demoCT1.FieldLinks.Add(link1);
//demoCT1.FieldLinks.Add(link2);
//demoCT1.Update();
SPContentType demoSiteCT = web.ContentTypes["DemoCT1"];
SPList demoList = web.GetList("/Lists/DemoCustomList");
demoList.ContentTypes.Add(demoSiteCT);
demoList.Update();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void AddView()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPList demoList = web.GetList("/Lists/DemoCustomList");
//StringCollection viewFields = new StringCollection();
//viewFields.Add("DemoSiteCol1");
//viewFields.Add("DemoSiteCol2");
//viewFields.Add("Title");
//viewFields.Add("ID");
//SPView view = demoList.Views.Add("Demoview", viewFields, "", 30, true, true);
SPView demoView = demoList.Views["Demoview"];
demoView.Query = "<OrderBy><FieldRef Name='DemoSiteCol1' /></OrderBy>";
demoView.Update();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void AddListItem()
{
try
{
using (SPSite site = new SPSite(@"<<siteUrl>>"))
{
using (SPWeb web = site.OpenWeb())
{
SPList demoList = web.GetList("/Lists/DemoCustomList");
SPListItem item= demoList.Items.Add();
item["Title"] = "demo123";
item["DemoSiteCol1"] = "DemoSiteCol1123412";
item["DemoSiteCol2"] = "DemoSiteCol2Choice4";
item.Update();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
No comments:
Post a Comment