Spt2432
Spt2432

Reputation: 234

Retrieve Image from Picture Library - REST

I am looking for some suggestion or sample around retrieving images (actual file, not URL), from a picture library using REST API.

Thanks for any input.

Upvotes: 1

Views: 2850

Answers (1)

Tjassens
Tjassens

Reputation: 922

Task 1: Getting a List of Image libs on a given site

public static XmlNode GetPicLibListingXML(string imagingServiceURL)
 {

                Imaging wsImaging = new Imaging();
                wsImaging.UseDefaultCredentials = true;
                wsImaging.Url = imagingServiceURL;
                XmlNode xnPicLibs = wsImaging.ListPictureLibrary();

                return xnPicLibs;
   }

Sample return XML:

<Library name="{3C1D52F5-5387-490A-9A2D-A9C99A208C00}" title="Tech Images" guid="3c1d52f5-5387-490a-9a2d-a9c99a208c00" url="Tech Images" xmlns="http://schemas.microsoft.com/sharepoint/soap/ois/" />

Task 2: Listing Images in a given library

public static XmlNode GetImageFileListing(string imagingServiceURL, string imageFileLibraryName)

 {
      Imaging wsImaging = new Imaging();
      ImageInfo curImageInfo = new ImageInfo();
       wsImaging.UseDefaultCredentials = true;
       wsImaging.Url = imagingServiceURL;
       XmlNode xnListItems = wsImaging.GetListItems(imageFileLibraryName, "");

       return xnListItems;
   }

Task 3: Download Image(s)

private const string ATTR_FILENAME = "name";

private const string FILENAMESPACEURI = "http://schemas.microsoft.com/sharepoint/soap/ois/";

public static bool DownloadImageFiles(string imagingServiceURL, string imageFileLibraryName, string[] fileNames, string saveToFolder)

 {
        Imaging wsImaging = new Imaging(); 
        wsImaging.UseDefaultCredentials = true;
        wsImaging.Url = imagingServiceURL;

       XmlElement parent = (XmlElement)wsImaging.Download(imageFileLibraryName, string.Empty, fileNames, 0, true);

       XmlNodeList files = parent.GetElementsByTagName("File", FILENAMESPACEURI);

        foreach (XmlNode file in files)
        {
             if (Directory.Exists(saveToFolder) == false)
            {
                 Directory.CreateDirectory(saveToFolder);
              }

           byte[] fileBytes = Convert.FromBase64String(file.InnerText);

            using (FileStream fs = File.OpenWrite(saveToFolder + file.Attributes[ATTR_FILENAME].Value))
             {
                    BinaryWriter writer = new BinaryWriter(fs);
                    writer.Write(fileBytes);
                    writer.Close();
                }
          }  
          return true;
        }

Note:

  • Imaging() class is a web reference to imagining.asmx
  • The Download call natively returns XML so yo uneed to run it through a conversion to byte
  • If you need to get a reference on the Imagine web service check this on out on MSDN:

http://msdn.microsoft.com/en-us/library/imaging.imaging.aspx

source:

http://gourangaland.wordpress.com/2008/05/30/using-the-moss-imaging-web-service-to-download-imagesimaging-asmx/

Upvotes: 1

Related Questions