Mapping Sitefinity Templates from the SDK

There are several different ways to map Sitefinity widgets to external templates, including globally using the ViewMap. You can also use the built-in Sitefinity Widget Template editor and even Sitefinity Thunder to modify templates and copy them to external files.

However, did you know that a copy of every single widget template is included in the Sitefinity SDK?

When you install the SDK, a zip file named WidgetTemplates.zip, which contains a copy of every usable Sitefinity template is copied to your machine.

Product Maximum and Minimums in Sitefinity Part 2: Shopping Cart

We previously looked at how you can enforce minimum and maximum product quantities on the Sitefinity product details page. However, users can still override the quantities in the shopping cart, defeating the restrictions.

Fortunately, the shopping cart template also includes a validation control for each item. By mapping this control to an external User Control template as well, we can tap into this validator with custom code just as we did before.

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

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:

Including CSS in a Sitefinity User Control

When working with Sitefinity Intra-Site Modules, all your module’s logic, code, and markup is contained in User Controls. This allows you to inherit all the styles from the default Sitefinity backend theme.

However you might want to add additional styling to your admin controls. You can accomplish this easily by adding a custom stylesheet for admin controls and referencing them from the user controls. Simply add the following code to each control’s frontend ascx file.

Filtering the Sitefinity Taxonomy Widget for a Specific Blog

The classification widgets in Sitefinity allows you to display the list of taxonomy names (such as categories or tags) used by content items in Sitefinity. You can specify a specific content type (even Dynamic Modules with this workaround) and to allow your visitors to filter the content items.

Custom Blogs Taxonomy Control

However, if you have multiple blogs on your website and use the taxonomy widget, it will include tags and categories used by ANY blog post from ANY of the blogs. There is currently no way to specify a parent blog.

Fortunately, this can be easily fixed with a custom control that inherits from the existing TaxonomyControl. By using Telerik’s handy new JustDecompile I was able to identify the method GetTaxaItemsCountForTaxonomy which both generates the list of taxonomy and counts for the list of links to be rendered.

By adding a new Guid property called ParentBlogID, I’m able to specify the specific blog from which I want to generate the taxonomy list. Then I simply modified the code to use the specified blog ID when checking for posts.

Here’s the full code for the custom class:


View Code Sample on Gist

Since this control is going to be used specifically for blogs, I initialized theContentType property to Telerik.Sitefinity.Blogs.Model.BlogPost so that I don’t have to manually specify them each time I use the widget. If you’re using a custom template, you could also specify it here so it’s loaded automatically. This is of course optional, but it’s a great time saver.

Register this control in the toolbox and you’re ready to drop it on the page and specify the Blog ID to filter the taxonomy items.

Retrieving the Blog ID

Finding the Blog ID was only a little tricky. While you could certainly go into the raw database tables and extract it, a safer way is to use the BlogPosts widget (which you are using anyway to display your blogs). In the widget control designer, you can specify the specific blog from which to display posts on the page.

sitefinity-blogposts-widget-3-

When you do this, the blog ID is saved in the ItemsParentsIds property, which is an array of Guids for the selected blogs. You can find this property (and the blog ID) by clicking the Advanced button on the control designer, then navigating to Control Definition > Views > MasterBlogPostsFrontend. Scroll down to the ItemsParentsIds property to reveal the Guid for the blog.

sitefinity-blogposts-parent-blog-id-3-

You can now copy this value and paste it into the ParentBlogID property of the BlogsTaxonomyControl. Note that you also need to specify the FieldName property to match the taxonomy, such as Category or Tags.

sitefinity-blogs-taxonomy-control-2-

NOTE: be sure to remove the brackets and quotes from the value, as the ParentBlogID is a Guid property, not an array of Guid.

Now when you load the page, the control will only load and display taxonomy items that match the Blog ID you specify. You can see this control in action now, as it is used on the taxonomy control on this page.

Hope this is helpful!

 

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