The best way to learn the API is to see how the web front end accomplishes tasks. So, to see how to add a file to the media gallery, take a look at how the Add Objects page does it (~/task/addobjects.aspx). Since my boy is napping and I have a few minutes, I'll write up a 2-minute tutorial.
The web project gsweb interacts with the business layer to get just about everything done. Within the business layer, you will often start with the Factory class to instantiate objects. To get a reference to the top level album, use this:
Code:IAlbum rootAlbum = Factory.LoadRootAlbumInstance();
To iterate through the child objects of the root album:
Code:foreach (IGalleryObject galleryObject in rootAlbum.GetChildGalleryObjects())
{
string title = galleryObject.Title;
IDisplayObject thumbnail = galleryObject.Thumbnail;
IDisplayObject compressedImage = galleryObject.Optimized;
IDisplayObject originalFile = galleryObject.Original;
}
The child gallery object may be an album, image, video, audio, generic media object, or external media object. Each of these map to a different class that inherits from the GalleryObject class.
The GetChildGalleryObjects method has overloads to help you return them sorted, or maybe you only want albums, or maybe you only want the photos.
To add an image to the root album:
Code:IAlbum rootAlbum = Factory.LoadRootAlbumInstance();
string pathToImage = @"C:\mypic.jpg";
IGalleryObject galleryObject = Factory.CreateMediaObjectInstance(pathToImage, rootAlbum);
galleryObject.CreatedByUserName = HttpContext.Current.User.Identity.Name;
galleryObject.DateAdded = DateTime.Now;
galleryObject.Save();
To generate an URL to an object take a look at the GetMediaObjectUrl method in the GspPage class at ~/gsweb/CodeFiles/. This class is the base class for all pages, so the method is available to every page. For example:
Code:string thumbnailUrl = GetMediaObjectUrl(galleryObject, DisplayObjectType.Thumbnail);
This gives you an url that uses the ASHX handler you can assign to an <img> tag. It'll look something like this: /dev/gs/handler/getmediaobject.ashx?moid=34&aid=8&mo=C%3A%5Cgs%5Cmypics%5Cbirthday.jpeg&mtc=1&dt=1&isp=false
If you have encryptMediaObjectUrlOnClient="true" in galleryserverpro.config (which is the default), the query string parameter will be encrypted so that end users won't be able to discover the path to your files.
Thanks for the bug report about the resource. It is now fixed.
Oops, I have a poopy diaper to change. Gotta run!
Roger Martin
Lead Developer for Gallery Server Pro