1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to add custom HTML content in ContentBlock dynamically ?

How to add custom HTML content in ContentBlock dynamically ?

8 posts, 1 answered
  1. Chetan
    Chetan avatar
    15 posts
    Registered:
    30 Aug 2012
    20 Sep 2012
    Link to this post
    Hi, I am new bee to Sitefinity4.4 and I am trying to create page dynamically with few content blocks in it. I am able to publish the page with blank Content blocks, However i want to add some custom html snippet within Content Blocks while i am adding this blocks in Page melodramatically.
    Can someone help me how to do this in Sitefinity 4.4. ? 
    I am using below code to add a content block - 
     PageDraftControl ctrl1 = pageManager.CreateControl<PageDraftControl>();
                ctrl1.ObjectType = typeof(ContentBlock).FullName;
                ctrl1.PlaceHolder = "middle_content";
                ctrl1.Caption = "About Us Content Block";
                ctrl1.Description = "Location description";
                ctrl1.SetDefaultPermissions(pageManager);
                draft.Controls.Add(ctrl1);
                pageManager.PublishPageDraft(draft,true);
                pageManager.SaveChanges();
  2. Brett Whittington
    Brett Whittington avatar
    89 posts
    Registered:
    10 Aug 2012
    20 Sep 2012
    Link to this post
    I've done something similar so maybe my code will give you a hand on figuring out how to do this.  I've never used the PageDraftControl that you are using though.
    //Get Contact Information Content Item from Content Blocks Module
                ContentItem contentItem = App.WorkWith().ContentItems().Where(ci => ci.Title == "Contact Information" && ci.Visible).Get().SingleOrDefault();
                if (contentItem != null)
                {
                    //Create the Content Block control to be loaded into the page
                    ContentBlock contentBlock = new ContentBlock();
                    contentBlock.PageId = contentItem.Id;
                    contentBlock.SharedContentID = contentItem.Id;
                    contentBlock.Html = contentItem.Content.Value;
                    rightHeader.Controls.Add(contentBlock);
                }

    Now if your content isn't already in the Content Block module, you could just make use of the ContentBlock controls HTML property to display data on the page.
    Answered
  3. Chetan
    Chetan avatar
    15 posts
    Registered:
    30 Aug 2012
    20 Sep 2012
    Link to this post
    Thanks Brett, That works well!  Probably I was working with old style of using API's :).
    Is there any quick reference for all such Control level API usage available, I tried documentation (its the same code that i have pasted here) but it doesn't explain all such usage.
  4. Brett Whittington
    Brett Whittington avatar
    89 posts
    Registered:
    10 Aug 2012
    20 Sep 2012
    Link to this post
    There isn't any outside documentation other than Intellisense that I am aware of.  I've been flying by the seat of my pants for months :D.  I am glad I was able to give you a hand.
  5. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    20 Sep 2012
    Link to this post
    Glad you were able to get this working, and many thanks to Brett for helping out!

    the docs are still improving as far as deep control API stuff like this goes. Your very best bet for good samples on how to do stuff like this is the Sitefinity SDK, because many of the samples have a Telerik.Sitefinity.Samples.Common project which is used to build the sites from scratch using the API, including creating pages, templates, controls, and content all from the API.

    Hope this is helpful!
  6. Chetan
    Chetan avatar
    15 posts
    Registered:
    30 Aug 2012
    25 Jan 2013
    Link to this post
    Hello ,On a similar front - how can I add Image block to a Page dynamically? I am able to add Content Blocks dynamically as suggested above by Brett.In our case we have uploaded few images in Sitefinity Custom Image Library and now what i am looking is adding a Image Block with already uploaded image from library on Page. 

    Any help will be appreciatedThanks,Chetan

  7. Chetan
    Chetan avatar
    15 posts
    Registered:
    30 Aug 2012
    29 Jan 2013
    Link to this post
    Hi,
    Could anyone got a chance to take a look at this? 
    I wanted to add Image control to a page dynamically (similar to what Brett has mentioned in his snippet), I couldnt find a Image control under Telerik...Web.UI library.
  8. Brett Whittington
    Brett Whittington avatar
    89 posts
    Registered:
    10 Aug 2012
    29 Jan 2013
    Link to this post
    Hello Chetan!

    I wasn't able to respond earlier because I have been extremely busy working at a client.  However, I think you can do something similar to what you did with the Content Control but this time with a SitefinityImage.  Typically when I need to find a control, object, or method in Sitefinity's libraries, I use a decompiler like JustDecompile so I can search for controls like this.  It is usually much faster than scouring the forums or the limited documentation.  Here is some code I threw together in about 5 mins.  It probably doesn't work exactly like you want but it should get you started.  Good Luck!

    Telerik.Sitefinity.Libraries.Model.Image image = App.WorkWith().Images().Where(i => i.Title == "INSERT TITLE HERE" && i.Visible).Get().SingleOrDefault();
                if(image != null)
                {
                    Telerik.Sitefinity.Web.UI.SitefinityImage imgctrl = new SitefinityImage();
                    imgctrl.ImageUrl = image.Url;
                }
8 posts, 1 answered