我在Tridion 2011上使用Core Service。我想创建一个文件夹结构,然后在该结构中创建一个组件。
实施例: 文件夹结构路径:/ ABCD / DEFG / aaaaa
如果文件夹存在,我们无需创建文件夹。如果它不存在,我们必须创建它并在其中创建组件。
我知道如何在具有URI的文件夹中创建组件。
答案 0 :(得分:6)
以下是我需要使用SDL Tridion的CoreService获取或创建文件夹时使用的代码。这是一个简单的递归方法,用于检查当前文件夹是否存在。如果它不存在,则进入GetOrCreate父文件夹,依此类推,直到找到现有路径。在离开递归的路上,它只是相对于它们的直接父级创建新的文件夹。
注意:此方法不会检查输入folderPath
。相反,它假定它代表一个有效的路径。
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;
}
}
答案 1 :(得分:5)
我会使用IsExistingObject - 传入WebDAV URL - 来查看文件夹是否已存在。如果返回false,则可以继续创建文件夹。
编辑:这是一些快速伪代码......
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
}
答案 2 :(得分:3)
这是我们在其中一个项目中用于为路径创建文件夹的内容。
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;
}
所以你用这样的东西调用它:
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 :(得分:2)
对于创建文件夹,请使用以下代码作为示例... 当然,您必须检查文件夹是否存在,此代码显示如何在文件夹中创建文件夹
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();
}
}
}
以下是引用方法的一些代码.... 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Text; 使用CoreWebService.ServiceReference1; 使用CoreWebService.Properties; 使用System.Xml; 使用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;
}
}
}