Sitefinity Usercontrol Properties tips and tricks

by Grank December 01, 2008 10:13
Hey,
 
Some of you who have worked with Sitefinity have discovered as I have that it's actually really easy to expose your user control's properties in Sitefinity's WYSIWYG-style page editor. 
 
When you're assembling a page in Sitefinity, you don't have direct access to the markup.  So properties on a control that you would normally set using an attribute on the control's tag, you can set using a nice graphical menu in Sitefinity.  It automatically recognizes that property and renders a menu item for it, and it'll even render a dropdown of possible values if the property is an enum and that kinda cool stuff.
 
However, since you're probably exposing that CMS page edit functionality to the end user, you want to make it as user-friendly as possible.
 
Here are 4 tips, two of which are obvious from the Sitefinity documentation and two of which I had to dig a bit for.
 
Sitefinity does a lot of stuff based on the System.ComponentModel attribute decorations, the same ones that control the .NET PropertyGrid.  If you're like me, you're probably not very familiar with them, so here's a reference for the ones that are useful in Sitefinity.
 
  • Category.  Decorate a property with this attribute to sort it under that category in the properties dialog.  You can just write whatever you want in there and it'll sort the property under that heading. 
  • Description.  This was the one I was really digging for!  Decorate a property with this attribute to explain what the setting does.  For Sitefinity, this renders a little (?) next to the property's name that users can click to read a nice pop-up helpbox explaining what to do.  This is a nice feature and doesn't appear anywhere obvious in the docs!
  • DefaultProperty.  Decorate a class with this attribute to denote that the property named in its string is the "default" for that class.  In Sitefinity, this means that it will appear in bold, with a box around it, with its category expanded. 

So if we decorate our class in this manner:

    [DefaultProperty("ResultCount")]
    public partial class MiniEventViewer : System.Web.UI.UserControl
    {
        [Category("Mini Event Viewer Settings")]
        [Description("Number of most recent events to display. Must be filled out; 0 will show no events.")]
        public int ResultCount {get; set;}

Sitefinity will render the properties pane for the user control like this:
How properties render in Sitefinity

Nifty, eh?

There's one more thing.  Say we want to show different information in the edit view of the Sitefinity CMS than we want to show when the control is on a live page.  This can be accomplished easily.  When the CMS is in edit or preview mode, there will be a querystring item "cmspagemode".  In your control, you can simply check if AllKeys.Contains that key to know if the control is being rendered in the CMS back-end.  (If you wander through the telerik assemblies with reflector, you'll actually find that this is pretty much how they do theirs, so it should be stable.)  Overriding RenderControl is where I'm executing this logic but you could probably stick it elsewhere as well.

One thing to keep in mind with the above, though, is that if you want to base that logic on the properties the user has assigned to that control, you will have to make it a viewstate property, as it won't otherwise have a value in the CMS page designer.

Hope that's helpful!

Tags: , , , , ,

Comments

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant

About The Author

I'm a software developer and musician in Edmonton, AB.  I write mostly web-based software, primarily on the Microsoft stack.  I have an MCPD and several MCTS, but I've only been at this whole developer thing for a few years, and the truth is that I'm still learning more than knowing.  So these are my adventures and experiments and some of it will probably be blatantly wrong...  Just warning ya.