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.
Note that, it is a best practice to dispose SPWeb object explicitly when we are using SPWeb in foreach statement.
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.
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();
}
}
Note that, it is a best practice to dispose SPWeb object explicitly when we are using SPWeb in foreach statement.
Comments
Post a Comment