Skip to content
Akshay Sura
Akshay Sura's blog posts
  • Home
  • Join Sitecore Slack
  • Unofficial Sitecore 8 Training
    • Unofficial Sitecore 8 Training Webinar Series – Full Curriculum
    • Unofficial Sitecore 8 Training Webinar Series – Session 1 – Plan C
    • Unofficial Sitecore 8 Training Webinar Series – Session 2
    • Unofficial Sitecore 8 Training Webinar Series – Session 3
    • Unofficial Sitecore 8 Training Webinar Series – Session 4
    • Unofficial Sitecore 8 Training Webinar Series – Session 5
    • Unofficial Sitecore 8 Training Webinar Series – Session 6
    • Unofficial Sitecore 8 Training Webinar Series – Session 7
    • Unofficial Sitecore 8 Training Webinar Series – Session 8
    • Unofficial Sitecore 8 Training Webinar Series – Session 9
    • Unofficial In Person Sitecore Training

Posts

Exporting and Importing packages from Sitecore through Code using Sitecore API

August 3, 2011 Akshay Sura

If you ever needed to export or import Sitecore packages dynamically through code, this is the article for you. You could be integrating it into automated build process or simply trying to move items between two environments.

Here is the Export Code:

            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("master");
                Sitecore.Install.PackageProject document = new Sitecore.Install.PackageProject();

                document.Metadata.PackageName = "INSERT PACKAGE NAME";
                document.Metadata.Author = "INSERT AUTHOR NAME";

                Sitecore.Install.Items.ExplicitItemSource source = new Sitecore.Install.Items.ExplicitItemSource(); //Create source – source should be based on BaseSource
                source.Name = "INSERT SOURCE NAME";
                Sitecore.Data.Items.Item[] items = db.Items.Database.SelectItems("SPECIFY THE SITECORE ITEM PATH TO EXPORT FROM");

                foreach (Sitecore.Data.Items.Item item in items)
                {
                    source.Entries.Add(new Sitecore.Install.Items.ItemReference(item.Uri, false).ToString());
                }

                document.Sources.Add(source);
                document.SaveProject = true;

                //path where the zip file package is saved
                using (Sitecore.Install.Zip.PackageWriter writer = new Sitecore.Install.Zip.PackageWriter("/data/packages/"  + " INSERT ZIP FILE NAME " + DateTime.Now.Ticks.ToString() + ".zip"))
                {
                    Sitecore.Context.SetActiveSite("shell");

                    writer.Initialize(Sitecore.Install.Installer.CreateInstallationContext());

                    Sitecore.Install.PackageGenerator.GeneratePackage(document, writer);

                    Sitecore.Context.SetActiveSite("website");
                }

            }

Here is the Import Code:

                using (new SecurityDisabler())
                {
                    using (new ProxyDisabler())
                    {
                        using (new SyncOperationContext())
                        {
                            IProcessingContext context = new SimpleProcessingContext();
                            IItemInstallerEvents events = new DefaultItemInstallerEvents(new BehaviourOptions(InstallMode.Overwrite, MergeMode.Undefined));
                            context.AddAspect(events);
                            IFileInstallerEvents events1 = new DefaultFileInstallerEvents(true);
                            context.AddAspect(events1);

                            Sitecore.Install.Installer installer = new Sitecore.Install.Installer();
                            installer.InstallPackage(MainUtil.MapPath("INSERT PACKAGE PATH INCLUDING THE FILE NAME"), context);
                        }
                    }
                }

Tagged as: API, BehaviourOptions, CreateInstallationContext, DefaultItemInstallerEvents, document sources, environment, environments, ExplicitItemSource, export, exporting, Factory, GetDatabase, IFileInstallerEvents, import, importing, Master, meta, metadata, Model, PACKAGE, PackageGenerator, PackageProject, PackageWriter, Per, processing, project, ProxyDisabler, SaveProject, Security, SecurityDisabler, SES, SetActiveSite, shell, Simple, SimpleProcessingContext, Sitecore, sitecore api, sitecore packages, source, source document, source entries, source name, SPECIFY, SyncOperationContext

Posted in: DefaultItemInstallerEvents, document sources, environment, environments, ExplicitItemSource, Factory, GetDatabase, IFileInstallerEvents, import, importing, Master, metadata, Model, PACKAGE, PackageProject, PackageWriter, Per, processing, project, ProxyDisabler, SaveProject, Security, SES, SetActiveSite, Simple, SimpleProcessingContext, Sitecore, source, source document, source entries, source name, SPECIFY, SyncOperationContext Filed under: export, exporting, meta, PackageGenerator, SecurityDisabler, shell, sitecore, sitecore api, sitecore packages

Post navigation

← Sitecore Lucene Search Index
Sitecore: Publish Item with Sub-Items quicker! →

Archives

  • April 2018 (2)
  • March 2018 (1)
  • December 2017 (2)
  • October 2017 (2)
  • September 2017 (4)
  • June 2017 (4)
  • April 2017 (1)
  • January 2017 (3)
  • December 2016 (3)
  • November 2016 (2)
  • October 2016 (3)
  • September 2016 (2)
  • August 2016 (8)
  • July 2016 (5)
  • June 2016 (5)
  • May 2016 (3)
  • February 2016 (3)
  • October 2015 (2)
  • August 2015 (6)
  • June 2015 (3)
  • May 2015 (4)
  • April 2015 (1)
  • February 2015 (6)
  • January 2015 (1)
  • August 2014 (2)
  • July 2014 (2)
  • April 2014 (1)
  • December 2013 (1)
  • November 2013 (3)
  • October 2013 (1)
  • June 2012 (2)
  • August 2011 (1)
  • July 2011 (1)
  • September 2010 (1)

Tags

Continuous Integration Coveo customer CustomFolder database DI export exporting FileSystemWatcher Glass Mapper Globalization Gulp Habitat Helix ioc meta Microsoft mongodb Octopack Octopus Deploy PackageGenerator parameter PowerShell scheduling SecurityDisabler SIM simpleinjector sitecore sitecore 8 Sitecore 8 Upgrade sitecore api sitecore cms sitecore development Sitecore Instance Manager Sitecore MVC Sitecore Scheduler Sitecore Scheduling Sitecore training SitecoreUploadWatcher SiteCron SPE Tealium Training VSTS XSS

Recent Posts

  • Sitecore Precision Scheduling with SiteCron 3.0.4 released in powerful ways!
  • Sitecore Precision Scheduling with SiteCron 3.0.2 released in powerful ways!
  • Sitecore Precision Scheduling with SiteCron 3.0 released in powerful ways!
  • TIHIDI: Sitecore Scheduling with SiteCron 2.1.8 in powerful ways!
  • Sitecore Precision Scheduling with SiteCron 2.1.8 released in powerful ways!
This is a personal blog. Any views or opinions represented in this blog are my own and do not represent those of people, institutions or organizations that the I may or may not be associated with in professional or personal capacity including past, current and future employers, unless explicitly stated.

I live and breathe Sitecore!
  • twitter
  • twitter
  • linkedin
  • linkedin
  • github
  • github
  • rss
  • rss
Copyright © 2023 Akshay Sura — Primer WordPress theme by GoDaddy