Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用代码添加分支模板不会复制布局/渲染数据,为什么/如何修复?_C#_Sitecore_Sitecore Branch Templates - Fatal编程技术网

C# 使用代码添加分支模板不会复制布局/渲染数据,为什么/如何修复?

C# 使用代码添加分支模板不会复制布局/渲染数据,为什么/如何修复?,c#,sitecore,sitecore-branch-templates,C#,Sitecore,Sitecore Branch Templates,我有一个从标准模板继承的分支模板。如果我使用菜单功能(插入->从模板插入->等)将渲染添加到分支模板并添加到Sitecore,则渲染等将按预期显示,即与分支模板中的效果完全相同。如我所料 但是,当我尝试在代码中添加分支模板时,问题就出现了。这样做似乎忽略了分支模板中的任何渲染。显示的渲染仅为标准模板中的渲染。我已尝试将所有渲染数据移动到分支模板中,但这不会导致布局数据被发送到新创建的项目。我创建的项目如下: Item parent=SitecoreService.Database.GetItem

我有一个从标准模板继承的分支模板。如果我使用菜单功能(插入->从模板插入->等)将渲染添加到分支模板并添加到Sitecore,则渲染等将按预期显示,即与分支模板中的效果完全相同。如我所料

但是,当我尝试在代码中添加分支模板时,问题就出现了。这样做似乎忽略了分支模板中的任何渲染。显示的渲染仅为标准模板中的渲染。我已尝试将所有渲染数据移动到分支模板中,但这不会导致布局数据被发送到新创建的项目。我创建的项目如下:

Item parent=SitecoreService.Database.GetItem(parentPath);
如果(父项==null)
抛出新的NullReferenceException(“无法从路径“+parentPath”加载项以充当父对象);
Item branchItem=SitecoreService.Database.GetItem(新ID(branchTemplateId));
branchItem.Fields.ReadAll();
//BranchItem branch=SitecoreService.Database.GetItem(新ID(branchTemplateId));
if(branchItem==null)
抛出新的ApplicationException(“未能找到id为“+branchTemplateId”的模板分支);
ItemItemFromBranch=parent.Add(itemName,new BranchItem(BranchItem));
实际上,我已经尝试了上面的几种变体(使用隐式转换,从
字段中添加和删除
ReadAll
,等等)

通过代码创建渲染字段时为空,通过菜单创建渲染字段时的“原始”值为:


我错过了什么


尝试了以下Mareks的建议,但似乎也这么做了:

Item parent = SitecoreService.Database.GetItem(parentPath);
if (parent == null)
    throw new NullReferenceException("Cannot load item to act as parent from path: " + parentPath);

BranchItem branchItem = SitecoreService.Database.GetItem(new ID(branchTemplateId));

if (branchItem == null)
     throw new ApplicationException("Failed ot find template branch with the id:" + branchTemplateId);


Item itemFromBranch = Context.Workflow.AddItem(itemName, branchItem, parent);

UI命令使用
工作流.AddItem
方法

请尝试以下代码:

Item branchItem=(branchItem)SitecoreService.Database.GetItem(新ID(branchTemplateId));
if(branchItem==null)
抛出新的ApplicationException(“未能找到id为“+branchTemplateId”的模板分支);
Context.Workflow.AddItem(itemName、branchItem、parent);

尝试使用以下方法

var branchpath = "your/branch/template/path";

using (new SecurityDisabler())
{
   var masterDb = Sitecore.Configuration.Factory.GetDatabase("master");

   Item parentItem = masterDb.Items["path/to/parent/item/here"];

   BranchItem template = masterDb.GetItem(branchpath);

   parentItem.Add("YourItemName", template);

}

谢谢

我以为你把它放在这里了,但似乎没什么区别。我猜你从站点核心dll中反映了这一点。您能告诉我执行上述操作的代码吗?请检查
Sitecore.Shell.Framework.Pipelines.AddFromTemplate.Execute()
方法。您使用哪个版本的Sitecore?你有多种语言吗?您是否尝试使用
en
语言添加新分支?sitecore 8。我有多种语言。我会尝试确保现在的英文版本。也许问题是访问权限?您可以尝试使用SecurityDisabler吗?这与我的原始代码有什么不同?您只是以稍微不同的方式选择父项。这听起来像是胡乱猜测。你知道这实际上解决了这个问题吗?除了安全禁用部分,没有什么不同。我曾经在一个为我工作的原型项目中使用过这段代码。只是想和大家分享一下。:)它是在Sitecore 7.2Ah上的,对,我已经有了一个安全禁用程序,它只是不在这个问题的代码中。使用此选项时,是否应用了分支渲染?i、 e.不是模板中的,而是分支中的?是的,即使我在分支模板上配置了渲染,也会显示渲染。