2014-01-11 47 views
0

我知道如何以编程方式添加或创建项目。我的疑问是如何以编程方式分配模板?如何在sitecore中动态地将模板分配给其他模板?

enter image description here

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"); 
+2

我认为这是坏主意编程继承模板。 什么情况下您需要这样的实施才能将开发人员和测试人员分配给MasterItem?为什么刚开始创建模板时,您只是从模板管理器分配? – 2014-01-11 10:14:46

+0

我同意。虽然我提供了一个答案,但我会质疑为什么会这么做。 –

+1

#TwentyGoto你的答案很好,但我不认为有人需要这样的实现。你想一次取消分配模板?什么发生在物品领域?所以#user1381319我认为你需要考虑其他的实现。 – 2014-01-11 10:24:28

回答

2

这似乎是一个相当奇怪的要求,我会建议你重新考虑,因为采取的做法最终会导致你的问题。

话虽如此,模板是像Sitecore中的所有其他项目,所以它应该是可能的。一旦你有MasterItem实例化,你应该能够添加东西到它的__Base template字段。

__Base template是一个多字段字段,所以该值存储为管道分隔的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

相关问题