以编程方式在sitecore中的模板中添加新字段

时间:2015-06-16 09:50:36

标签: sitecore sitecore7

function addItem(){ $('#taskAccordion').accordion('add',{ title: 'Perl', selected: false, content:'This is Perl.' }); } 中,是否可以通过编程方式在模板中添加新字段?

我有一个模板" Sitecore",在此模板中我想添加一个字段" DictionaryName"其类型" Newname"。

3 个答案:

答案 0 :(得分:1)

您可以从项目中以编程方式访问模板,然后将一个项目添加到此模板。该模板是一个常见的项目儿童。

答案 1 :(得分:1)

我为你编写并测试了这段代码 - 它在我的机器上运行完美,并在指定的模板中创建了新的单行字段。这是方法:

    private void AddFieldToTemplate(string fieldName, string tempatePath)
    {
        const string templateOftemplateFieldId = "{455A3E98-A627-4B40-8035-E683A0331AC7}";

        // this will do on your "master" database, consider Sitecore.Context.Database if you need "web"
        var templateItem = Sitecore.Configuration.Factory.GetDatabase("master").GetItem(tempatePath);
        if (templateItem != null)
        {
            var templateSection = templateItem.Children.FirstOrDefault(i => i.Template.Name == "Template section");
            if (templateSection != null)
            {
                var newField = templateSection.Add(fieldName, new TemplateID(new ID(templateOftemplateFieldId)));
                using (new EditContext(newField))
                {
                    newField["Type"] = "Text"; // text stands for single-line lext field type
                }
            }
            {
                // there are no template sections here, you may need to create one. template has only inherited fields if any
            }
        }
    }

以下是用法 - 第一个字符串参数是新字段的名称,第二个是您正在使用的数据库中模板路径的字符串值:

AddFieldToTemplate("New Single Line Field", "/sitecore/templates/Sample/Sample Item");

将“Sample Item”模板替换为模板路径,并设置要添加的所需字段名称。另外不要忘记使用命名空间:

using Sitecore;
using Sitecore.Data;
using Sitecore.Data.Items;

希望这有帮助!

答案 2 :(得分:0)

为您编写了此示例。

<块引用>

想了解更多https://doc.sitecore.com/legacy-docs/SC71/data-definition-api-cookbook-sc70-a4.pdf

public JsonResult CreateTemplate()
    {
        try
        {
            using(new SecurityDisabler())
            {
                ///Get Database
                Database master = Sitecore.Configuration.Factory.GetDatabase("master");

                /// Every node in content tree ia an Item. Ex- Templates,Field, Item, etc.
                /// Template: /sitecore/templates/System/Templates/Template -{AB86861A-6030-46C5-B394-E8F99E8B87DB}
                var templateId = master.GetTemplate(new ID("{AB86861A-6030-46C5-B394-E8F99E8B87DB}"));

               /// parentItem is the item/ Template folder where you want to create your template.
               /// ParentItem: /sitecore/templates/[new folder {Guid}]
                Item parentItem = master.GetItem(new ID("{3C7516ED-7E3E-4442-8124-26691599596E}"));


                Item newItem = parentItem.Add("HelloTemplate", templateId);

                // adding Field in Templates.
                TemplateItem exampleTemplate = master.Templates[new ID(newItem.ID.ToString())];

                TemplateSectionItem data = exampleTemplate?.GetSection("data");

                if( data == null || data.InnerItem.Parent.ID != exampleTemplate.ID)
                {
                    data = exampleTemplate.AddSection("Data", false);
                }
                TemplateFieldItem title = data?.GetField("title");
                if(title == null)
                {
                    TemplateFieldItem field = data.AddField("Title");
                }
            }
                return Json(new { Result = "item created" }, JsonRequestBehavior.AllowGet);
            
        }
        catch (Exception ex)
        {
            return Json(new { Result = "item not created " + ex.Message+"\n"+ex.StackTrace } , JsonRequestBehavior.AllowGet);
        }
    }
相关问题