c# - 更新从 pageManager.LoadControl() 中提取的 ContentBlock。站点有限 5

标签 c# .net sitefinity

也在论坛上问过这个问题,但目前还没有成功。我需要做的是在给定页面上设置每个内容 block 的 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 循环中迭代被检测为内容 block 的控件。
如 foreach 循环内的注释中所写,您可以通过其显式 ID(通过名为“ID”的属性)或通过其相关的共享内容 block (通过名为“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。站点有限 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11990040/

相关文章:

c# - SiteFinity C# 中的联系表

c# - 用数据表进行数据分页

c# - Entity Framework Core 两个外键 - 同一张表

c# - 在 Google BigQuery API 中,查询响应的默认超时是多少?

c# - WPF MenuItem.IsChecked 绑定(bind)不起作用

c# - 从 ASP.NET Web API ASP.NET Core 2 返回 HTML 并获取 http 状态 406

c# - 为什么 RequestAdditionalTime() 方法在 Vista/7 中重启时不起作用?

c# - 在数据集 C# 中查询

.net - 这是做什么的?任务列表/m "mscor*"

linq - Sitefinity 4 带有 LINQ to SQL?