I'm using the Tridion Core Service to create new folders whilst trying to keep the same structure.
I have the following code:
using (CoreServiceClient client = new CoreServiceClient("basicHttp"))
{
FolderData folderData = (FolderData)client.Read(folderId, new ReadOptions());
string currentFolderId = string.Empty;
currentFolderId = folderData.LocationInfo.OrganizationalItem.IdRef;
// Create a folder in new publicaiton
FolderData newFolderData = new FolderData();
newFolderData.Id = "tcm:0-0-0";
newFolderData.Title = folderData.Title;
// Setup metadata, only applies if exists
newFolderData.MetadataSchema = new LinkToSchemaData
{
IdRef = folderData.MetadataSchema.IdRef.Replace("363", "419") // This changes the tcm Id from tcm:0-363-1 to tcm:0-419-1 i.e. my target publication
};
// Setup location
newFolderData.LocationInfo = new LocationInfo
{
OrganizationalItem = new LinkToOrganizationalItemData
{
IdRef = folderData.LocationInfo.OrganizationalItem.IdRef.Replace(currentFolderId, "tcm:344-16287-2")
}
};
// Apply old metadata to new folder
newFolderData.Metadata = folderData.Metadata;
// Create the folder
try
{
newFolderData = (FolderData)client.Create(newFolderData, new ReadOptions());
}
catch (Exception ex)
{
// Catch exception here
}
}
My code works and the folders are created but the paths are wrong. For example in my source publication I have the following folder structure:
Content\Homepage\
Inside that I have three folders; One, Two and Three. When I run my code it doesn't keep this structure it just creates all of the folders under the the tcm Id of the folder I provide.
My question is, how can I do the above and keep the same folder structure? One thing that may be a problem is if the code tries to create a folder in a location where the "parent" folder it lives in hasn't been created yet.
Your thoughts would be much appreciated.