Tuesday, May 22, 2012

How to know existing SharePoint site template name and ID

Scenario :

I want to know what is the template name used  while creating site for each site under a site collection and i want to store each site template name in a list like "sitename -- templatename"

Steps Involved
------------------------------------------------------------------------------------------------------------

TIP:  We can find site template name for a particular site 
step 1: open ur site in browser and right click select view source. in  view source find the following line of text "var g_wsaSiteTemplateId="


example: "var g_wsaSiteTemplateId = 'STS#0';" --- STS is the template name and 0 is the template id


2. var g_wsaSiteTemplateId = 'SDPROJ1#0'; ---- SDPROJ1 is the custom template name and 0 is the id.
---------------------------------------------------------------------------------



first i tried to get template name for a site using client object model and display in a message box but unfortunately using client object model we cant get template name for a site.

Then i tried with server object model , Here the steps..

 here am creating one webpart in that am taking one button and one label.

in button click event am writing my code for getting template name for each site and saving it into a list called "AllSitesTemplates"





Here the code :


 string str = "";
            try
            {
                SPWebApplication myWebApp = SPContext.Current.Site.WebApplication;
                SPSiteCollection mySiteCollection = myWebApp.Sites;
                SPSite oSiteCollection = SPContext.Current.Site;
                SPWeb oWebsiteRoot = oSiteCollection.OpenWeb();
                SPList oList = oWebsiteRoot.Lists["AllSitesTemplates"];
                foreach (SPSite mySite in mySiteCollection)
                {
                 
                    SPWebCollection myWebCollection = mySite.AllWebs;
                    foreach (SPWeb web in myWebCollection)
                    {
                        str += "Web Name : " + web.Title;
                        str += "\n Web Template Name : " + web.WebTemplate;
                        str += "\n Web Template ID : " + web.WebTemplateId;
                        str += "\n Web GUID : " + web.ID+" ";
                     
                        SPListItemCollection listItemCOll;

                        //add the item in the User List
                        SPListItem oListItem = oList.Items.Add();
                        oListItem["Title"] = web.Title;
                        oListItem["TemplateName"] = web.WebTemplate + "#" + web.WebTemplateId;

                        oListItem.Update();          
                        web.Dispose();
                    }
                    mySite.Dispose();
                }            
            }
            catch (Exception ex)
            {
                str+="Error: " + ex.Message;
            }
            Label1.Text = str;
        }

Now deploy your webpart and add this web part to page and click the button.


Now click the button and go to AllSitesTemplates list and u can find the result.



1 comment: