navigation
 Monday, September 15, 2008

My latest challenge is to learn everything there is about Sitefinity. When I first came to Falafel, one of my first assignments was to update some of Falafel's DNN modules, one of which was QuoteMax. This was a simple module that would manage a list of quotes and would pick one for display upon each page load. Of course, you might have a hard time saying any DNN module development was simple. In any case, this simple control would be my first try at custom Sitefinity development.

Taking a look at Sitefinity, I found that it had a list module built in. Using the instructions on this page, I added a new list with several quotes. 

Now for the control. Using this video, I came up with a framework for my control. It was surprising easy and very un-DNN like. The control is nothing more than a simple web control. No complicated process to add the control to your Sitefinity web site. All I needed was a div to host the content. Below is my simple web control.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ActiveQuote.ascx.cs" Inherits="MyControls_ActiveQuote" %> <div id="quote" runat="server"></div>

So how do I implement my control and get access to Sitefinity's list modules? A detailed API can be found here. All of the modules within Sitefinity have some type of manager class, and the one I was looking for was the ListManager. Using this manager, I came up with the following helper class and appended it to my webcontrol code behind.

public class ActiveQuoteHelper { public static INamedList FindQuoteList(string quoteList) { INamedList myList = null; ListManager listMgr = new ListManager(); foreach (INamedList list in listMgr.GetLists()) { if (list.Name == quoteList) { myList = list; break; } } return myList; } public static string GetQuote(INamedList myList) { string result = string.Empty; if (myList != null) { Random rand = new Random(DateTime.Now.Millisecond); int index = rand.Next(myList.Items.Count); IListItem item = (IListItem)myList.Items[index]; result = item.Content; } return result; } }

The method FindQuoteList uses the ListManger to iterate through all of the lists to find and return the one whose name matches the provided parameter quoteList. The returned type is INamedList. The second method, GetQuote, takes a INamedList, picks a random item from the list, and returns the content from that random item.

Now I need to add a property to my web control code behind to store the name of the Sitefinity list that I'm going to use for my quotes. I also need to add a couple of lines of code to the page load to call into my helper class and write my quote to the div in my web control.

public partial class MyControls_ActiveQuote : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { INamedList myList = ActiveQuoteHelper.FindQuoteList(QuoteList); quote.InnerHtml = ActiveQuoteHelper.GetQuote(myList); } } private string _QuoteList; public string QuoteList { get { return _QuoteList; } set { _QuoteList = value; } } }

So let's add this to your Sitefinity site. See the first part of this page for instructions on how to add your new control to a page. With your control now in the toolbox, add it to your page. Click on the edit link on your control (circled in red) and notice the property (also circled in red) that we added to our control shows up. This is where you add the name of the quotes list that you created earlier.

AQ1

Click on I'm done, publish the page, and view it. Refresh the page a few times to see that random quotes are getting loaded.

 |  |  |  |