How to create a folder structure in SDL Tridion 2011 SP1 using the main service

I am using Core Service at Tridion 2011. I want to create a folder structure and then create a component in this structure.

Example: Path to the folder structure: / ABCD / DEFG / aaaaa

If the folder exists, we do not need to create the folder. If this does not exist, we must create it and create a component in it.

I know how to create a component in a folder with a URI.

+6
source share
4 answers

Below is the code that I use when I need to get or create folders using the SDL Tridion CoreService. This is a simple recursive method that checks for the presence of the current folder. If it does not exist, it goes to GetOrCreate parent folder and so on until it finds an existing path. Along the recursion path, it simply creates new folders relative to their immediate parent.

Note : this method does not validate the input of folderPath . Rather, it suggests that it represents a valid path.

 private FolderData GetOrCreateFolder(string folderPath, SessionAwareCoreServiceClient client) { ReadOptions readOptions = new ReadOptions(); if (client.IsExistingObject(folderPath)) { return client.Read(folderPath, readOptions) as FolderData; } else { int lastSlashIdx = folderPath.LastIndexOf("/"); string newFolder = folderPath.Substring(lastSlashIdx + 1); string parentFolder = folderPath.Substring(0, lastSlashIdx); 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; } } 
+6
source

I would use IsExistingObject - going through a WebDAV URL - to see if the folder already exists. If it returns false, you can continue and create a folder.

Edit: Here are some quick pseudo codes ...

 string parentFolderId = @"/webdav/MyPublication/Building%20Blocks"; var client = GetCoreServiceClient(); if (!client.IsExistingObject(parentFolderId + "/AAA")) { var folder = client.GetDefaultData(2, parentFolderId); folder.Title = "AAA"; client.Save(folder); // Create the other folders and components here } 
+5
source

This is what we used in one of our projects to create folders for the path.

 static FolderData GetOrCreateFolder(List<string> folders, FolderData root, SessionAwareCoreService2010Client client) { var filter = new OrganizationalItemItemsFilterData(); filter.ItemTypes = new [] { ItemType.Folder }; var items = client.GetListXml(root.Id, filter). Elements(TRIDION_NAMESPACE + "Item"); foreach (var element in items) { if (folders.Count == 0) { break; // break from foreach } var titleAttribute = element.Attribute("Title"); var idAttribute = element.Attribute("ID"); if (titleAttribute != null && titleAttribute.Value == folders[0] && idAttribute != null) { // folder exists FolderData fd = client.Read(idAttribute.Value, EXPANDED_READ_OPTIONS) as FolderData; // We just took care of this guy, remove it to recurse folders.RemoveAt(0); return GetOrCreateFolder(folders, fd, client); } } if (folders.Count != 0) { //Folder doesn't exist, lets create it and return its folderdata var newfolder = new FolderData(); newfolder.Title = folders[0]; newfolder.LocationInfo = new LocationInfo { OrganizationalItem = new LinkToOrganizationalItemData { IdRef = root.Id } }; newfolder.Id = "tcm:0-0-0"; var folder = client.Create(newfolder, EXPANDED_READ_OPTIONS) as FolderData; folders.RemoveAt(0); if (folders.Count > 0) { folder = GetOrCreateFolder(folders, folder, client); } return folder; } return root; } 

So you call it something like this:

 var root = client.Read("tcm:1-1-2", null) as FolderData; var pathParts = "/ABCD/DEFG/aaaaa".Trim('/').Split('/').ToList(); var folder = GetOrCreateFolder(pathParts, root, client); 
+3
source

To create a folder, use the following code as a sample ... You will need to check if the folder exists, of course, this code shows how to create a folder in the folder

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using CoreWebService.ServiceReference1; namespace CoreWebService { class CoreWebServiceSamples { public static void createFolder() { string folderWebDavUrl = "/webdav/020%20Content/Building%20Blocks/Content/wstest"; CoreServicesUtil coreServicesUtil = new CoreServicesUtil(); FolderData folderData = coreServicesUtil.getFolderData(folderWebDavUrl); FolderData folderDataChild = folderData.AddFolderData(); folderDataChild.Title = "childFolder"; folderDataChild = (FolderData)coreServicesUtil.coreServiceClient.Save(folderDataChild, coreServicesUtil.readOptions); coreServicesUtil.coreServiceClient.Close(); } } } 

Here is the code for the methods referenced .... using the system; using System.Collections.Generic; using System.Linq; using System.Text; using CoreWebService.ServiceReference1; using CoreWebService.Properties; using System.Xml; using System.Xml.Serialization;

 namespace CoreWebService { public class CoreServicesUtil { public CoreService2010Client coreServiceClient; public ReadOptions readOptions; /// <summary> /// /// </summary> public CoreServicesUtil() { this.coreServiceClient = new CoreService2010Client("basicHttp_2010"); this.readOptions = new ReadOptions(); } public FolderData getFolderData(string tcmuri) { FolderData folderData = (FolderData)coreServiceClient.Read(tcmuri, readOptions); return folderData; } } public static class CoreServicesItemCreator { /** * <summary> * Name: AddFolder * Description: returns a new Folder Data created in the folder Data * </summary> **/ public static FolderData AddFolderData(this FolderData folderData) { FolderData childFolder = new FolderData(); childFolder.LocationInfo = getLocationInfo(folderData); childFolder.Id = "tcm:0-0-0"; return childFolder; } } } 
+2
source

Source: https://habr.com/ru/post/911847/


All Articles