using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
// Author: William Cornwill
// Blog: http://www.codejedi.net
// Date: 11th June 2007
// Licence: Creative Commons Attribution 3.0 Unported (http://creativecommons.org/licenses/by/3.0/)
// You are free to use, alter, remix and redistribute this works, as long as
// this attribution remains unaltered.
namespace CodeJedi.SharePoint.UserControls
{
///
/// This is a MOSS version of the AdRotator control. It obtains a handle to a picture library
/// and then randomly chooses an image to render.
///
[DefaultProperty("Text")]
[ToolboxData("<{0}:MossAdRotator runat=server>")]
public class MossAdRotator : AdRotator
{
#region Properties
///
/// The name of the Picture Library that contains the images for the AdRotator
///
[Bindable(true)]
[Category("Behavior")]
[DefaultValue("")]
[Localizable(true)]
public string AdsLibraryName
{
get { return adsLibraryName; }
set { adsLibraryName = value; }
}
///
/// Indicates whether errors should be shown in the page.
/// if true, then any error messages will be displayed in the Alt Text of the image
/// if false, then the visibility of the AdRotator will be set to false
///
[Bindable(false)]
[Category("Behavior")]
[DefaultValue(false)]
public bool DisplayErrors
{
get { return displayErrors; }
set { displayErrors = value; }
}
///
/// The display name of the SPField that contains the AlternativeText.
///
[Bindable(true)]
[Category("Behavior")]
[DefaultValue("Description")]
public string AlternativeTextFieldName
{
get { return alternativeTextFieldName; }
set { alternativeTextFieldName = value; }
}
#endregion
#region Privates
private string adsLibraryName;
private bool displayErrors;
private string alternativeTextFieldName;
///
/// Generates a random number between 0 and Max
///
/// The maximum number to randomly generate
/// A random number
private static int Randomiser(int max)
{
int index = 0;
Random rand = new Random(); //no need for anything more complex!
index = rand.Next(0, max);
return index;
}
#endregion
#region Overrides
///
/// Event that obtains a random image from the nominated image library
/// - ImageUrl is Url of the image in the image library
/// - NavigateUrl is the Title of the image in the image library
/// - AlternativeText is the DisplayName of the image in the image library
///
///
protected override void OnAdCreated(AdCreatedEventArgs e)
{
try
{
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists[adsLibraryName];
if (list == null)
throw new Exception("The Picture Library '" + adsLibraryName + "' could not be found.");
int resourceIndex = Randomiser(list.ItemCount);
SPListItem li = list.Items[resourceIndex];
string webUrl = web.ServerRelativeUrl;
if (webUrl != "/")
{
webUrl = webUrl + "/";
}
e.ImageUrl = webUrl + li.Url;
e.NavigateUrl = li.Title;
try
{
//Try and obtain the Alternative Text from the nominated field
e.AlternateText = li[alternativeTextFieldName].ToString();
}
catch (ArgumentException)
{
//If the nominated field does not exist then use "" as Alternative Text
e.AlternateText = "";
}
}
catch (Exception ex)
{
// If an exception occurs then output the message text to the Alternative Text of the image
// if displayErrors is true, otherwise hide the entire control from rendering.
if (displayErrors)
{
e.AlternateText = ex.Message;
}
else
{
this.Visible = false;
}
}
}
#endregion
}
}