CoreService PowerShell Script To Create Bundle/Bundles

As I decided to embark on my PowerShell scripting journey and strolling through some new ideas, I chanced upon this requirement where I need to create few bundles and do some basic operation on that.

The plan was to create the script extensively so that everybody can use it. I have started with some basic operations like create bundle/bundles through PowerShell coreservice module. Though I have mentioned about the PowerShell module only, but I considered to keep C# version as well, so that anyone can pick as per their preference –

Here is the PowerShell Version –

To create the Bundle we have used below Parameter –

Parameter Description
Title Title of the Bundle
Description Description Of the Bundle
BundleSchemaId Id of the Bundle Schema (Based on which the Bundle will be created)
Parent Id of the Folder (Where you want to place the Bundle)

Code-

Process

{

$readOptions = New-Object Tridion.ContentManager.CoreService.Client.ReadOptions;

$bundleSchema = $BundleSchemaId;

$folder = $Parent;

$virtualFolderTypeSchema = $client.GetVirtualFolderTypeSchema("http://www.sdltridion.com/ContentManager/Bundle");

$bundle = New-Object Tridion.ContentManager.CoreService.Client.VirtualFolderData;

$MetadataSchema = New-Object Tridion.ContentManager.CoreService.Client.LinkToSchemaData;

$TypeSchema = New-Object Tridion.ContentManager.CoreService.Client.LinkToSchemaData;

$OrganizationalItem = New-Object Tridion.ContentManager.CoreService.Client.LinkToOrganizationalItemData;

$LocationInfo = New-Object Tridion.ContentManager.CoreService.Client.LocationInfo;

$MetadataSchema.IdRef = $bundleSchema;

$TypeSchema.IdRef = $virtualFolderTypeSchema.Id;

$OrganizationalItem.IdRef = $folder;

$LocationInfo.OrganizationalItem = $OrganizationalItem;

$bundle.Id = "tcm:0-0-0";

$bundle.Title = $Title;

$bundle.Description = $Description;

$bundle.MetadataSchema = $MetadataSchema;

$bundle.TypeSchema = $TypeSchema;

$bundle.LocationInfo = $LocationInfo;

$bundle = $client.Create($bundle,$readOptions);

return $bundle.Id;

}

Example command to use this code -

$bundleid =  New-TridionBundle -Title $title -Description $Description -BundleSchemaId "tcm:5-403-8" -Parent $newFolder.Id

Write-Host "New Bundle Id :" $bundleid

In my case I have to create a no. of Bundles at the same time, so I have garnished my script like below –

// Here I am creating a new folder to store all the bundles at the same place

$newFolder = New-TridionItem -ItemType 2 -Parent tcm:5-27-2 -Name Bundles Write-Host "Folder Id: "

$newFolder.Id Write-Host "====================="

$count = 10;

for($i=0; $i -le $count;$i++)

{

$title = "My New Bundle ” + $i;

$Description = "Bundle along with updated component"

$bundleid =  New-TridionBundle -Title $title -Description $Description -BundleSchemaId "tcm:5-403-8" -Parent $newFolder.Id

Write-Host "New Bundle Id" $bundleid

Write-Host "====================="

}

As I mentioned above here is the code for C# Module:

public VirtualFolderData CreateBundle()
{
SchemaData bundleSchema = (SchemaData) coreServiceClient.Read("tcm:5-403-8", new 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, new ReadOption());
return bundle;
}

 

You can find both the code here at one place. Hope you like this!

Cheers & Stay tuned to get the Coreservice PowerShell script to Add/Remove an item into/from Bundle.