0

How do I publish items using sitecore powershell script? The catch here is I only want to publish the items which got added as a part of latest package install. So I want a single powershell script which:

  1. Get the list of items which got created/updated as a part of latest package install
  2. Publish those items.
1
  • Not sure I tried this yet but maybe you can extract the project XML file from the package and read all the item paths or ids. Then you have a record of what will change. Commented Aug 30, 2020 at 2:44

1 Answer 1

1

Doing this via Sitecore Powershell will be a tedious task. One possible solution is to hook into the packageinstall:items:ended event.

This event will allow you to retrieve all the ItemUri, hence you will have the item ids. Below is the code snippet on how to implement the event listener.

Code Snippet

public class ItemPackageEvent
{
    public void OnPackageInstallItemsEndHandler(object sender, EventArgs e)
    {
        if (e == null)
        {
            return;
        }

        SitecoreEventArgs sitecoreEventArgs = e as SitecoreEventArgs;

        if (sitecoreEventArgs == null || sitecoreEventArgs.Parameters == null || sitecoreEventArgs.Parameters.Length != 1)
        {
            return;
        }

        InstallationEventArgs parameter = sitecoreEventArgs.Parameters[0] as InstallationEventArgs;
        
        // The paramter.ItemsToInstall provides the list of items together with the item id, language that have been installed on the CMS
        // Check for null in case no item is available.
        if (parameter == null || parameter.ItemsToInstall == null)
        {
            return;
        }

        // Implement here your code for the publishing.
    }
}

Config File Patch

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
  <sitecore>
    <events>
        <event name="packageinstall:items:ended">
          <handler type="YourNamespace.ItemPackageEvent, YourAssemblyName" method="OnPackageInstallItemsEndHandler" />
        </event>
    </events>
  </sitecore>
</configuration>

Note that if ever you have already implemented your SPE script to trigger the publish, you can still use it in the code. All you need to do is to call the Powershell Script.

More information on how to call the Powershell Script via code can be found at the following StackOverflow question.

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.