How to add custom HTML content in ContentBlock dynamically ?

Posted by Community Admin on 04-Aug-2018 11:25

How to add custom HTML content in ContentBlock dynamically ?

All Replies

Posted by Community Admin on 20-Sep-2012 00:00

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

Posted by Community Admin on 20-Sep-2012 00:00

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.

Posted by Community Admin on 20-Sep-2012 00:00

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.

Posted by Community Admin on 20-Sep-2012 00:00

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.

Posted by Community Admin on 20-Sep-2012 00:00

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!

Posted by Community Admin on 25-Jan-2013 00:00

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

Posted by Community Admin on 29-Jan-2013 00:00

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.

Posted by Community Admin on 29-Jan-2013 00:00

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;
            

This thread is closed