public override void Execute(SC.Shell.Framework.Commands.CommandContext context)
        {
            if ((context.Items == null) || (context.Items.Length != 1))
                return;

            string currentUserName = Sitecore.Context.User.Profile.FullName; //Current User Full name.

            //Admin Account
            Sitecore.Security.Accounts.User scUser = Sitecore.Security.Accounts.User.FromName("sitecore\\admin", false);
            using (new Sitecore.Security.Accounts.UserSwitcher(scUser))
            {
                Sitecore.Data.Database db = Context.ContentDatabase;
                Sitecore.Install.PackageProject document = new Sitecore.Install.PackageProject();

                document.Metadata.PackageName = context.Items[0].Name;
                document.Metadata.Author = currentUserName;

                Sitecore.Install.Items.ExplicitItemSource source = new Sitecore.Install.Items.ExplicitItemSource();
                source.Name = context.Items[0].Name;

                List<Item> items = new List<Item>();

                items.Add(db.Items.Database.GetItem(context.Items[0].Paths.Path)); //Self Item
                //decorate item name with # otherwise query will break because of special character.
                var paths = SC.StringUtil.Split(context.Items[0].Paths.Path, '/', true).Where(p => p != null & p != string.Empty).Select(p => "#" + p + "#").ToList();
                string allChildQuery = string.Format("/{0}//*", SC.StringUtil.Join(paths, "/"));
                var children = db.Items.Database.SelectItems(allChildQuery);
                if (children != null && children.Length > 0)
                    items.AddRange(children); //Get children.

                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 will be saved
                string filePath = Sitecore.Configuration.Settings.DataFolder + "/packages/" + context.Items[0].Name + "_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss_fffffff") + ".zip";

                using (Sitecore.Install.Zip.PackageWriter writer = new Sitecore.Install.Zip.PackageWriter(filePath))
                {
                    Sitecore.Context.SetActiveSite("shell");

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

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

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

                Sitecore.Web.UI.Sheer.SheerResponse.Download(filePath);

            }
        }
Пример #2
0
        protected void btnExportUsersInList_Click(object sender, EventArgs e)
        {
            this.BuildUserList();
            Sitecore.Data.Database          db       = Sitecore.Configuration.Factory.GetDatabase("master");
            Sitecore.Install.PackageProject document = new PackageProject();
            document.Metadata.PackageName = "SitecoreUserExport-" + DateTime.Now.ToString("yyyy-MM-dd-hhmmss");
            document.Metadata.Author      = "Autogenerated by SitecoreExtension.SitecoreUserImporter";

            Sitecore.Install.Security.SecuritySource securitySource = new SecuritySource();
            securitySource.Name = "Users To Export";

            int count        = 1;
            int packageCount = 0;

            foreach (string key in usersByEmail.Keys)
            {
                UserData userData = usersByEmail[key];

                string userName = GetFullName(userData.Login);

                if (Sitecore.Security.Accounts.User.Exists(userName))
                {
                    securitySource.AddAccount(new AccountString(userName, AccountType.User));
                    packageCount++;
                }

                count++;
            }

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

            if (packageCount > 0)
            {
                //path where the zip file package is saved
                using (Sitecore.Install.Zip.PackageWriter writer = new Sitecore.Install.Zip.PackageWriter(this.userExportFilePath))
                {
                    Sitecore.Context.SetActiveSite("shell");
                    writer.Initialize(Sitecore.Install.Installer.CreateInstallationContext());
                    Sitecore.Install.PackageGenerator.GeneratePackage(document, writer);
                    Sitecore.Context.SetActiveSite("website");
                }

                divDebug.InnerHtml = string.Format("<h1>{0} Users packaged, file location: </h1>{1}", packageCount, this.userExportFilePath);
            }
            else
            {
                divDebug.InnerHtml = string.Format("<h1>Could not find any users</h1>");
            }
        }
Пример #3
0
        public void StartProcess(params object[] parameters)
        {
            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                Sitecore.Data.Database db = Factory.GetDatabase("master");
                var document = new Sitecore.Install.PackageProject
                {
                    Metadata = { PackageName = "package.update", Author = "Tim Ward" }
                };


                var source = new Sitecore.Install.Items.ExplicitItemSource {
                    Name = "source"
                };
                //Create source – source should be based on BaseSource
                var items = db.GetItem(parameters[0].ToString());

                foreach (Item project in items.Children)
                {
                    CopyAll(new DirectoryInfo(@"C:\SiteTemplate"), new DirectoryInfo(@"C:\Export\" + project.Name), project.Name);

                    foreach (var item in project.Axes.GetDescendants())
                    {
                        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 (var writer = new Sitecore.Install.Zip.PackageWriter(@"C:\Export\" + project.Name + "\\SiteName\\SiteEnvironment\\items\\" + " package.update " + 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");
                    }
                }
            }
        }
        public void StartProcess(params object[] parameters)
        {
            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                Sitecore.Data.Database db = Factory.GetDatabase("master");
                var document = new Sitecore.Install.PackageProject
                                   {Metadata = {PackageName = "package.update", Author = "Tim Ward"}};

                var source = new Sitecore.Install.Items.ExplicitItemSource {Name = "source"};
                    //Create source – source should be based on BaseSource
                var items = db.GetItem(parameters[0].ToString());

                foreach (Item project in items.Children)
                {
                    CopyAll(new DirectoryInfo(@"C:\SiteTemplate"), new DirectoryInfo(@"C:\Export\" + project.Name), project.Name);

                    foreach (var item in project.Axes.GetDescendants())
                    {
                        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 (var writer = new Sitecore.Install.Zip.PackageWriter(@"C:\Export\" + project.Name + "\\SiteName\\SiteEnvironment\\items\\" + " package.update " + 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");
                    }
                }
            }
        }
        /// <summary>
        /// Creates package
        /// </summary>
        /// <param name="contextItem">Item on wich we create package</param>
        /// <param name="fileName">File name of generating package</param>
        /// <param name="includeDescendants">If including descendant items</param>
        /// <returns>Generated package path</returns>
        protected String CreatePackage(Item contextItem, String fileName, bool includeDescendants)
        {
            String filePath = String.Empty;

            string currentUserName = Sitecore.Context.User.Profile.FullName;
            Sitecore.Security.Accounts.User scUser = Sitecore.Security.Accounts.User.FromName("sitecore\\admin", false);

            using (new Sitecore.Security.Accounts.UserSwitcher(scUser))
            {
                Sitecore.Data.Database db = Context.ContentDatabase;
                Sitecore.Install.PackageProject packageProject = new Sitecore.Install.PackageProject();

                packageProject.Metadata.PackageName = PackageName.Value;
                packageProject.Metadata.Author = currentUserName;

                Sitecore.Install.Items.ExplicitItemSource source = new Sitecore.Install.Items.ExplicitItemSource();
                source.Name = contextItem.Name;

                List<Item> items = new List<Item>();

                items.Add(db.Items.Database.GetItem(contextItem.Paths.Path));

                if (includeDescendants)
                {
                    var paths = Sitecore.StringUtil.Split(contextItem.Paths.Path, '/', true)
                        .Where(p => p != null & p != string.Empty)
                        .Select(p => "#" + p + "#")
                        .ToList();

                    String allChildQuery = string.Format("/{0}//*", Sitecore.StringUtil.Join(paths, "/"));

                    var children = db.Items.Database.SelectItems(allChildQuery);
                    if (children != null && children.Length > 0)
                    {
                        items.AddRange(children);
                    }
                }

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

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

                // - Path where the zip file package will be saved -
                filePath = Sitecore.Configuration.Settings.DataFolder + "/packages/" + fileName;

                using (Sitecore.Install.Zip.PackageWriter writer = new Sitecore.Install.Zip.PackageWriter(filePath))
                {
                    Sitecore.Context.SetActiveSite("shell");

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

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

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

            return filePath;
        }
        /// <summary>
        /// Creates package
        /// </summary>
        /// <param name="contextItem">Item on wich we create package</param>
        /// <param name="fileName">File name of generating package</param>
        /// <param name="includeDescendants">If including descendant items</param>
        /// <returns>Generated package path</returns>
        protected String CreatePackage(Item contextItem, String fileName, bool includeDescendants)
        {
            String filePath = String.Empty;

            string currentUserName = Sitecore.Context.User.Profile.FullName;

            Sitecore.Security.Accounts.User scUser = Sitecore.Security.Accounts.User.FromName("sitecore\\admin", false);

            using (new Sitecore.Security.Accounts.UserSwitcher(scUser))
            {
                Sitecore.Data.Database          db             = Context.ContentDatabase;
                Sitecore.Install.PackageProject packageProject = new Sitecore.Install.PackageProject();

                packageProject.Metadata.PackageName = PackageName.Value;
                packageProject.Metadata.Author      = currentUserName;

                Sitecore.Install.Items.ExplicitItemSource source = new Sitecore.Install.Items.ExplicitItemSource();
                source.Name = contextItem.Name;

                List <Item> items = new List <Item>();

                items.Add(db.Items.Database.GetItem(contextItem.Paths.Path));

                if (includeDescendants)
                {
                    var paths = Sitecore.StringUtil.Split(contextItem.Paths.Path, '/', true)
                                .Where(p => p != null & p != string.Empty)
                                .Select(p => "#" + p + "#")
                                .ToList();

                    String allChildQuery = string.Format("/{0}//*", Sitecore.StringUtil.Join(paths, "/"));

                    var children = db.Items.Database.SelectItems(allChildQuery);
                    if (children != null && children.Length > 0)
                    {
                        items.AddRange(children);
                    }
                }

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

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

                // - Path where the zip file package will be saved -
                filePath = Sitecore.Configuration.Settings.DataFolder + "/packages/" + fileName;

                using (Sitecore.Install.Zip.PackageWriter writer = new Sitecore.Install.Zip.PackageWriter(filePath))
                {
                    Sitecore.Context.SetActiveSite("shell");

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

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

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

            return(filePath);
        }