Home » Sharepoint 2010RSS

How to import site template programmatically

I have customized a subsite and exported as .wsp file. How can I programmatically import this file and use as site template to create a same subsite in another sharePoint server.

I need code to import and use the template file (.wsp) programatically.

 

 

7 Answers Found

 

Answer 1

Hope you find answer in this post, read each reply carefully.

Creating a dynamic site using custom template programmatically

 

Answer 2

Sandip,

Thanks for sharing the link, i followed your instructions and after some reading i am lost. As there are lots of other links/questions and replies in that link which is not exactly covering my question to the point which makes it confusing for me. As i am not much experienced i would like you to write the only code that is answer to my question. I will be thankful to you.

1- How to import a .wsp (site template) file programmatically on a fresh SharePoint (import and activate). This should be done by a proper check if the same is not already installed only then, otherwise skip to next point below

2- Ceate a new sub-site based on custom site template that is uploaded and activated in step No. 1

3- How can I list all existing site templates which are shown while we create a new site? I have to give an option to choose site templete which are already setup on sharepoint server.

 

Answer 3

Hi all,

To my last question for listing all existing web templates I used following code for testing:

staticvoid Main(string[] args)
    {
      string webTemps = "";
      string cusTemps = "";
      using (SPSite site = new SPSite("http://sp2010dev.mydomain.us"))
      {
        using (SPWeb web = site.OpenWeb())
        {

          foreach (SPLanguage lang in web.RegionalSettings.InstalledLanguages)
          {
            foreach (SPWebTemplate template in site.GetWebTemplates((uint)lang.LCID))
            {
              webTemps += template.Name + " = " + template.Title + " (" + lang.LCID + ")\r\n";
            }
            foreach (SPWebTemplate template in site.GetCustomWebTemplates((uint)lang.LCID))
            {
              cusTemps += template.Name + " = " + template.Title + " (Custom)(" + lang.LCID + ")\r\n";
            }
          }
        }
      }
      Console.WriteLine (webTemps);
      Console.WriteLine(cusTemps);
      Console.ReadLine();
    }

but the problem is, its showing all as Standard web templates even i had imported and activated my own few templates manullay to test the code which should be in list of cusTemp but they are all in webTemp. Just to understand then what does mean of customWebTemplates.

also the list is something odd. its showing LCID with my customer web templates. How can i list/filter only those web templates which are avaiable through UI at the time of choosing a template when creating a subsite etc.

GLOBAL#0 = Global template (1033)
STS#0 = Team Site (1033)
STS#1 = Blank Site (1033)
STS#2 = Document Workspace (1033)
MPS#0 = Basic Meeting Workspace (1033)
MPS#1 = Blank Meeting Workspace (1033)
MPS#2 = Decision Meeting Workspace (1033)
MPS#3 = Social Meeting Workspace (1033)
MPS#4 = Multipage Meeting Workspace (1033)
CENTRALADMIN#0 = Central Admin Site (1033)
WIKI#0 = Wiki Site (1033)
BLOG#0 = Blog (1033)
SGS#0 = Group Work Site (1033)
TENANTADMIN#0 = Tenant Admin Site (1033)
ACCSRV#0 = Access Services Site (1033)
ACCSRV#1 = Assets Web Database (1033)
ACCSRV#3 = Charitable Contributions Web Database (1033)
ACCSRV#4 = Contacts Web Database (1033)
ACCSRV#6 = Issues Web Database (1033)
ACCSRV#5 = Projects Web Database (1033)
BDR#0 = Document Center (1033)
OFFILE#0 = (obsolete) Records Center (1033)
OFFILE#1 = Records Center (1033)
OSRV#0 = Shared Services Administration Site (1033)
PPSMASite#0 = PerformancePoint (1033)
BICenterSite#0 = Business Intelligence Center (1033)
SPS#0 = SharePoint Portal Server Site (1033)
SPSPERS#0 = SharePoint Portal Server Personal Space (1033)
SPSMSITE#0 = Personalization Site (1033)
SPSTOC#0 = Contents area Template (1033)
SPSTOPIC#0 = Topic area template (1033)
SPSNEWS#0 = News Site (1033)
CMSPUBLISHING#0 = Publishing Site (1033)
BLANKINTERNET#0 = Publishing Site (1033)
BLANKINTERNET#1 = Press Releases Site (1033)
BLANKINTERNET#2 = Publishing Site with Workflow (1033)
SPSNHOME#0 = News Site (1033)
SPSSITES#0 = Site Directory (1033)
SPSCOMMU#0 = Community area template (1033)
SPSREPORTCENTER#0 = Report Center (1033)
SPSPORTAL#0 = Collaboration Portal (1033)
SRCHCEN#0 = Enterprise Search Center (1033)
PROFILES#0 = Profiles (1033)
BLANKINTERNETCONTAINER#0 = Publishing Portal (1033)
SPSMSITEHOST#0 = My Site Host (1033)
ENTERWIKI#0 = Enterprise Wiki (1033)
SRCHCENTERLITE#0 = Basic Search Center (1033)
SRCHCENTERLITE#1 = Basic Search Center (1033)
SRCHCENTERFAST#0 = FAST Search Center (1033)
visprus#0 = Visio Process Repository (1033)
<span style="text-decoration:underline"><strong>{0F0ACE45-1E7C-44F7-BE70-0DC5C976E8C4}#MyTestSiteTemplate = MyTestSite</strong></span>
<strong><span style="text-decoration:underline">Template (1033)
{65BD8110-DE95-450E-9076-89FBC2157DE3}#MySiteTemplate = MySiteTemplate
-1033
{FDB996BF-9038-403A-9AD9-F851C8C976BA}#MyProdSiteTemplate = MyProdSite
Template (1033)
</span></strong>

 

The last three bold and underlined templates are those which I uploaded and activated manually. Should not these be found in CustomeTeplates' list?

Please help me with all me three questions earlier.

 

 

Answer 4

Hi Shamshad,

 

Thanks for your post.

I don’t think it’s a good idea to achieve this using .wsp Site template. you can create a site definition to achieve.

More information about “Create Site Definitions for SharePoint 2010”.

http://msdn.microsoft.com/en-us/vcsharp/Video/ff623012

 

Answer 5

Hi Wayne,

I watched the video. But still I am looking for upload method code to add solution (MyTemplate.wsp) file and activate it programmatically like we do this from UI (i.e. Site Settings > Galleries > Solutions > upload solution and activate solution). The reason is I have to create different templates based on client's requirement (ie. color theme and other navigation links etc.) by time to time and I want those should be setup in users sharePoint server automatically. I am working on making a setup installer which will show a dropdown based on an .xml/config file for available templates in a particular folder and let user choose any template among a list of available templates. Then watever the user choose template we create a subsite based on that template. It will save alot of time and in future if we modify existing template based on client requirement we will name it a new template and copy in setup folder and update new template information in xml/config file. When user run setup it will show new template available for users when selected new template will be upload on users sharePoint servers by simply running the installer setup and hence the installer will add/upload new template and based on it users will be able to avail the new template style without any problem.

I tried following code to upload templates in All sites> Gallaries > solutions but it was uploaded in "_catalogs/lt/Forms/AllItems.aspx" (all sites> Gallaries > List templates). But not in "_catalogs/solutions/Forms/AllItems.aspx" for this reason the new template is not available in UI as well. Please guide me how do i do this programmatically.

staticvoid Main(string[] args)
    { 
      SPSecurity.RunWithElevatedPrivileges(delegate()
      {
        using (SPSite site = new SPSite("http://sp2010.mydomain.us"))
        {
          site.AllowUnsafeUpdates = true;
          using (SPWeb web = site.OpenWeb())
          {
            // SPDocumentLibrary list = (SPDocumentLibrary)web.Lists["Soutions Gallery"];
            SPDocumentLibrary list = (SPDocumentLibrary)web.Lists["List Template Gallery"];
            SPFolder myFolder = list.RootFolder;
            // ---
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            map.ExeConfigFilename = (@"C:\SPPackageWork\MyTemplates.config");
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            KeyValueConfigurationCollection siteTemplates = config.AppSettings.Settings; 
            // ---//Configuration config = (Configuration)WebConfigurationManager.OpenWebConfiguration("/appSettings"); //as System.Configuration.Configuration;//KeyValueConfigurationCollection siteTemplates = config.AppSettings.Get; // GetSiteTemplates("siteTemplatesSection");//NameValueCollection siteTemplates = ConfigurationManager.AppSettings;string[] templates = siteTemplates.AllKeys;
            //string[] templates = siteTemplates.AllKeys;foreach (string template in templates)
            {
              string siteTemplate = siteTemplates[template].Value;
              byte[] data = File.ReadAllBytes(siteTemplate);
              Hashtable metaData = new Hashtable();
              metaData.Add ("Language", "1033");
              metaData.Add("Title", template);
              web.ParserEnabled = true;
              SPFile file = myFolder.Files.Add(siteTemplates[template].Key, data, metaData, true);
              file.Update();
              web.ParserEnabled = false;
            }
          }
        }
      });
    }
 

Answer 6

Hello everyone,

By replacing the List Name from above code with following, I am able to upload my all .wsp solutions in Solution's Gallery from given folder.

SPDocumentLibrary list = (SPDocumentLibrary)web.Lists["Solution Gallery"];

 but all those solutions which I had uploaded programmatically are not activate, I am able to view them from UI but the activate/deactivate buttons are disable for these programmatically uploaded solutions. Where I am doing mistake. Please help.

 

 

Answer 7

Hello again,

I come up with some test that if now i upload a .wsp file manually it also won't let me activate/deactivate the newly uploaded file. The reason might be I am just copying an existing solution.wsp file and replacing with solution1.wsp which might be read internally by sharePoint and match its LCID with existing uploaded one and hence it won't let you activate it anymore. Is that correct?

Further I tried by removing exiting original uploaded template using UI and then uploaded programmatically using above code. The activagte option was not available from UI for the same solution file, then I tried the same solution uploading by UI, it gave me activate option. so I finally concluded that the given code above has some bug or i am doing wrong way to upload the solution. Also I don't know how can I activate solution  programmatically. Please help !!!

My question is now close to following post:

http://social.msdn.microsoft.com/Forums/en/sharepoint2010general/thread/9bf5e5a4-1d9f-4fc3-ad0f-52b8be35d053

The guy is able to activate but its not availabe until it has been manually deactivated and activated back using UI. What is the issue with code then?

from this link above I tried to activate the solution but getting error "Value cannot be null. Parameter name: g". I don't understand where i am doing wrong in this code?

SPUserSolution userSolution = site.Solutions.Add(file.Item.ID)
I also tried this way http://blogs.msdn.com/b/manuj/archive/2010/06/09/manipulating-sharepoint-2010-user-solutions-programmatically.aspx no luck ....
 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter