1

I have written code that creates packages in Sitecore ver 8 and uploads and publishes in sitecore 9. The code is done in C# create package and installing package and publishing the package. Creating a package code was done with the help of using this link https://hishaamn.wordpress.com/2016/11/07/sitecore-create-package-programmatically/

installing a package was also done searching code in stack exchange. code below

global::Sitecore.Context.SetActiveSite("shell");
            using (new SecurityDisabler())
            {
                using (new SyncOperationContext())
                {
                    using (new EventDisabler())
                    {
                        var db = Factory.GetDatabase("master");
                        using (var dbSwitcher = new DatabaseSwitcher(db))
                        {
                            try
                            {
                                global::Sitecore.Install.Framework.IProcessingContext context = new global::Sitecore.Install.Framework.SimpleProcessingContext();

                                global::Sitecore.Install.Items.IItemInstallerEvents events =
                                    new global::Sitecore.Install.Items.DefaultItemInstallerEvents(
                                    new global::Sitecore.Install.Utils.BehaviourOptions(global::Sitecore.Install.Utils.InstallMode.Merge,
                                    global::Sitecore.Install.Utils.MergeMode.Merge));

                                context.AddAspect(events);
                                global::Sitecore.Install.Files.IFileInstallerEvents events1 = new global::Sitecore.Install.Files.DefaultFileInstallerEvents(true);
                                context.AddAspect(events1);
                                var inst = new global::Sitecore.Install.Installer();

                                ConfigurationManager.AppSettings.Set("MYImportRunning", "true");
                                global::Sitecore.Diagnostics.Log.Info("MY Import: Install Package start", this);
                                inst.InstallPackage(global::Sitecore.MainUtil.MapPath(path), context);
                                global::Sitecore.Diagnostics.Log.Info("My Import: Install Package end", this);
                                ConfigurationManager.AppSettings.Set("My.Running", "false");
                            }
                            catch (Exception e)
                            {
                                global::Sitecore.Diagnostics.Error.LogError(e.Message + e.StackTrace);
                            }
                        }
                    }
                }
            }


 private void PublishLocations()
        {
            var publishSetting = global::Sitecore.Configuration.Settings.GetSetting("My.ImportPublish");

            bool publish;

            bool.TryParse(publishSetting, out publish);
            if (publish)
                PublishingHelper.PublishItem("/sitecore/content/Home/Pages", PublishMode.Smart, true, true);
        }

The problem I am facing is, In sitecore 8 for example, I have a page Apple, and that page has 4 versions(ver1, ver2, ver3, ver4). Version 4 being the latest version. When the package is created it gets the latest 4th version. In sitecore 9 the same page Apple has 5 versions. When the package is installed and published it only updates the fourth version being the latest version. The 5 version is not updated because the 5 version is not there in sitecore 8 package. My question is, is there a way when the package is installed and published in sitecore 9, it updates the latest versions of the page in sitecore 9 or is there any other way of doing this.

2 Answers 2

1

This happens because your code installs packages with the Merge-Merge option:

new global::Sitecore.Install.Utils.BehaviourOptions(
    global::Sitecore.Install.Utils.InstallMode.Merge,
    global::Sitecore.Install.Utils.MergeMode.Merge));

This option instructs Sitecore to replace only matching items and versions from the package, for example if there is version 4 in the package, it will overwrite fields in the version 4 of the target item.

If you want to keep the existing item version and add the packaged content as a new version, then you should use the Merge-Append option:

new global::Sitecore.Install.Utils.BehaviourOptions(
    global::Sitecore.Install.Utils.InstallMode.Merge,
    global::Sitecore.Install.Utils.MergeMode.Append));

In your example, it will keep the existing version 5 and create a version 6 with content from the package, so installed content will become the latest version.

0

I agree with Anna's answer and addon to that, if you want to update the latest version (version 5) with the current condition Merge.

new global::Sitecore.Install.Utils.BehaviourOptions(
    global::Sitecore.Install.Utils.InstallMode.Merge,
    global::Sitecore.Install.Utils.MergeMode.Append));

You have to prepare a package with that version (version 5) and try to install it with the `Merge option.

Installation Options are: enter image description here

Reference: Installation Options

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.