C# 正在更新从pageManager.LoadControl()中提取的ContentBlock。站点有限性5

C# 正在更新从pageManager.LoadControl()中提取的ContentBlock。站点有限性5,c#,.net,sitefinity,C#,.net,Sitefinity,在论坛上也问过这个问题,但到目前为止还没有运气。我需要做的是设置给定页面上每个内容块的HTML内容。看起来我可以设置html值,但是保存它不会更新实际页面 我想知道这是否是因为需要在控件上进行某种保存调用。似乎没有任何方法可用于此类操作 foreach (var c in duplicated.Page.Controls) { // go through the properties, se the ID to grab the right text foreach (var p

在论坛上也问过这个问题,但到目前为止还没有运气。我需要做的是设置给定页面上每个内容块的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();
}
我希望这段代码有帮助


Alon.

现在正在实现它,谢谢你的回答。请注意,我的line
htmlProperty
对象没有
GetString
方法。有什么想法吗?@agmcleod、GetString和SetString是存储在Telerik.Sitefinity.Model.DataExtensions类中的扩展方法。请确保包括:
在源文件中使用Telerik.Sitefinity.Model;