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

Programmatically add a new field in a template in sitecore

Sitecore 中是否可以通过编程方式在模板中添加新字段?

我有一个模板“DictionaryName”,我想在这个模板中添加一个类型为“Single-Line Text”的字段“Newname”。

您可以通过编程方式从项目访问模板,然后将项目添加到该模板。模板是儿童常用的项目。

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

    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;

希望对您有所帮助!

为您编写了这个示例。

Want to find out more 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);
        }
    }