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
    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:


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.

1 Answer 1


You need to create the child folders in the their respective parent folders - For example: Once Content\Homepage\ folder is getting created, you need to create Folders - One, Two and Three inside it.

To achieve it you may want to write a recursive function (or something like that of your choice) where you need to get the Path of the Folder created, do a sub-string for child folders and accordingly create.

You may want to write something like this:

private FolderData GetOrCreateFolder(string folderPath, SessionAwareCoreServiceClient client)
    ReadOptions readOptions = new ReadOptions();

    if (client.IsExistingObject(folderPath))
        return client.Read(folderPath, readOptions) as FolderData;
        int lastIdx = folderPath.LastIndexOf("/");
        string newFolder = folderPath.Substring(lastIdx + 1);
        string parentFolder = folderPath.Substring(0, lastIdx);
        FolderData parentFolderData = GetOrCreateFolder(parentFolder, client);
        FolderData newFolderData = client.GetDefaultData(ItemType.Folder, parentFolderData.Id) as FolderData;
        newFolderData.Title = newFolder;

        return client.Save(newFolderData, readOptions) as FolderData;
  • Thanks Pankaj. I'll give this a try and get back to you soon. Apr 16, 2013 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.