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.

Comments (18)

  1. williamcornwill says:

    Nigel Says:

    April 3, 2007 at 3:59 am

    Does not

    protected void arAds_AdCreated(object sender, AdCreatedEventArgs e)

    Also need converting ? Or have I missed it ?

  2. williamcornwill says:

    Code Jedi Says:

    April 12, 2007 at 7:50 pm

    Instead of implementing a standard AdRotator as suggested (using arAds_AdCreated) I just registered my DLL as a safe control and using SharePoint Designer I added my control to the Page Layout and it all worked fine.

  3. williamcornwill says:

    joshmadagan.com » Adapting the MCMS AdRotator to MOSS Says:

    April 18, 2007 at 9:15 am

    […] MOSS AdRotator Control […]

  4. williamcornwill says:

    joshmadagan.com » Adapting the MCMS AdRotator to MOSS Says:

    April 18, 2007 at 9:15 am

    […] MOSS AdRotator Control […]

  5. williamcornwill says:

    Code Jedi Says:

    April 18, 2007 at 3:36 pm

    I have updated the code sample above to take host header mappings into account. It now uses the web.ServerRelativeUrl instead of the web.Url.

  6. williamcornwill says:

    Nigel Says:

    April 19, 2007 at 6:32 pm

    Thanks William – works a treat

  7. williamcornwill says:

    Sebastiaan Brozius Says:

    April 25, 2007 at 12:25 am

    Could ypu provide me with the complete code for this?? I’m really new at this Web Part-stuff (and programming in C#), but I could really use this, and would like to learn from it….

  8. williamcornwill says:

    Code Jedi Says:

    April 25, 2007 at 8:50 am

    This code example is based on the MCMSAdRotator (http://www.mcmsfaq.com/articles/sp2ads.asp), if you download that and then modify with the changes from my article you shouldn’t have a problem. If you would still like me to send you the complete code, then email me with your details and I will forward it to you.(william@codejedi.net).

  9. williamcornwill says:

    Eric Says:

    May 12, 2007 at 8:28 am

    Are you implementing this as a “webpart” solution or as something else?

  10. williamcornwill says:

    Code Jedi Says:

    May 12, 2007 at 8:46 am

    This has been implemented as a derived custom control.

  11. williamcornwill says:

    Eric Says:

    May 12, 2007 at 9:02 am

    Ok then I’m a little confused on how I make it available to be used on my PageLayout.

    I thought that I could follow the same procedure as a WebPart (i.e. web.config safe controls, etc.) So how then do I make it “available” for use?

  12. williamcornwill says:

    Trevor Says:

    May 14, 2007 at 3:15 am

    Willam – I’ve been trying to email you some details/questions I am having with this… I get an immediate bounceback stating I am likely spam when I send an email from my gmail or business accounts saying I am spam (I’ve sent a minimal message also – purposely leaving possible spam words out). Please advise how I can contact you.

  13. williamcornwill says:

    Code Jedi Says:

    May 14, 2007 at 3:40 pm

    Trevor, try william.cornwill@hotmail.com.

  14. williamcornwill says:

    carrie Says:

    July 26, 2007 at 8:59 am

    i need to use this control, as my client wants the adrotator on his MOSS site, but I have no idea what to do with the .cs file. Can anyone out there help me???

  15. williamcornwill says:

    links for 2007-08-25 « Footprint of IT Says:

    August 25, 2007 at 12:34 pm

    […] MOSS AdRotator Control « CodeJedi.NET (tags: sharepoint) […]

  16. williamcornwill says:

    phebe Says:

    September 18, 2007 at 12:20 am

    Please let us kno what to do with the .cs file.

  17. williamcornwill says:

    Raghu Says:

    January 3, 2008 at 12:15 am

    Hi,

    i am new to sharepoint (MOSS 2007)

    How can i use adrotator control?

    Please let me know step by step procedure

  18. williamcornwill says:

    Code Jedi Says:

    January 3, 2008 at 9:41 am

    These are the fail safe steps for implementing an Ad Rotator for MOSS;

    1. Download the source code file from the sample code section of this blog

    2. Open Visual Studio 2005

    3. Create a new DLL class library project

    4. Add the MOSSAdRoatator.cs file to the project

    5. Strong Name the assembly and compile

    6. Copy the .dll file to the SharePoint server being deployed and add to the GAC

    7. Add the Assembly to the list of safe controls in the web.config of the SharePoint Web Application

    8. Add the reference and control to the master page or page layout using SharePoint Designer

    Note the control uses a picture library to obtain images from, in this example “PicLibName”. The Picture Library is assumed to be located in the same site as the page.

Skip to main content