Lightbox enabled custom webpart, displays images from sharepoint picture library

Here is the simple Lightbox enabled custom webpart which displays the images from sharepoint picture library.

Lightbox enabled webpart
Lightbox enabled webpart

Instructions to use:

  • Download Lightbox
  • After downloading, copy all the Lightbox files or folders to sharepoint 2007 website using sharepoint designer 2007.
  • Add the lightbox javascript and css reference tags in the head section of sharepoint site master page.
Add Lightbox reference tags
Add Lightbox reference tags
  • Create a Picture Library in sharepoint site naming Picture Library (you can also give any other name, but make sure to change the name in the downloaded source code too) upload few pictures to the library.
  • Create a webpart using visual studio, copy and paste the source code, build and import the webpart to sharepoint site.

Finally looks like this when you click on  a image

Lightbox enabled custom webpart
Lightbox enabled custom webpart

Download the complete source code

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace NewLightBox
{
    [Guid("148b47a8-0e84-423d-bb3f-44e5f2e33dea")]
    public class NewLightBox : System.Web.UI.WebControls.WebParts.WebPart
    {
        protected override void Render(HtmlTextWriter writer)
        {
            SPSite mysite = new SPSite("http://" + System.Environment.MachineName);
            mysite = SPControl.GetContextSite(Context);
            SPWeb myweb = mysite.OpenWeb();
            myweb = SPControl.GetContextWeb(Context);
            SPList mylist = myweb.Lists["Picture Library"];
            SPQuery myquery=new SPQuery();
            myquery.Query="";
            string path = mysite.ServerRelativeUrl.ToString() + "Picture Library/";
            SPListItemCollection mylstcol = mylist.GetItems(myquery);
            if (mylstcol.Count > 0)
            {                
                foreach (SPListItem mylstitem in mylstcol)
                {
                    writer.Write("");
                    writer.Write("");
                    writer.Write(" ");
                }
            }
            else
            {
                this.Page.Response.Write("No image found");
            }                  
            // TODO: add custom rendering code here.
            // writer.Write("Output HTML");
        }
    }
}