Cannot update a content block in a previously published page

Posted by Community Admin on 04-Aug-2018 16:55

Cannot update a content block in a previously published page

All Replies

Posted by Community Admin on 18-Oct-2011 00:00

public void UpdateSubPage(string pageName, string pageContentHeader, string pageContent)
    PageManager pManager = PageManager.GetManager();
    IQueryable<PageData> pgList = pManager.GetPageDataList();
    pManager.Provider.SuppressSecurityChecks = false;
 
    PageData pgToModify = null;
 
    foreach (PageData item in pgList)
    
        if (item.Title.CompareTo(pageName) == 0)
        
            pgToModify = item;
            PageDraft editPage = pManager.EditPage(pgToModify.Id, true);
 
            foreach (PageControl pcItem in pgToModify.Controls)
            
                var control = pManager.LoadControl(pcItem);
                if (control is ContentBlock)
                
                    ContentBlock cb = ((ContentBlock)control);
                    if (cb.ID.ToLower()  == "c001")
                    
                        cb.Html = "for each control and verify the returned result with";
                       
                    
                
            
            pManager.PublishPageDraft(editPage.Id, true);
            pManager.SaveChanges();
           
        
    

This the code that we have created. Although we don't get any errors. The content block don't get update.
Please advice us for a solution.

Posted by Community Admin on 31-Oct-2011 00:00

Hello Roni,

The only thing you're missing is that actually the ContentBlock cb for which you're setting the Html property
is a different control from the one that exists in the PageData object, namely var control = pManager.LoadControl(pcItem); What you need to do in order to complete the setting of the Html to the actual content block is a call to Pagemanager.ReadProperties()  will copy all properties from the loaded control to the actual representation of the content block in the PageData object. Please find below a code sample that demonstrates how you can edit a content block content from code:

PageManager pageManager = PageManager.GetManager();
            var pageData = pageManager.GetPageDataList().Where(p => p.HtmlTitle == "TTT").FirstOrDefault();
            if (pageData != null)
            
                var draft = pageManager.EditPage(pageData.Id);
                draft = pageManager.PagesLifecycle.CheckOut(draft);
  
                if (draft != null)
                
                    var contentBlockControl = draft.Controls.Where(c => c.ObjectType == typeof(ContentBlock).FullName).FirstOrDefault();
                    var contentBlockControlToEdit = pageManager.LoadControl(contentBlockControl) as ContentBlock;
                    contentBlockControlToEdit.Html = "Edited from code!";
                    pageManager.ReadProperties(contentBlockControlToEdit, contentBlockControl);
                    var master = pageManager.PagesLifecycle.CheckIn(draft);
                    pageManager.PagesLifecycle.Publish(master);
                    pageManager.SaveChanges();
                



All the best,
Boyan Barnev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 04-Nov-2011 00:00

Hi Boyan,

ContenBlock update is ok. When we update the page and page content, it adds some record to "sf_control_properties" table. But if we open the page from backend to edit, ex content comes.
Nothing changed seems to be. If we fetch the page data from code behind, our new content comes.
We are not working on the same content with UI. Is versioning causes for this?

Posted by Community Admin on 09-Nov-2011 00:00

Hi Volkan Demirpence,

Thank you very much for turning my attention to this. Actually, it's the page Lifecycle that we need to address, so we can make sure the actual content inside the content block is updated and the changes are reflected in the Live version of the page. Please find below a revised sample describing the proper approach to doing this. I've also introduced a little change in the way we obtain the Html of the ContentBlock - please note that in this case we don't need to make a call to ReadProperties() as we're directly obtaining the Html from the properties collection of the control, without the need of loading it explicitly as ContentBlock.

public static void ModifyContentBlock()
       
           var pageManager = PageManager.GetManager();
           var page = pageManager.GetPageNodes().Where(p => p.Title == "TTTT").FirstOrDefault();
 
           if (page != null)
           
               //Please specify the desired culture, or use  CultureInfo.CurrentUICulture.Name instead
               var culture = CultureInfo.GetCultureInfo("en");
               //Get the master record
               var master = pageManager.PagesLifecycle.Edit(page.Page, culture);
               //Get the temp version for editing
               var pageDraft = pageManager.PagesLifecycle.CheckOut(master, culture);
                
               //Get the ContentBlock from the Controls collection on the page
               var contentBlockControl = pageDraft.Controls.Where(c => c.ObjectType == typeof(ContentBlock).FullName).FirstOrDefault();
 
               //Directly access the Html property of the ContentBlock control
               var htmlProperty = contentBlockControl.GetProperties(culture, false).SingleOrDefault(p => p.Name == "Html");
               htmlProperty.Value = "New value";
               htmlProperty.MultilingualValue = "New value";
 
               //Checkin to the master record
               master = pageManager.PagesLifecycle.CheckIn(pageDraft);
               //Publish the Live version
               pageManager.PagesLifecycle.Publish(master);
               pageManager.SaveChanges();
           
       

Best wishes,
Boyan Barnev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed