Bundle code examples for TOM.NET
This topic contains a number of code samples for programmatically working with Bundles using TOM.NET.
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 Tridion.ContentManager.CommunicationManagement;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.Security;
using Tridion.ContentManager.Workflow;
namespace MyOrganization.ContentManager.Tests
{
public class CodeSample
{
Session _Session = new Session("User_Name");
// Methods below go here.
}
}
Creating a Bundle
The following code creates a Bundle.
public Bundle CreateBundle()
{
Schema bundleSchema = (Schema) _Session.GetObject("tcm:1-78-8");
// Content Manager URI of the Folder in which to create the Bundle
TcmUri folderId = new TcmUri(100, ItemType.Folder, 1);
Bundle bundle = new Bundle(_Session, folderId);
{
Title = "Test Bundle Title",
Description = "Test Bundle Description",
MetadataSchema = bundleSchema
};
bundle.Save();
return bundle;
}
Adding items to a Bundle
The following code adds a number of items to a Bundle.
public void AddItemsToBundle(TcmUri bundleId, params RepositoryLocalObject[] itemsToAdd)
{
Bundle bundle = (Bundle) _Session.GetObject(bundleId);
foreach (var repositoryLocalObject in itemsToAdd)
{
bundle.AddItem(repositoryLocalObject);
}
bundle.Save();
}
Removing items from a Bundle
The following code removes a number of items from a Bundle.
public void RemoveItemsFromBundle(TcmUri bundleId, params RepositoryLocalObject[] itemsToRemove)
{
Bundle bundle = (Bundle) _Session.GetObject(bundleId);
foreach (var repositoryLocalObject in itemsToRemove)
{
bundle.RemoveItem(repositoryLocalObject);
}
bundle.Save();
}