Example 1 :
The following example displays title and url of all sites in a
specific site colleciton.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
SPSite sc = new SPSite(
"http://c4968397007/sites/peersfjeslaiflaesaf");
foreach (SPWeb web in sc.AllWebs)
{
Console.WriteLine("Title : {0}",
web.Title);
Console.WriteLine("Url : {0}",
web.Url);
Console.WriteLine("Created on : {0}",
web.Created);
}
}
}
}
Example 2:
The following code modifies the Title of the top-level site in a
specific site collection.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = site.RootWeb)
{
web.Title = web.Title + " - Changed by code!";
web.Update();
}
}
}
}
}
Example 3:
The following code deletes a specific subsite in a site collection.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://sp2013dev/sites/peers487"))
{
SPWeb web = site.OpenWeb("Sales");
web.Delete();
}
}
}
}
Example 4:
Delete all sub sites under a specific site.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
string siteUrl;
Console.WriteLine("Enter the site url: ");
siteUrl = Console.ReadLine();
SPWeb web =
new SPSite(siteUrl).OpenWeb();
for (int i = web.Webs.Count - 1; i >= 0; i--)
web.Webs[i].Delete();
}
}
}
Example 5:
The following code enumerates through all of the lists contained in a
sample SharePoint site, displaying a few of the properties of
each list. If the list is hidden, an asterisk is displayed before
the list title and the item count.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite mySiteCollection = new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
foreach (SPList list in web.Lists)
{
Console.WriteLine("{0}{1} - {2} items.",
list.Hidden ? "* " : "", list.Title, list.ItemCount);
Console.WriteLine("Description : {0}", list.Description);
Console.WriteLine("Created by {0}", list.Author.Name);
Console.WriteLine
("----------------------------------------------------");
}
Console.WriteLine("\n{0} lists found.", web.Lists.Count);
}
}
}
}
}
Example 6:
Update the price of all products by a 10% increase.
Assume that a list called Products with the Price column is existing.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
string siteUrl;
Console.WriteLine("Enter the site url: ");
siteUrl = Console.ReadLine();
SPWeb web =
new SPSite(siteUrl).OpenWeb();
SPList list = web.Lists["Products"];
foreach(SPListItem item in list.Items)
{
item["Price"] =
((double) item["Price"] * 1.1).ToString();
item.Update();
}
}
}
}
Example 7:
The following code creates a Customers list based on the Contacts list
template.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite mySiteCollection =
new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
SPList list = web.Lists.TryGetList("Customers");
if (list != null) list.Delete();
Guid newListId = web.Lists.Add(
"Customers", // List Title
"Company's Customers", // List Description
SPListTemplateType.Contacts // List Template Type
);
SPList newList = web.Lists[newListId];
newList.OnQuickLaunch = true;
newList.Update();
Console.WriteLine("Created list: {0}", newList.Title);
}
}
}
}
}
Example 8:
The following code populates the Customers list with items.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite mySiteCollection =
new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
SPList list = web.Lists["Customers"];
SPListItem newItem;
newItem = list.Items.Add();
newItem["Last Name"] = "Gorantla";
newItem["First Name"] = "Ramesh";
newItem["Email Address"] = "ramesh@peerstech.com";
newItem.Update();
newItem = list.Items.Add();
newItem["Last Name"] = "Doppalapudi";
newItem["First Name"] = "Kiran Kumar";
newItem["Email Address"] = "kiran@hotmail.com";
newItem.Update();
newItem = list.Items.Add();
newItem["Last Name"] = "Tenneti";
newItem["First Name"] = "Surendra";
newItem["Email Address"] = "surendra@hotmail.com";
newItem.Update();
newItem = list.Items.Add();
newItem["Last Name"] = "Gorantla";
newItem["First Name"] = "Rajani";
newItem["Email Address"] = "rajani@gmail.com";
newItem.Update();
newItem = list.Items.Add();
newItem["Last Name"] = "Boyapati";
newItem["First Name"] = "Suneetha";
newItem["Email Address"] = "suneetha@yahoo.co.in";
newItem.Update();
newItem = list.Items.Add();
newItem["Last Name"] = "Bandlamudi";
newItem["First Name"] = "Ravi";
newItem["Email Address"] = "ravi@hotmail.com";
newItem.Update();
}
}
}
}
}
Example 9:
The following code displays First Name, Last Name and E-mail Address
of all customers in the Customers list.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite mySiteCollection =
new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
SPList list = web.Lists["Customers"];
foreach (SPListItem item in list.Items)
{
Console.WriteLine("{0} {1} - {2}",
item["First Name"],
item["Last Name"],
item["Email Address"]);
}
}
}
}
}
}
Example 10:
The following code displays all products having a price between 5
and 10.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite mySiteCollection =
new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
SPList list = web.Lists["Products"];
SPQuery myQuery = new SPQuery();
// Define columns to retrieve
myQuery.ViewFields = @"<FieldRef Name='Title' /><FieldRef Name='Price' />";
// Force retrieving only the selected columns
myQuery.ViewFieldsOnly = true;
// Define the query
myQuery.Query = @"
<Where>
<And>
<Geq>
<FieldRef Name='Price' />
<Value Type='Currency'>5</Value>
</Geq>
<Leq>
<FieldRef Name='Price' />
<Value Type='Currency'>10</Value>
</Leq>
</And>
</Where>
<OrderBy>
<FieldRef Name='Title' Ascending='True' />
</OrderBy>";
// Define the maximum number of results for each (like a SELECT TOP)
myQuery.RowLimit = 10;
// Query for items
SPListItemCollection items = list.GetItems(myQuery);
foreach (SPListItem item in items)
{
Console.WriteLine("{0} - {1:C2}",
item["Title"], (double)item["Price"]);
}
}
}
}
}
}
The following example displays title and url of all sites in a
specific site colleciton.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
SPSite sc = new SPSite(
"http://c4968397007/sites/peersfjeslaiflaesaf");
foreach (SPWeb web in sc.AllWebs)
{
Console.WriteLine("Title : {0}",
web.Title);
Console.WriteLine("Url : {0}",
web.Url);
Console.WriteLine("Created on : {0}",
web.Created);
}
}
}
}
Example 2:
The following code modifies the Title of the top-level site in a
specific site collection.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = site.RootWeb)
{
web.Title = web.Title + " - Changed by code!";
web.Update();
}
}
}
}
}
Example 3:
The following code deletes a specific subsite in a site collection.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://sp2013dev/sites/peers487"))
{
SPWeb web = site.OpenWeb("Sales");
web.Delete();
}
}
}
}
Example 4:
Delete all sub sites under a specific site.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
string siteUrl;
Console.WriteLine("Enter the site url: ");
siteUrl = Console.ReadLine();
SPWeb web =
new SPSite(siteUrl).OpenWeb();
for (int i = web.Webs.Count - 1; i >= 0; i--)
web.Webs[i].Delete();
}
}
}
Example 5:
The following code enumerates through all of the lists contained in a
sample SharePoint site, displaying a few of the properties of
each list. If the list is hidden, an asterisk is displayed before
the list title and the item count.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite mySiteCollection = new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
foreach (SPList list in web.Lists)
{
Console.WriteLine("{0}{1} - {2} items.",
list.Hidden ? "* " : "", list.Title, list.ItemCount);
Console.WriteLine("Description : {0}", list.Description);
Console.WriteLine("Created by {0}", list.Author.Name);
Console.WriteLine
("----------------------------------------------------");
}
Console.WriteLine("\n{0} lists found.", web.Lists.Count);
}
}
}
}
}
Example 6:
Update the price of all products by a 10% increase.
Assume that a list called Products with the Price column is existing.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
string siteUrl;
Console.WriteLine("Enter the site url: ");
siteUrl = Console.ReadLine();
SPWeb web =
new SPSite(siteUrl).OpenWeb();
SPList list = web.Lists["Products"];
foreach(SPListItem item in list.Items)
{
item["Price"] =
((double) item["Price"] * 1.1).ToString();
item.Update();
}
}
}
}
Example 7:
The following code creates a Customers list based on the Contacts list
template.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite mySiteCollection =
new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
SPList list = web.Lists.TryGetList("Customers");
if (list != null) list.Delete();
Guid newListId = web.Lists.Add(
"Customers", // List Title
"Company's Customers", // List Description
SPListTemplateType.Contacts // List Template Type
);
SPList newList = web.Lists[newListId];
newList.OnQuickLaunch = true;
newList.Update();
Console.WriteLine("Created list: {0}", newList.Title);
}
}
}
}
}
Example 8:
The following code populates the Customers list with items.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite mySiteCollection =
new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
SPList list = web.Lists["Customers"];
SPListItem newItem;
newItem = list.Items.Add();
newItem["Last Name"] = "Gorantla";
newItem["First Name"] = "Ramesh";
newItem["Email Address"] = "ramesh@peerstech.com";
newItem.Update();
newItem = list.Items.Add();
newItem["Last Name"] = "Doppalapudi";
newItem["First Name"] = "Kiran Kumar";
newItem["Email Address"] = "kiran@hotmail.com";
newItem.Update();
newItem = list.Items.Add();
newItem["Last Name"] = "Tenneti";
newItem["First Name"] = "Surendra";
newItem["Email Address"] = "surendra@hotmail.com";
newItem.Update();
newItem = list.Items.Add();
newItem["Last Name"] = "Gorantla";
newItem["First Name"] = "Rajani";
newItem["Email Address"] = "rajani@gmail.com";
newItem.Update();
newItem = list.Items.Add();
newItem["Last Name"] = "Boyapati";
newItem["First Name"] = "Suneetha";
newItem["Email Address"] = "suneetha@yahoo.co.in";
newItem.Update();
newItem = list.Items.Add();
newItem["Last Name"] = "Bandlamudi";
newItem["First Name"] = "Ravi";
newItem["Email Address"] = "ravi@hotmail.com";
newItem.Update();
}
}
}
}
}
Example 9:
The following code displays First Name, Last Name and E-mail Address
of all customers in the Customers list.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite mySiteCollection =
new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
SPList list = web.Lists["Customers"];
foreach (SPListItem item in list.Items)
{
Console.WriteLine("{0} {1} - {2}",
item["First Name"],
item["Last Name"],
item["Email Address"]);
}
}
}
}
}
}
Example 10:
The following code displays all products having a price between 5
and 10.
using System;
using Microsoft.SharePoint;
namespace KNRao.SP2013Examples
{
class Program
{
static void Main(string[] args)
{
using (SPSite mySiteCollection =
new SPSite("http://sp2013dev/sites/peers487"))
{
using (SPWeb web = mySiteCollection.OpenWeb())
{
SPList list = web.Lists["Products"];
SPQuery myQuery = new SPQuery();
// Define columns to retrieve
myQuery.ViewFields = @"<FieldRef Name='Title' /><FieldRef Name='Price' />";
// Force retrieving only the selected columns
myQuery.ViewFieldsOnly = true;
// Define the query
myQuery.Query = @"
<Where>
<And>
<Geq>
<FieldRef Name='Price' />
<Value Type='Currency'>5</Value>
</Geq>
<Leq>
<FieldRef Name='Price' />
<Value Type='Currency'>10</Value>
</Leq>
</And>
</Where>
<OrderBy>
<FieldRef Name='Title' Ascending='True' />
</OrderBy>";
// Define the maximum number of results for each (like a SELECT TOP)
myQuery.RowLimit = 10;
// Query for items
SPListItemCollection items = list.GetItems(myQuery);
foreach (SPListItem item in items)
{
Console.WriteLine("{0} - {1:C2}",
item["Title"], (double)item["Price"]);
}
}
}
}
}
}
No comments:
Post a Comment