Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to add items to Bundle
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);
}
Function AddItemToBundle
{
[CmdletBinding()]
Param (
# Bundle ID
[Parameter(Mandatory=$true)]
[string]$BundleId,
# Item ID .
[Parameter(Mandatory=$true)]
[string]$ItemID
)
Begin
{
$client = Get-TridionCoreServiceClient -Verbose:($PSBoundParameters['Verbose'] -eq $true);
$readOptions = New-Object Tridion.ContentManager.CoreService.Client.ReadOptions;
}
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);
}
End
{
Close-TridionCoreServiceClient $client;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment