Quick tip: Adding Media Items from a URL

I needed to create new media items programatically and a quick trip via Google came up with this excellent piece of content.

Only thing is, I needed to fetch the image from a URL, not a file and came up with the following quick method;

private Stream GetImageStreamFromUrl(string url)
  using (var client = new WebClient()) 
    var imageBytes = client.DownloadData(url);
    var memoryStream = new MemoryStream(imageBytes);
    return memoryStream;

Simply call the method with the method as argument: CreateFromStream(GetImageStreamFromUrl("http://www.sitecore.net/logo.png"), "logo.png", options);

Posted in Sitecore Tagged with: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *