Skip and go to main content

Design & Technology August 03, 2011

Uploading Images using the Core Service in SDL Tridion 2011

Image

Internally we have a really useful batch image uploader that uses the native .Net API for Tridion 2009. The code sample below shows how I have re-written this to work with the new Core Service in Tridion 2011. The sample method accepts a location for where the images are stored (relative to the application server the website is running on), the TcmId of the folder to upload the images into and the CoreService2010Client object:

[code lang=”c-sharp”]
public static void UploadImages(string location, string folderTcmId, CoreService2010Client client)
{
//create a reference to the directory of where the images are
DirectoryInfo directory = new DirectoryInfo(location);
//create global Tridion Read Options
ReadOptions readOptions = new ReadOptions();
//use Expanded so that Tridion exposes the TcmId of the newly created component
readOptions.LoadFlags = LoadFlags.Expanded;
try
{
//loop through the files
foreach (FileInfo fileInfo in directory.GetFiles())
{
//only allow images
if (IsAllowedFileType(fileInfo.Extension))
{
try
{
//create a new multimedia component in the folder specified
ComponentData multimediaComponent = (ComponentData)client.GetDefaultData(Tridion.ItemType.Component, folderTcmId);
multimediaComponent.Title = fileInfo.Name.ToLower();
multimediaComponent.ComponentType = ComponentType.Multimedia;
multimediaComponent.Schema.IdRef = ConfigurationManager.AppSettings[“MultimediaSchemaId”];


//create a string to hold the temporary location of the image to use later
string tempLocation = “”;


//use the StreamUpload2010Client to upload the image into Tridion
UploadResponse us = new UploadResponse();
using (Tridion.StreamUpload2010Client streamClient = new StreamUpload2010Client())
{
FileStream objfilestream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);
tempLocation = streamClient.UploadBinaryContent(fileInfo.Name.ToLower(), objfilestream);
}

//create a new binary component
BinaryContentData binaryContent = new BinaryContentData();
//set this temporary upload location to the source of this binary
binaryContent.UploadFromFile = tempLocation;
binaryContent.Filename = fileInfo.Name.ToLower();
//get the multimedia type id
binaryContent.MultimediaType = new LinkToMultimediaTypeData() { IdRef = GetMultimediaTypeId(fileInfo.Extension) };
multimediaComponent.BinaryContent = binaryContent;

//save the image into a new object
IdentifiableObjectData savedComponent = client.Save(multimediaComponent, readOptions);

//check in using the Id of the new object
client.CheckIn(savedComponent.Id, null);
}
catch (Exception ex)
{
Log.Debug(“Error creating image ” + fileInfo.Name, ex);
}
}
}
}
catch (Exception ex)
{
Log.Error(“Error processing images”, ex);
}
}
[/code]

One thing to watch out for is that sometimes Tridion does not clean up the temporary files so I would recommend writing a method to delete the temporary file after the component has been successfully created.

I also created a few utility methods. The first method check’s for a valid file type:

[code lang=”c-sharp”]
private static bool IsAllowedFileType(string fileExtension)
{
bool allowed = false;
switch (fileExtension.TrimStart(‘.’).ToLower())
{
case “jpg”:
case “jpeg”:
case “jpe”:
case “gif”:
case “png”:
allowed = true;
break;
}
return allowed;
}
[/code]

The second method was to get the correct Tcm Id of the binary type:


[code lang=”c-sharp”]
private static string GetMultimediaTypeId(string fileExtension)
{
string tcmId = “”;
switch (fileExtension.TrimStart(‘.’).ToLower())
{
case “jpg”:
case “jpeg”:
case “jpe”:
tcmId = ConfigurationManager.AppSettings[“JpegId”];
break;
case “gif”:
tcmId = ConfigurationManager.AppSettings[“GifId”];
break;
case “png”:
tcmId = ConfigurationManager.AppSettings[“PngId”];
break;
}
return tcmId;
}
[/code]

Questions? We are here to help!