Add a different file for a new Image translation

The following code creates a new translation of an Image and uploads a new file only for this translation. You can use similar code to edit translations.

Native API

C#
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Localization;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity.Workflow;

namespace SitefinityWebApp
{
   public partial class MultilingualSupportFormediaContentSnippets
   {

       public static void CreateImageTranslationWithDifferentFileNativeAPI(CultureInfo culture, Guid masterImageId, string title, Stream imageStream, string imageFileName, string imageFileExtension)
       {
           // Ensure we are working in the correct culture.
           using (new CultureRegion(culture))
           {
               var librariesManager = LibrariesManager.GetManager();

               // Get the master version of the image
               var master = librariesManager.GetImages().FirstOrDefault(i => i.Id == masterImageId);

               if (master != null)
               {
                   // Check out the master to get a temp version.
                   var temp = librariesManager.Lifecycle.CheckOut(master) as Image;

                   temp.Title = title;
                   temp.LastModified = DateTime.UtcNow;
                   temp.UrlName = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                   temp.MediaFileUrlName = Regex.Replace(imageFileName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");

                   librariesManager.Upload(temp, imageStream, imageFileExtension);

                   // Recompiles and validates the url of the image.
                   librariesManager.RecompileAndValidateUrls(temp);

                   // Checkin the temp and get the updated master version. 
                   // After the check in the temp version is deleted.
                   master = librariesManager.Lifecycle.CheckIn(temp) as Image;

                   librariesManager.SaveChanges();

                   // Publish the Image.
                   var bag = new Dictionary<string, string>();
                   bag.Add("ContentType", typeof(Image).FullName);
                   WorkflowManager.MessageWorkflow(masterImageId, typeof(Image), null, "Publish", false, bag);
               }
           }
       }
   }
}

Fluent API

C#
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Localization;
using Telerik.Sitefinity.Workflow;

namespace SitefinityWebApp
{
   public class CreateImageTranslationWithDifferentFileFluentAPI
   {
       public static void CreateImageTranslationWithDifferentFileWithFluentAPI(CultureInfo culture, Guid masterImageId, string title, Stream imageStream, string imageFileName, string imageFileExtension)
       {
           // Ensure we are working in the correct culture.
           using (new CultureRegion(culture))
           {
               var count = 0;

               App.WorkWith().Images().Where(i => i.Id == masterImageId).Count(out count);

               if (count > 0)
               {
                   App.WorkWith().Image(masterImageId).CheckOut().Do(image =>
                   {
                       image.Title = title;
                       image.LastModified = DateTime.UtcNow;
                       image.UrlName = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                       image.MediaFileUrlName = Regex.Replace(imageFileName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                   }).UploadContent(imageStream, imageFileExtension).CheckIn().Publish().SaveChanges();
               }
           }
       }
   }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
This Article Contains
New to Sitefinity?