Add a file from another translation to a new Image translation

This article contains
NEW TO SITEFINITY?

The following code creates a new translation for an image and copies an existing file from another translation. You can use similar code to edit translations.

Native API

using System.Collections.Generic;
using Telerik.Sitefinity.Workflow;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Localization;
using Telerik.Sitefinity.Modules.Libraries;
using System;
using System.Text.RegularExpressions;
using System.Linq;
using System.Globalization;
namespace SitefinityWebApp
{
public class CreateImageTranslationUsingExistingFileNativeAPI
{
public static void CreateImageTranslationUsingExistingFileWithNativeAPI(CultureInfo culture, Guid masterImageId, string title, CultureInfo sourceCulture)
{
// 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_]+", "-");
// Copies the file used by the existing traslation for the source culture.
librariesManager.CopyUploadedContent(temp, sourceCulture);
// 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

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Localization;
using Telerik.Sitefinity.Workflow;
namespace SitefinityWebApp
{
public class CreateImageTranslationUsingExistingFileFluentAPI
{
public static void CreateImageTranslationUsingExistingFileWithFluentAPI(CultureInfo culture, Guid masterImageId, string title, CultureInfo sourceCulture)
{
// 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().CopyUploadedContent(sourceCulture).Do(image =>
{
image.Title = title;
image.LastModified = DateTime.UtcNow;
image.UrlName = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
}).CheckIn().Publish().SaveChanges();
}
}
}
}
}

Want to learn more?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?