Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How do I rotate images from an image library on a SharePoint page? I want to do this using custom code not jQuery though. I'm using C#.

Anyone have an idea?

share|improve this question
Do you want to do this automatically when an image is uploaded or on demand from an images context menu? –  Muhimbi Sep 26 '10 at 7:52

3 Answers 3

You could do it server side with a control. Very basic and no ajax

<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.WebControls" %>
<script runat="server">
    private void Page_Load() {
        Random random = new Random();
        SPSite site = SPControl.GetContextSite(Context);
        SPWeb web = site.RootWeb;
        SPList list = web.Lists[listname];
        int number = list.Items.Count;
        SPListItem listItem = list.Items[random.Next(number)];
        txtImage.Text = site.ServerRelativeUrl + "/" + listItem.Url;
    private string listname = "Images";
    public string ListName {
        set { listname = value; }
        get { return listname; }

<img src="<asp:Literal ID="txtImage" runat="server" Text=""></asp:Literal>" />
share|improve this answer

Here is a pure JavaScript (no jQuery) solution:


It only works for authenticated users. For anonymous access, you'd need to use a different data source.

share|improve this answer

If you don't want to use client-side scripting (Javascript), the only (easy) solution would be to use the ASP.NET UpdatePanel, which is available in a .NET 3.x project. Just create a new webpart, add the UpdatPanel to it and you're good to go. This site has a nice example available: http://www.codegod.de/webappcodegod/updatepanel-tutorial-aspnet-ajax-AID281.aspx

Do note that you'll need .NET 3.x activated in the web.config. Jan Tielen has a nice blogpost on how you can do it the Lazy way: http://weblogs.asp.net/jan/archive/2008/10/10/enabling-net-3-5-in-sharepoint-2007-sites-the-lazy-way.aspx Or you could follow @zimmergren his tutorial on how to do it manually: http://www.zimmergren.net/archive/2008/09/22/how-to-get-up-and-running-with-net-3-5-in-your-sharepoint-environment.aspx

Good luck!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.