MOSS AdRotator Control

I recently had a client who wanted an AdRotator on their MOSS Internet site. The client wanted to randomly choose an image from a nominated Image Library and display it on the home page. Unfortunately there is no Moss AdRotator control out-of-the-box. So I went back to my MCMS roots and adapted the MCMS AdRotator found on MCMS faq.com.

Take the MCMS AdRotator, rename it to MossAdRotator, and modify the OnAdCreated to obtain a random SPListItem from the nominated SPList (adsLibraryName) of the Current Web. Note: The code below assumes that the Image Library is located in the current web, but that could be adapted.

protected override void OnAdCreated(AdCreatedEventArgs e) {     try     {         SPWeb web = SPContext.Current.Web;         SPList list = web.Lists[adsLibraryName];           if (list != null)         {             int resourceIndex = Randomiser(list.ItemCount);             SPListItem li = list.Items[resourceIndex];               string webUrl = web.ServerRelativeUrl;             if (webUrl != "/")             {                 webUrl = webUrl + "/";             }               e.ImageUrl = web.Url + "/" + li.Url;             e.AlternateText = li.DisplayName;         }         else         {             throw new Exception(               "The Picture Library '" +               adsLibraryName +               "' could not be found.");         }     }     catch (Exception ex)     {         if (displayErrors)         {             e.AlternateText = ex.Message;         }         else         {             this.Visible = false;         }     } }

Update: Quite a few people have asked me for a downable copy of the MossAdRotator control, so I have created a C# class file that you can download.