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.

Advertisements

22 Responses to MOSS AdRotator Control

  1. Nigel says:

    Does not
    protected void arAds_AdCreated(object sender, AdCreatedEventArgs e)

    Also need converting ? Or have I missed it ?

  2. Code Jedi says:

    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. Code Jedi says:

    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.

  4. Nigel says:

    Thanks William – works a treat

  5. Sebastiaan Brozius says:

    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….

  6. Code Jedi says:

    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).

  7. Eric says:

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

  8. Code Jedi says:

    This has been implemented as a derived custom control.

  9. Eric says:

    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?

  10. Trevor says:

    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.

  11. […] Quite a few people have asked me for a downable copy of the MossAdRotator control I discussed in March, so I have created a C# class file that you can […]

  12. carrie says:

    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???

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

  14. phebe says:

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

  15. Raghu says:

    Hi,

    i am new to sharepoint (MOSS 2007)

    How can i use adrotator control?

    Please let me know step by step procedure

  16. Code Jedi says:

    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.

  17. Srinivas says:

    how to implement backward and forward buttons to this adrotator control.

    Please share the code.

  18. Empresa de criação de sites, aplicativos web, lojas virtuais, manutenção de sites, otimização de site e muitos outros serviços web. A empresa foi constituída para atender desde o pequeno e médio empreendedor como as grandes empresas, ONG, Instituiçõe…

    […]MOSS AdRotator Control « CodeJedi.NET[…]…

  19. Article Exchange…

    […]MOSS AdRotator Control « CodeJedi.NET[…]…

  20. Well Put Together Site!…

    The content that you have here is very good! Most likely will visit again!…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: