Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove Item From Bundle
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);
}
}
Function Remove-TridionItemFromBundle
{
[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 = $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);
}
End
{
Close-TridionCoreServiceClient $client;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment