Bundle code examples for the Core Service
This topic contains a number of code samples for programmatically working with Bundles using the Core Service.
Basic code header
To provide some context, here is the class definition for a class that could contain the methods below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Tridion.ContentManager.CoreService.Client;
namespace MyOrganization.ContentManager.Tests
{
public class CodeSample
{
CoreServiceClient _CoreServiceClient = new CoreServiceClient();
ReadOptions _ReadOption = new ReadOptions()
{
LoadFlags = CoreService.Client.LoadFlags.None
};
// Methods listed below go here
}
}
Creating a Bundle
The following code creates a Bundle.
public VirtualFolderData CreateBundle()
{
SchemaData bundleSchema = (SchemaData) _CoreServiceClient.Read("tcm:1-78-8", _ReadOption);
SchemaData virtualFolderTypeSchema =
_CoreServiceClient.GetVirtualFolderTypeSchema(@"http://www.sdltridion.com/ContentManager/Bundle");
TcmUri folderId = new TcmUri(100, ItemType.Folder, 1);
VirtualFolderData bundle = new VirtualFolderData()
{
Id = "tcm:0-0-0",
Title = "Test Bundle Title",
Description = "Test Bundle Description",
MetadataSchema = new LinkToSchemaData()
{
IdRef = bundleSchema.Id
},
TypeSchema = new LinkToSchemaData()
{
IdRef = virtualFolderTypeSchema.Id
},
LocationInfo = new LocationInfo()
{
OrganizationalItem = new LinkToOrganizationalItemData()
{
IdRef = folderId
}
}
};
bundle = (VirtualFolderData) _CoreServiceClient.Create(bundle, _ReadOption);
return bundle;
}
Adding items to a Bundle
The following code adds items to a Bundle.
public void AddItemsToBundle(TcmUri bundleId, params TcmUri[] itemsToAdd)
{
VirtualFolderData bundle =
(VirtualFolderData) _CoreServiceClient.Read(bundleId, _ReadOption);
XmlDocument bundleConfiguration = new XmlDocument();
bundleConfiguration.LoadXml(bundle.Configuration);
XmlNameTable nameTable = new NameTable();
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace("b", @"http://www.sdltridion.com/ContentManager/Bundle");
namespaceManager.AddNamespace("xlink", @"http://www.w3.org/1999/xlink");
XmlElement itemsElement =
bundleConfiguration.SelectSingleElement("/b:Bundle/b:Items", namespaceManager);
foreach (var repositoryLocalObject in itemsToAdd)
{
XmlElement itemElement =
itemsElement.OwnerDocument.CreateElement("Item", @"http://www.sdltridion.com/ContentManager/Bundle");
itemElement.SetXLinkAttribute("type", "simple");
XmlAttribute xmlAttr =
itemElement.OwnerDocument.CreateAttribute("xlink", "href", "http://www.w3.org/1999/xlink");
xmlAttr.Value = respositoryLocalObject;
itemElement.SetAttributeNode(xmlAttr);
itemsElement.AppendChild(itemElement);
}
_CoreServiceClient.Save(bundle, _ReadOption);
}
Removing items from a Bundle
The following code removes items from a Bundle.
public void RemoveItemsFromBundle(TcmUri bundleId, params TcmUri[] itemsToRemove)
{
VirtualFolderData bundle =
(VirtualFolderData) _CoreServiceClient.Read(bundleId, _ReadOption);
XmlDocument bundleConfiguration = new XmlDocument();
bundleConfiguration.LoadXml(bundle.Configuration);
XmlNameTable nameTable = new NameTable();
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace("b", @"http://www.sdltridion.com/ContentManager/Bundle");
namespaceManager.AddNamespace("xlink", @"http://www.w3.org/1999/xlink");
XmlElement itemsElement =
bundleConfiguration.SelectSingleElement("/b:Bundle/b:Items", namespaceManager);
foreach (var repositoryLocalObject in itemsToRemove)
{
XmlElement itemElement =
itemsElement.SelectSingleElement(
string.Format("b:Item[@xlink:href='{0}']", repositoryLocaObject), namespaceManager);
if (itemElement != null)
{
itemsElement.RemoveChild(itemElement);
}
}
_CoreServiceClient.Save(bundle, _ReadOption);
}