A quick look on Sharepoint object model programs – Part 2

In my previous post you can have a quick look on Sharepoint object model programs – Part 1

Here, once again am going to post Sharepoint object model programs (part 2) with minimum lines of code and are really handy, which are actually most useful even.

Show only BLOG sites:

//STS#0 – Team Site
//STS#1 – Blank Site
//STS#2 – Document Workspace
//MPS#0 – Basic Meeting Workspace
//MPS#1 – Blank Meeting Workspace
//MPS#2 – Decision Meeting Workspace
//MPS#3 – Social Meeting Workspace
//MPS#4 – Multipage Meeting Workspace
//WIKI#0 – Wiki
//BLOG#0 – Blog

[sourcecode language=”csharp”]static void Main(string[] args)
{
SPSite site = new SPSite(“http://servername:port”);
foreach (SPWeb web in site.AllWebs)
{
string template = web.WebTemplate + “#” + web.Configuration;
if (template.ToUpper() == “BLOG#0”)
{
Console.WriteLine(web.Title);
}
}
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]

Show services and status in Sharepoint server farm:

[sourcecode language=”csharp”]static void Main(string[] args)
{
SPServiceCollection services = SPFarm.Local.Services;
foreach (SPService service in services)
{
Console.WriteLine(“Service Name: {0}”, service.Name);
Console.WriteLine(“Service Status: {0}”, service.Status);
Console.WriteLine(“————————————-“);
}
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]

Adding/Retrieving values to property bag:

[sourcecode language=”csharp”]static void Main(string[] args)
{
using (SPSite siteCollection = new SPSite(“http://servername:port”))
{
using (SPWeb site = siteCollection.RootWeb)
{
site.Properties.Add(“TempKey”, “TempValue”);//Adding value
site.Properties.Update();

Console.WriteLine(site.Properties[“TempKey”]);//Reading value
}
}
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]

Read files in a folder:

[sourcecode language=”csharp”]static void Main(string[] args)
{
SPSite site = new SPSite(“http://servername:port”);
SPWeb web = site.OpenWeb();
SPFolder folder = web.GetFolder(“Shared Documents”);
SPFileCollection files = folder.Files;
foreach (SPFile file in files)
{
Console.WriteLine(file.Name.ToString());
}
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]

Get content database for all site collections:

[sourcecode language=”csharp”]
static void Main(string[] args)
{
SPSite site = new SPSite(“http://CentralAdminSiteURL”);
SPFarm farm = site.WebApplication.Farm;
SPWebService service = farm.Services.GetValue(“”);
foreach (SPWebApplication webapp in service.WebApplications)
{
foreach (SPSite mysite in webapp.Sites)
{
Console.WriteLine(mysite.Url+” —– “+ mysite.ContentDatabase.Name);
}
}
Console.WriteLine(“Press any key to continue…..”);
Console.ReadLine();
}
[/sourcecode]