C# Sitecore动态占位符允许渲染
我正在Sitecore 7中实现动态占位符,如文章中所述C# Sitecore动态占位符允许渲染,c#,.net,sitecore,sitecore7,C#,.net,Sitecore,Sitecore7,我正在Sitecore 7中实现动态占位符,如文章中所述 它工作正常,因此我可以将相同的渲染添加到布局中,渲染将进入相应的动态占位符。但是,当我单击向动态占位符添加渲染时,占位符设置未被使用 我期望的是,动态占位符上可能会出现允许的渲染提示。相反,呈现“渲染/布局”树以手动选择渲染-使内容编辑器能够向占位符添加不允许的渲染 我已经调试了代码,正在为动态占位符找到正确的占位符设置项,并且正在检索允许的渲染列表。但是,尽管在args中设置了该列表,但该列表不会显示给用户。请参阅下面的代码 p
public class GetDynamicKeyAllowedRenderings : GetAllowedRenderings
{
//string that ends in a GUID
public const string DynamicKeyRegex = @"(.+){[\d\w]{8}\-([\d\w]{4}\-){3}[\d\w]{12}}";
public new void Process(GetPlaceholderRenderingsArgs args)
{
Assert.IsNotNull(args, "args");
// get the placeholder key
string placeholderKey = args.PlaceholderKey;
var regex = new Regex(DynamicKeyRegex);
Match match = regex.Match(placeholderKey);
// if the placeholder key text followed by a Guid
if (match.Success && match.Groups.Count > 0)
{
// Is a dynamic placeholder
placeholderKey = match.Groups[1].Value;
}
else
{
return;
}
Item placeholderItem = null;
if (ID.IsNullOrEmpty(args.DeviceId))
{
placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
args.LayoutDefinition);
}
else
{
using (new DeviceSwitcher(args.DeviceId, args.ContentDatabase))
{
placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
args.LayoutDefinition);
}
}
// Retrieve the allowed renderings for the Placeholder
List<Item> collection = null;
if (placeholderItem != null)
{
bool allowedControlsSpecified;
args.HasPlaceholderSettings = true;
collection = this.GetRenderings(placeholderItem, out allowedControlsSpecified);
if (allowedControlsSpecified)
{
args.CustomData["allowedControlsSpecified"] = true;
}
}
if (collection != null)
{
if (args.PlaceholderRenderings == null)
{
args.PlaceholderRenderings = new List<Item>();
}
args.PlaceholderRenderings.AddRange(collection);
}
}
}
公共类GetDynamicKeyAllowedRenderings:GetAllowedRenderings
{
//以GUID结尾的字符串
公共常量字符串DynamicKeyRegex=@“(.+){[\d\w]{8}-([\d\w]{4}-){3}[\d\w]{12}”;
公共新建无效进程(GetPlaceholderRenderingsArgs)
{
Assert.IsNotNull(args,args);
//获取占位符键
字符串占位符键=args.placeholder键;
var regex=新regex(DynamicKeyRegex);
Match=regex.Match(占位符键);
//如果占位符键文本后跟Guid
if(match.Success&&match.Groups.Count>0)
{
//是一个动态占位符
占位符键=匹配。组[1]。值;
}
其他的
{
返回;
}
项目占位符项目=null;
if(ID.IsNullOrEmpty(args.DeviceId))
{
占位符项=Client.Page.GetPlaceholderItem(占位符键,args.ContentDatabase,
参数。布局定义);
}
其他的
{
使用(新的DeviceSwitcher(args.DeviceId、args.ContentDatabase))
{
占位符项=Client.Page.GetPlaceholderItem(占位符键,args.ContentDatabase,
参数。布局定义);
}
}
//检索占位符允许的渲染
列表集合=null;
if(占位符项!=null)
{
指定的bool允许控制;
args.haspholderSettings=true;
collection=this.GetRenderings(占位符项,指定了OutAllowedControls);
if(指定的允许控制)
{
args.CustomData[“allowedControlsSpecified”]=true;
}
}
if(集合!=null)
{
if(args.PlaceholderRenderings==null)
{
args.PlaceholderRenderings=新列表();
}
args.PlaceholderRenderings.AddRange(集合);
}
}
}
由于这段代码是为Sitecore 6.5/6.6开发的,我想知道在跳到Sitecore 7.0的过程中是否带来了影响代码后半部分的更改我通过反编译Sitecore 7内核并查看默认的GetAllowedRenderings类找到了问题的根源。如果找到允许的渲染,则需要将ShowTree选项设置为false。见下文
public class GetDynamicKeyAllowedRenderings : GetAllowedRenderings
{
//string that ends in a GUID
public const string DynamicKeyRegex = @"(.+){[\d\w]{8}\-([\d\w]{4}\-){3}[\d\w]{12}}";
public new void Process(GetPlaceholderRenderingsArgs args)
{
Assert.IsNotNull(args, "args");
// get the placeholder key
string placeholderKey = args.PlaceholderKey;
var regex = new Regex(DynamicKeyRegex);
Match match = regex.Match(placeholderKey);
// if the placeholder key text followed by a Guid
if (match.Success && match.Groups.Count > 0)
{
// Is a dynamic placeholder
placeholderKey = match.Groups[1].Value;
}
else
{
return;
}
// Same as Sitecore.Pipelines.GetPlaceholderRenderings.GetAllowedRenderings from here but with fake placeholderKey
// i.e. the placeholder without the Guid
Item placeholderItem = null;
if (ID.IsNullOrEmpty(args.DeviceId))
{
placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
args.LayoutDefinition);
}
else
{
using (new DeviceSwitcher(args.DeviceId, args.ContentDatabase))
{
placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
args.LayoutDefinition);
}
}
List<Item> collection = null;
if (placeholderItem != null)
{
bool allowedControlsSpecified;
args.HasPlaceholderSettings = true;
collection = this.GetRenderings(placeholderItem, out allowedControlsSpecified);
if (allowedControlsSpecified)
{
// Hide the Layout/Rendering tree to show the Allowed Renderings
args.Options.ShowTree = false;
}
}
if (collection != null)
{
if (args.PlaceholderRenderings == null)
{
args.PlaceholderRenderings = new List<Item>();
}
args.PlaceholderRenderings.AddRange(collection);
}
}
}
公共类GetDynamicKeyAllowedRenderings:GetAllowedRenderings
{
//以GUID结尾的字符串
公共常量字符串DynamicKeyRegex=@“(.+){[\d\w]{8}-([\d\w]{4}-){3}[\d\w]{12}”;
公共新建无效进程(GetPlaceholderRenderingsArgs)
{
Assert.IsNotNull(args,args);
//获取占位符键
字符串占位符键=args.placeholder键;
var regex=新regex(DynamicKeyRegex);
Match=regex.Match(占位符键);
//如果占位符键文本后跟Guid
if(match.Success&&match.Groups.Count>0)
{
//是一个动态占位符
占位符键=匹配。组[1]。值;
}
其他的
{
返回;
}
//与Sitecore.Pipelines.GetPlaceholderRenderings.GetAllowedRenderings相同,但带有假占位符键
//即不带Guid的占位符
项目占位符项目=null;
if(ID.IsNullOrEmpty(args.DeviceId))
{
占位符项=Client.Page.GetPlaceholderItem(占位符键,args.ContentDatabase,
参数。布局定义);
}
其他的
{
使用(新的DeviceSwitcher(args.DeviceId、args.ContentDatabase))
{
占位符项=Client.Page.GetPlaceholderItem(占位符键,args.ContentDatabase,
参数。布局定义);
}
}
列表集合=null;
if(占位符项!=null)
{
指定的bool允许控制;
args.haspholderSettings=true;
collection=this.GetRenderings(占位符项,指定了OutAllowedControls);
if(指定的允许控制)
{
//隐藏布局/渲染树以显示允许的渲染
args.Options.ShowTree=false;
}
}
if(集合!=null)
{
if(args.PlaceholderRenderings==null)
{
args.PlaceholderRenderings=新列表();
}
args.PlaceholderRenderings.AddRange(集合);
}
}
}