简体   繁体   English

如何使用Core Service在SDL Tridion 2011 SP1中创建文件夹结构

[英]How to create folder structure in SDL Tridion 2011 SP1 using Core Service

I am using the Core Service on Tridion 2011. I want to create a folder structure, and then create a component in that structure. 我在Tridion 2011上使用Core Service。我想创建一个文件夹结构,然后在该结构中创建一个组件。

Example: Path of folder structure: /ABCD/DEFG/aaaaa 示例:文件夹结构的路径:/ ABCD / DEFG / aaaaa

If the folder exists, we need not create folder. 如果该文件夹存在,我们不需要创建文件夹。 If it doesn't exist we have to create it and create component in it. 如果它不存在,我们必须创建它并在其中创建组件。

I know how to create the component in a folder having URI. 我知道如何在具有URI的文件夹中创建组件。

The following is the code I use when I need to Get or Create Folders with SDL Tridion's CoreService. 以下是我需要使用SDL Tridion的CoreService获取或创建文件夹时使用的代码。 It's a simple recursive method that checks for the existence of the current folder. 这是一个简单的递归方法,用于检查当前文件夹是否存在。 If it doesn't exist, it goes into GetOrCreate the parent folder and so on until it finds an existing path. 如果它不存在,则进入GetOrCreate父文件夹,依此类推,直到找到现有路径。 On the way out of the recursion, it simply creates the new Folders relative to their immediate parent. 在离开递归的过程中,它只是创建相对于其直接父级的新文件夹。

Note : this method does not check the input folderPath . 注意 :此方法不检查输入folderPath Rather, it assumes 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;
    }
}

I would use IsExistingObject - passing in the WebDAV URL - to see if the Folder already exists. 我会使用IsExistingObject - 传入WebDAV URL - 来查看文件夹是否已经存在。 If it returns false, you can go ahead and create the folder. 如果返回false,则可以继续创建文件夹。

Edit: Here's some quick pseudo code... 编辑:这是一些快速伪代码......

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
}

This is what we used on one of our projects to create folders for a 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'd invoke it with 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);

For Create a folder use the following code as sample... You will have to check if the folder exists of course, this code shows how to create a folder within a 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 some code for the methods referenced .... using System; 以下是使用System引用的方法的一些代码; using System.Collections.Generic; 使用System.Collections.Generic; using System.Linq; 使用System.Linq; using System.Text; 使用System.Text; using CoreWebService.ServiceReference1; 使用CoreWebService.ServiceReference1; using CoreWebService.Properties; 使用CoreWebService.Properties; using System.Xml; 使用System.Xml; using System.Xml.Serialization; 使用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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM