Deactivate/Remove Site Template Programmatically in SharePoint 2010/2013

2 comments
Hi All,

In One of my Examples, we have seen to how to create Site Template Programmatically. http://sharepointchampion.blogspot.in/2014/05/save-site-as-template-programatically.html

In a below example, we going to look How to deactivate and remove Site Template from the Solution Gallery.
Please find the below code Snippet. I hope it will save your Time :-)

protected void btnRemoveTemp_Click(sender object,EventArgs e)
{
           RemoveSiteTemplate(NameofTemplate)
}

        public bool RemoveSiteTemplate(string templateName)
        {
            Guid siteID = SPContext.Current.Site.ID;
            bool isTemplateDeleted = false;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite oSite = new SPSite(siteID))
                {
                    using (SPWeb web = oSite.OpenWeb())
                    {
                        using (SPWeb rootweb = web.Site.RootWeb)
                        {
                            SPList solGallery =  rootweb.Site.GetCatalog(
                                                  SPListTemplateType.SolutionCatalog);
                            //deactivate solution template
                var solutionResult= from s inrootweb.Site.Solutions.OfType<SPUserSolution>() 
                                    where s.Name == templateName + ".wsp" 
                                    select s;
                            if (solutionResult != null)
                            {        
                             rootweb.Site.Solutions.Remove(solutionResult.FirstOrDefault());
                                // Delete solution template                                                                var listItemResult = from i in 
                                                   solGallery.Items.OfType<SPListItem>()
                                                   where i.Name == templateName + ".wsp" 
                                                   select i;
                              if (listItemResult != null)
                              {
                               listItemResult.FirstOrDefault().Delete();
                               isTemplateDeleted = true;
                              }
                            }
                        }
                    }
                }
            });
            return isTemplateDeleted;
        }

Related Post

2 comments:

  1. can you do this via csom for provider hosted apps? I cannot seem to get a handle of the solutions in the solutions gallery. thanks.

    ReplyDelete