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
Anonymous
January 31, 2008
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 ?Anonymous
January 31, 2008
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.Anonymous
January 31, 2008
joshmadagan.com » Adapting the MCMS AdRotator to MOSS Says: April 18, 2007 at 9:15 am […] MOSS AdRotator Control […]Anonymous
January 31, 2008
joshmadagan.com » Adapting the MCMS AdRotator to MOSS Says: April 18, 2007 at 9:15 am […] MOSS AdRotator Control […]Anonymous
January 31, 2008
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.Anonymous
January 31, 2008
Nigel Says: April 19, 2007 at 6:32 pm Thanks William - works a treatAnonymous
January 31, 2008
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….Anonymous
January 31, 2008
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).Anonymous
January 31, 2008
Eric Says: May 12, 2007 at 8:28 am Are you implementing this as a “webpart” solution or as something else?Anonymous
January 31, 2008
Code Jedi Says: May 12, 2007 at 8:46 am This has been implemented as a derived custom control.Anonymous
January 31, 2008
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?Anonymous
January 31, 2008
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.Anonymous
January 31, 2008
Code Jedi Says: May 14, 2007 at 3:40 pm Trevor, try william.cornwill@hotmail.com.Anonymous
January 31, 2008
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???Anonymous
January 31, 2008
links for 2007-08-25 « Footprint of IT Says: August 25, 2007 at 12:34 pm […] MOSS AdRotator Control « CodeJedi.NET (tags: sharepoint) […]Anonymous
January 31, 2008
phebe Says: September 18, 2007 at 12:20 am Please let us kno what to do with the .cs file.Anonymous
January 31, 2008
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 procedureAnonymous
January 31, 2008
Code Jedi Says: January 3, 2008 at 9:41 am These are the fail safe steps for implementing an Ad Rotator for MOSS;
- Download the source code file from the sample code section of this blog
- Open Visual Studio 2005
- Create a new DLL class library project
- Add the MOSSAdRoatator.cs file to the project
- Strong Name the assembly and compile
- Copy the .dll file to the SharePoint server being deployed and add to the GAC
- Add the Assembly to the list of safe controls in the web.config of the SharePoint Web Application
- 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.