Creating a Hideable Sitefinity Content Block Widget

Adding a content block to a Sitefinity page with a Content widget (such as News, Events, Blogs, etc.) displays that content for both the master list and single item details view for the content. This video demonstrates how you can easily inherit from this control to make a hideable content block that only renders when the page is in the master list view.

Code Sample

Here is the full source for the custom class.

View Code Sample on Gist

Get the Url of a Sitefinity Page from the Guid

If you are working with the Sitefinity Pages API you can easily retrieve the Id of any page in the system. You might also be working with a custom or intra-site module, which has a static Guid property for the landing page.

Fortunately, there is an extension method in the Telerik.Sitefinity.Modules.Pages that can return the actual full url to the page.

Add Control to Sitefinity Page with Default Permissions

When adding controls to a Sitefinity page programmatically, by default it’s going to set restricted permissions so that only users in the Administrators role can view them.

You can easily change this so that the widget is installed with the default permissions it would get by being dragged onto the page with the following code:

Enabling and Disabling Sitefinity Inline Editing

Sitefinity includes a helpful inline editing feature, which allows an authorized user to quickly update content on the public website instead of the administration backend. This can be enabled for all, some, or no users, depending on your needs and configuration

Programmatically Adding Controls to a Sitefinity Page

The PageManager in Sitefinity has a method called CreateControl that is overloaded to allow both Compiled (dll) controls as well as simple User Controls (.ascx files) to be initialized and added to a Sitefinity page. Both overloads require a string containing the name of the ContentPlaceholder that will hold the control, which is defined in the .master file backing a Sitefinity template.

CreateControl Method and Sample Code

For compiled web controls, the signature of the CreateControl method looks like this:

CreateControl<T>(System.Web.UI.Control control, string placeHolder)

Here is an example code snippet that creates a NewsView control and adds it to a page.

View code sample on Gist

The method for UserControls, on the other hand, accepts a string with the relative path to the user control (such as “~/widgets/control.ascx”):

CreateControl<T>(string controlPath, string placeHolder)

Here is an example code snippet that creates an instance of a user control and adds it to the page.

View code sample on Gist

In both methods, the generic type <T> should be PageControl.

Adding Controls in Order

Controls added to Sitefinity Pages load at runtime in no particular order, and even if you add them in a specific sequence, they may load on the page in a different order.

Fortunately, you can specify the order the widgets should load by using the SiblingId property of the control, which refers to the ID of the control that should precede it in the placeholder.

The first control should have this value be empty, since it is at the top and has no previous sibling control. After adding the control, save the ID, and assign it to the SiblingID property of the next control you wish to load.

Here is a simple example that demonstrates this, adding the three controls so they load in order on the page. This example demonstrates using a User Control, but the property works for compiled controls as well.

View code sample on Gist