Monday, January 11, 2010

How to determine Site Definition programmatically

We can create different kind of sites such as Team Site, Document Workspace, Blog etc. Once we create such sites, how do we find out what kind of sites we have? I was trying to find this answer programmatically. Here is the simple code to get that.

using (SPSite mySite = new SPSite("http://mysite"))
{
foreach (SPWeb myWeb in mySite.AllWebs)
{
Console.WriteLine("Name: " + myWeb.Title);
Console.WriteLine("Template ID: " + myWeb.WebTemplateId.ToString());
Console.WriteLine("Template Name: " + myWeb.WebTemplate);
Console.WriteLine("====================");
myWeb.Dispose();
}
}

In this code, I'm looping through each Web in the site collection and display the Title, Site Definition Template ID, and Site Definition template Name of each Web.
Note that, it is a best practice to dispose SPWeb object explicitly when we are using SPWeb in foreach statement.

0 comments:

Post a Comment