2012-08-16 36 views
3

在論壇上也提到過這個問題,但是至今沒有運氣。我需要做的是在給定頁面上設置每個內容塊的HTML內容。看來我可以設置html值,但保存它並不會更新實際頁面。更新從pageManager.LoadControl()中提取的ContentBlock。 Sitefinity 5

我想知道是否因爲需要在控件上進行某種保存調用。似乎沒有任何方法可用於此類行動。

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); 

回答

4

下面的代碼可以幫助你達到你所需要的。
它首先會通過標題獲取頁面(我正在尋找一個標題爲「重複」的頁面,因爲它是由您的代碼隱含的)。
它生成當前頁面的新草稿,然後檢查其控件。
然後在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(); 
} 

我希望這個代碼可以幫助。

阿隆。

+0

正在努力實現它,謝謝你的回答。請注意,我的行'htmlProperty'對象沒有'GetString'方法。任何想法爲什麼? – agmcleod 2012-08-20 12:14:10

+0

@agmcleod,GetString和SetString是存儲在Telerik.Sitefinity.Model.DataExtensions類中的擴展方法。請確保在您的源文件中包含:使用Telerik.Sitefinity.Model的 '; 。 – alrotem 2012-08-20 14:54:27