c#-更新从pageManager.LoadControl()拉出的ContentBlock. Sitefinity 5
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-更新从pageManager.LoadControl()拉出的ContentBlock. Sitefinity 5,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3069字,纯文字阅读大概需要5分钟。
内容图文
![c#-更新从pageManager.LoadControl()拉出的ContentBlock. Sitefinity 5](/upload/InfoBanner/zyjiaocheng/675/0ee86a109d24404988a9bff2e4af1013.jpg)
在论坛上也有问过这个问题,但是到目前为止还没有运气.我需要做的是在给定页面上设置每个内容块的HTML内容.看来我可以将html值设置为正确,但是保存它不会更新实际页面.
我想知道是否是因为控件上需要进行某种保存调用.似乎没有任何方法可用于此类操作.
foreach (var c in duplicated.Page.Controls)
{
// go through the properties, se the ID to grab the right text
foreach (var p in c.Properties)
{
if (p.Name == "ID")
{
var content = pageContent.Where(content_pair => content_pair.Key == p.Value).SingleOrDefault();
var control = pageManager.LoadControl(c);
if (control is ContentBlock)
{
var contentBlock = pageManager.LoadControl(c) as ContentBlock;
contentBlock.Html = content.Value;
}
}
}
}
pageManager.SaveChanges(); */
WorkflowManager.MessageWorkflow(duplicated.Id, typeof(PageNode), null, "Publish", false, bag);
解决方法:
以下代码可以帮助您实现所需的功能.
它将首先按标题获得页面(我正在寻找标题为“重复”的页面,因为您的代码暗含了该页面).
它生成当前页面的新草稿,然后遍历其控件.
被检测为内容块的控件然后在foreach循环中进行迭代.
正如在foreach循环中的注释中所写,您可以通过控件的显式ID(通过名为“ ID”的属性)或相关的共享内容块(通过名为“ SharedContentID”的属性)或任何其他条件(或忽略)来检测控件.这种情况将导致更新页面中的所有控件.
一旦有了可以更新的控件,您就可以根据项目的本地化设置来设置其新值.
之后,草稿将被保存并发布,并且可以选择为其创建一个新版本.
PageManager pageManager = PageManager.GetManager();
VersionManager vmanager = VersionManager.GetManager();
PageNode duplicated = pageManager.GetPageNodes().FirstOrDefault(p => p.Title == "duplicate");
if (duplicated != null)
{
var draft = pageManager.EditPage(duplicated.Page.Id, true);
string contentBlockTypeName = typeof(ContentBlock).FullName;
PageDraftControl[] contentBlocks = draft.Controls.Where(contentBlock => contentBlock.ObjectType == contentBlockTypeName).ToArray();
foreach (PageDraftControl contentBlock in contentBlocks)
{
Guid contentBlockId = contentBlock.Id;
//User "SharedContentID" if you are looking up controls which are linked to a shared content block of a specific ID.
//If you you are trying to locate a specific control by its own ID, use the explicit "ID" property instead of "SharedCotentID"
if (contentBlock.Properties.Where(prop => prop.Name == "SharedContentID" && prop.Value.ToString() == contentItemIdstr).FirstOrDefault() != null)
{
ControlProperty htmlProperty = contentBlock.Properties.Where(prop => prop.Control.Id == contentBlockId && prop.Name == "Html").FirstOrDefault();
if (htmlProperty != null)
{
if (AppSettings.CurrentSettings.Multilingual)
{
htmlProperty.GetString("MultilingualValue").SetString(CultureInfo.CurrentUICulture, "New Value");
}
else
{
htmlProperty.Value = "New Value";
}
}
}
}
draft = pageManager.SavePageDraft(draft);
draft.ParentPage.LockedBy = Guid.Empty;
pageManager.PublishPageDraft(draft);
pageManager.DeletePageTempDrafts(draft.ParentPage);
//Use the 2 next lines to create a new version of your page, if you wish.
//Otherwise the content will be updated on the current page version.
vmanager.CreateVersion(draft, draft.ParentPage.Id, true);
vmanager.SaveChanges();
pageManager.SaveChanges();
}
我希望这段代码对您有所帮助.
阿隆
内容总结
以上是互联网集市为您收集整理的c#-更新从pageManager.LoadControl()拉出的ContentBlock. Sitefinity 5全部内容,希望文章能够帮你解决c#-更新从pageManager.LoadControl()拉出的ContentBlock. Sitefinity 5所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。