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();
        }

Published by

G Vijai Kumar

Passion towards Microsoft Technologies, Specialized in MOSS