Sitecore 以编程方式添加移动网站
我面临以下问题:我想为特定模板动态添加移动网站,我在特定项目的标准值中指定了移动布局。这一切都工作正常,但是当我更改项目的字段时,默认站点的布局和渲染消失了!有人对这个问题有解决方案/建议吗?
我正在使用 Sitecore 6.4。
提前致谢!
我当前使用的代码(这是向项目添加硬编码布局,下一步是(当我解决此问题时)从 standard_values 项目获取布局)
public class CheckMobileLayout
{
public void Process([NotNull] SaveArgs args)
{
try
{
foreach (Sitecore.Pipelines.Save.SaveArgs.SaveItem saveItem in args.Items)
{
Item orgItem = Context.ContentDatabase.Items[saveItem.ID, saveItem.Language, saveItem.Version];
if(orgItem.Name != "Content Editor")
{
TemplateItem testTemplate = orgItem.Template;
foreach (Field orgField in orgItem.Fields)
{
if (orgField != null)
{
if (orgField.GetTemplateField().Type == "Mobile Checkbox")
{
foreach (Sitecore.Pipelines.Save.SaveArgs.SaveField saveField in saveItem.Fields)
{
if (saveField.ID == orgField.ID)
{
if (saveField.Value != orgField.Value)
{
if (saveField.Value == "1") AddMobileLayout(orgItem);
else RemoveMobileLayout(orgItem);
}
}
}
}
}
}
}
}
}
catch (NullReferenceException)
{
}
}
private void RemoveMobileLayout(Item orgItem)
{
using (new SecurityDisabler())
{
Database masterDatabase = Database.GetDatabase("master");
orgItem = masterDatabase.GetItem(orgItem.Paths.Path);
string renderingXml = orgItem[Strings.Renderings];
LayoutDefinition layoutDefinition = new LayoutDefinition();
layoutDefinition.LoadXml(renderingXml);
string mobileDeviceId = Strings.mobileDeviceID;
DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(mobileDeviceId);
deviceDefinition.Layout = String.Empty;
string outputXml = layoutDefinition.ToXml();
Log.Info(outputXml, this);
orgItem.Editing.BeginEdit();
orgItem[Strings.Renderings] = outputXml;
orgItem.Editing.EndEdit();
}
}
private void AddMobileLayout(Item orgItem)
{
using (new SecurityDisabler())
{
Database masterDatabase = Database.GetDatabase("master");
Item testItem = masterDatabase.GetItem(orgItem.Paths.Path);
string renderingXml = testItem[Strings.Renderings];
LayoutDefinition layoutDefinition = new LayoutDefinition();
layoutDefinition.LoadXml(renderingXml);
string mobileDeviceId = Strings.mobileDeviceID;
DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(mobileDeviceId);
deviceDefinition.Layout = Strings.mobileLayoutID;
string outputXml = layoutDefinition.ToXml();
testItem.Editing.BeginEdit();
testItem[Strings.Renderings] = layoutDefinition.ToXml();
testItem.Editing.EndEdit();
}
}
}
I am facing the following problem: I want to add dynamically a mobile site for specific templates, I specified the mobile layout in the standard values of the specific item. This all works fine but when I changed a field of the Item the layout and renderings of the default site is gone! Does anyone has a solution/suggestion for this problem?
I am working with Sitecore 6.4.
Thanx in advance!
The code that I am currently using (this is to add hardcoded a layout to an item, the next step is (when I fixed this problem) to get the layout from the standard_values item)
public class CheckMobileLayout
{
public void Process([NotNull] SaveArgs args)
{
try
{
foreach (Sitecore.Pipelines.Save.SaveArgs.SaveItem saveItem in args.Items)
{
Item orgItem = Context.ContentDatabase.Items[saveItem.ID, saveItem.Language, saveItem.Version];
if(orgItem.Name != "Content Editor")
{
TemplateItem testTemplate = orgItem.Template;
foreach (Field orgField in orgItem.Fields)
{
if (orgField != null)
{
if (orgField.GetTemplateField().Type == "Mobile Checkbox")
{
foreach (Sitecore.Pipelines.Save.SaveArgs.SaveField saveField in saveItem.Fields)
{
if (saveField.ID == orgField.ID)
{
if (saveField.Value != orgField.Value)
{
if (saveField.Value == "1") AddMobileLayout(orgItem);
else RemoveMobileLayout(orgItem);
}
}
}
}
}
}
}
}
}
catch (NullReferenceException)
{
}
}
private void RemoveMobileLayout(Item orgItem)
{
using (new SecurityDisabler())
{
Database masterDatabase = Database.GetDatabase("master");
orgItem = masterDatabase.GetItem(orgItem.Paths.Path);
string renderingXml = orgItem[Strings.Renderings];
LayoutDefinition layoutDefinition = new LayoutDefinition();
layoutDefinition.LoadXml(renderingXml);
string mobileDeviceId = Strings.mobileDeviceID;
DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(mobileDeviceId);
deviceDefinition.Layout = String.Empty;
string outputXml = layoutDefinition.ToXml();
Log.Info(outputXml, this);
orgItem.Editing.BeginEdit();
orgItem[Strings.Renderings] = outputXml;
orgItem.Editing.EndEdit();
}
}
private void AddMobileLayout(Item orgItem)
{
using (new SecurityDisabler())
{
Database masterDatabase = Database.GetDatabase("master");
Item testItem = masterDatabase.GetItem(orgItem.Paths.Path);
string renderingXml = testItem[Strings.Renderings];
LayoutDefinition layoutDefinition = new LayoutDefinition();
layoutDefinition.LoadXml(renderingXml);
string mobileDeviceId = Strings.mobileDeviceID;
DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(mobileDeviceId);
deviceDefinition.Layout = Strings.mobileLayoutID;
string outputXml = layoutDefinition.ToXml();
testItem.Editing.BeginEdit();
testItem[Strings.Renderings] = layoutDefinition.ToXml();
testItem.Editing.EndEdit();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已使用以下方法修复了此问题:
I've fixed this with the following to methods: