When working with the Sitefinity API and its various Content Managers, you need to make sure and consider the fact that after version 5.1, users have the ability to disable modules. Attempting to access the content manager of a disabled module will result in an exception.
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.
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.
Here is the full source for the custom class.
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.
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:
When developing a Sitefinity Intra-Site Module you’ll likely be creating public widgets that your users will be dropping onto pages. Rather than relying on your users to manually add them to the toolbox, you can register these controls yourself during module installation.
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:
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.
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.
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.
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!
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.
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.
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.
When migrating to the latest Sitefinity from the old version 3.7, by default the original .aspx page extension remains intact.
You can remove (or change) this extension with this simple code snippet.