SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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:

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: Or you could follow @zimmergren his tutorial on how to do it manually:

Good luck!

share|improve this answer

Your Answer


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