0

I would like to create package of one single content item using Sitecore PowerShell Extensions. While creating the package, I would like to include all the referenced items in the package. Is there any script available to solve this task?

I know only the content item path, in that I need to include referenced template, media,etc.

1 Answer 1

4

If you like to add the referenced items into your package using PowerShell then you need to use Get-ItemReference to get all the referenced items of the item that you want to include in your package. And then you need to loop through all the items to put them into your package and then the package will be created with the referenced items that you need. So the code should be something like this.

# Pass your item ID 
$items = Get-ItemReference -ID "{56305117-8C9C-4C08-96D5-BB97A8E27419}"

$package = New-Package "Sitecore Package";

# Set package metadata
$package.Sources.Clear();

$package.Metadata.Author = "Sumit";
$package.Metadata.Publisher = "Sumit";
$package.Metadata.Version = "1.0";
$package.Metadata.Readme = 'Readme Text'

# Loop on all the related items and add into the package
ForEach ($item in $items) {

    $source = $item | New-ExplicitItemSource -Name 'Your Page Name' -InstallMode Overwrite
    $package.Sources.Add($source);
}

# Save package
Export-Package -Project $package -Path "$($package.Name)-$($package.Metadata.Version).zip" -Zip

# Offer the user to download the package
Download-File "$SitecorePackageFolder\$($package.Name)-$($package.Metadata.Version).zip"

Hope this will help.

Some useful links.

How to create a package.

https://doc.sitecorepowershell.com/appendix/packaging/new-package

Get item references.

https://doc.sitecorepowershell.com/appendix/common/get-itemreference

Get Item Renderings

https://doc.sitecorepowershell.com/working-with-items/item-renderings

3
  • Is it take Datasource reference content items and it's template items?
    – Astle
    Jun 30 at 5:26
  • It will give you all the items that return by the Get-ItemReference function. Like template, rendering, media items etc. Also if you want to get any particular item then you can extend this logic to get items and keep them into a variable and then loop through this and add it to your Sitecore Package. Jun 30 at 6:31
  • @Astle If you want to get only the renderings set on an item then you need to follow this article doc.sitecorepowershell.com/working-with-items/item-renderings and then write logic to create loop and then add it to your Sitecore package. Jun 30 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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