Home > Uncategorized > Retrieving random images from picture library in sharepoint

Retrieving random images from picture library in sharepoint

November 21st, 2005 Leave a comment Go to comments

If you are planning to create a web part that can retrieve and display a thumbnail of a random image from the picture library, then you have two options:


1) The images are stored as binary fields in the DOCS table inside the _SITE database. The field name is CONTENT. Simply connect to the database to retrieve the image. This is not the way I usually get stuff done in SPS.


2) I would suggest you access the images via the Sharepoint namespaces. Picture libraries store their image files in the same manner as document libraries store documents, so get a reference to the picture library as an SPList class and retrieve the image from the SPListItem.File property.


 


protected override void RenderWebPart(HtmlTextWriter output)
{
   string url;
   string caption;


   string HTMLtext;
    try
      {
         SPWeb mysite=SPControl.GetContextWeb(Context);
         SPListCollection lists=mysite.Lists;
         foreach (SPList list in lists)
            {
            if (list.Title.Equals(PicLib.ToString()))
                  {
                  SPListItemCollection items=list.Items;
                  int rndCount=items.Count;
                  if (rndCount > 0)
                     {
                         int intRandom=rIndex.Next(1,rndCount);
                         url= items[intRandom].File.Url;
                         caption=items[intRandom].File.Title;
                         HTMLtext= “some html text”;

                         output.Write(HTMLtext);
                     }
                  else
                    {
                  output.Write(“There is no picture in the selected picture library.”);
                    }
              }
          }
      }
catch(Exception e)
      {
      output.Write(SPEncode.HtmlEncode(e.Message.ToString()));
      }
}

Categories: Uncategorized Tags:
  1. No comments yet.
You must be logged in to post a comment.