簡體   English   中英

如何在sitecore中動態地將模板分配給另一個模板?

[英]How to assign templates to another template dynamically in sitecore?

我知道如何以編程方式添加或創建項目。 我懷疑如何以編程方式分配模板如下?

在此輸入圖像描述

Sitecore.Data.Database masterDatabase = Sitecore.Configuration.Factory.GetDatabase("master");
//This is master item.I want to add  some items in this template.like below programatically
Item MasterItem = masterDatabase.GetItem("/sitecore/templates/DynamicTemplates/Employees");

//This is folder which has two templates[Developers,Tester].I want to assign these two as in image programatically.
Item GetAllTemplates = masterDatabase.GetItem("/sitecore/templates/DynamicTemplates/Team");

這似乎是一個非常奇怪的請求,我建議你重新考慮,因為采取方法最終會導致你的問題。

話雖如此,模板是像Sitecore中的其他所有項目所以它應該是可能的。 實例化MasterItem您應該能夠在其__Base template字段中添加內容。

__Base template是一個Multlist字段,因此該值存儲為一系列管道分隔的GUID。

使用你的變量:

var baseTemplates = GetAllTemplates.Children;
var baseTemplateIds = baseTemplates.Select(item => item.ID.ToString());
var fieldValue = String.Join("|",baseTemplateIds);

using (new Sitecore.SecurityModel.SecurityDisabler())
{
    try
    {
        MasterItem.Editing.BeginEdit();
        MasterItem["__Base template"] = fieldValue;
    }
    finally
    {   
        MasterItem.Editing.EndEdit();
    }
}

如果您不熟悉以編程方式編輯項目,請查看此處:

http://learnsitecore.cmsuniverse.net/en/Developers/Articles/2009/06/ProgramaticallyItems2.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM