CoreService PowerShell Script To Add/Remove Item To/From Bundle

This is the continuation of my previous blog. So the very next thing that I am going to share is how to add and remove items to/from Bundle.

In the same way I will share both the PowerShell as well as C# module of the script.

Let’s start with the PowerShell Version first –

So before I start here are the parameters that I have used in my script –

 

Parameter Description
BundleId Id of the Bundle (Where you want to add items)
ItemID Id of the item (That you want to add)

Code:

Process
{
$bundle = Get-TridionItem -id $BundleId;

$doc = New-Object System.Xml.XmlDocument;
$doc.LoadXml($bundle.Configuration)

$itemElem = $doc.CreateElement('Item', $BundleNamespace);
$itemElem.SetAttribute('href', $XLinkNamespace, $itemId) | Out-Null;

$rootElem = $doc.SelectSingleNode('/*/*');
$rootElem.appendChild($itemElem) | Out-Null;

$bundle.Configuration = $doc.InnerXml;
$client.Save($bundle, $readOptions);
}

Example command to use this code –

AddItemToBundle -BundleId "tcm:25-53475-8192" -ItemID "tcm:5-338-16"

In case you have list of items to add into the Bundle at a same time, then you can use script like below –

FOREACH($item in $ItemList)

{

AddItemToBundle -BundleId ‘tcm:25-53475-8192' -ItemID $item

Write-Host $item “Added Successfully” -ForegroundColor Yellow

}

Here is the code for C# Module:

public static void AddItemToBundle(string bundleId, string itemId)
{
CoreServiceClient _CoreServiceClient = Utility.GetCoreServiceSettings();
ReadOptions _readOption = new ReadOptions();
VirtualFolderData bundle = (VirtualFolderData)_CoreServiceClient.Read(bundleId, _readOption);
XmlDocument bundleConfiguration = new XmlDocument();
bundleConfiguration.LoadXml(bundle.Configuration);

XmlNameTable nameTable = new NameTable();
nameTable = bundleConfiguration.NameTable;
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace("b", @"http://www.sdltridion.com/ContentManager/Bundle");
namespaceManager.AddNamespace("xlink", @"http://www.w3.org/1999/xlink");

XmlNode itemsElement = bundleConfiguration.SelectSingleNode("/b:Bundle/b:Items", namespaceManager);

XmlElement itemElement = itemsElement.OwnerDocument.CreateElement("Item", @"http://www.sdltridion.com/ContentManager/Bundle");
XmlAttribute xmlAttr = itemElement.OwnerDocument.CreateAttribute("xlink", "href", "http://www.w3.org/1999/xlink");
xmlAttr.Value = itemId;
XmlAttribute xmlAttr1 = itemElement.OwnerDocument.CreateAttribute("xlink", "type", "http://www.w3.org/1999/xlink");
xmlAttr1.Value = "simple";
itemElement.SetAttributeNode(xmlAttr);
itemElement.SetAttributeNode(xmlAttr1);
itemsElement.AppendChild(itemElement);
bundle.Configuration = bundleConfiguration.InnerXml;
_CoreServiceClient.Save(bundle, _readOption);
}

You can find both the code to add an item to bundle here at one place.

Similarly to remove items from Bundle, same set of Parameters has been used –

Parameter Description
BundleId Id of the Bundle (From where you want to remove items)
ItemID Id of the item (That you want to remove)

 

Code:

Process
{
$bundle = $client.Read($BundleId, $null);
$item = $client.Read($ItemID, $null);
$doc = New-Object System.Xml.XmlDocument;
$doc.LoadXml($bundle.Configuration)

$nodeList = $doc.GetElementsByTagName('Items')
FOREACH($node in $nodeList.ChildNodes){
FOR($count=0; $count -lt $node.Attributes.Count; $count++)
{
#need to put this condition as attribute names are different for component and page
if($node.Attributes[$count].Name -like "*:href")
{
$value = $node.Attributes[$count].Value;
}
}
if($value -eq $item.Id)
{
$nodeList.RemoveChild($node)
}
}

$bundle.Configuration = $doc.InnerXml;
$client.Save($bundle, $readOptions);
}

To use this code here is the command line for example –

RemoveItemFromBundle -BundleId $bundleid -ItemID "tcm:5-338-16"

In case you have list of items to add into the Bundle at a same time, then you can use script like below –

FOREACH ($item in $ItemList)

{

RemoveItemFromBundle -BundleId ‘tcm:25-53475-8192' -ItemID $item

Write-Host $item “Added Successfully” -ForegroundColor Yellow

}

Here is the code for C# Module:

public static void RemoveItemFromBundle(string bundleId, string itemId)
{
CoreServiceClient _CoreServiceClient = Utility.GetCoreServiceSettings();
ReadOptions _readOption = new ReadOptions();
//string bundleId = "tcm:5-1076-8192";
VirtualFolderData bundle = (VirtualFolderData)_CoreServiceClient.Read(bundleId, _readOption);
XmlDocument bundleConfiguration = new XmlDocument();
bundleConfiguration.LoadXml(bundle.Configuration);

XmlNameTable nameTable = new NameTable();
nameTable = bundleConfiguration.NameTable;
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace("b", @"http://www.sdltridion.com/ContentManager/Bundle");
namespaceManager.AddNamespace("xlink", @"http://www.w3.org/1999/xlink");

XmlNode itemsElement = bundleConfiguration.SelectSingleNode("/b:Bundle/b:Items", namespaceManager);
XmlNode itemElement= null;
foreach (XmlNode node in itemsElement)
{
if(node.Attributes[@"xlink:href"] != null && node.Attributes[@"xlink:href"].Value == itemId)
{
itemElement = node;
}
}
// XmlNode itemElement = itemsElement.SelectSingleNode(string.Format("Item[@xlink:href='{0}']", itemId), namespaceManager);
if (itemElement != null)
{
itemsElement.RemoveChild(itemElement);
}
bundle.Configuration = bundleConfiguration.InnerXml;
_CoreServiceClient.Save(bundle, _readOption);
}
}

You can find both the code to remove an item from bundle here at one place.

A special thanks to Tridion Powershell Module Guru Peter Kjaer for helping me to refactor my script.

If you like it, then start using Tridion Powershell Module and don't forget to contribute. Cheers!!