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
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();
}
Show services and status in Sharepoint server farm:
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();
}
Adding/Retrieving values to property bag:
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();
}
Read files in a folder:
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();
}
Get content database for all site collections:
static void Main(string[] args)
{
SPSite site = new SPSite("http://CentralAdminSiteURL");
SPFarm farm = site.WebApplication.Farm;
SPWebService service = farm.Services.GetValue<SPWebService>("");
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();
}
Comments
Comment from gowri sankar
Time August 29, 2010 at 9:34 am
Can you pleas tell more about property bag? Where exactly we can use this efficiently?
Comment from G Vijai Kumar
Time August 30, 2010 at 4:38 pm
@ Gowri Sankar: I have posted a new article on understanding SharePoint property bag settings please look into the article and let me know your comments, thanks


Pingback from Microsoft Sharepoint Server » A quick look on Sharepoint object model programs – Part 3
Time August 28, 2010 at 4:09 pm
[...] I have got the chance to post useful Sharepoint object model programs (part 3) with bare minimum lines of code and really handy even. Please follow the link for a quick look on Sharepoint object model programs Part 1 and Part 2 [...]