Modify images
When editing an image, you must work with its master version. To modify an image, perform the following:
- Get the master version.
Get the master version of the image. When you modify an image, you must not work with its live version. - Get a temp version.
To get a temp version of the image, check out the master version . - Modify the temp version.
Set the properties of the temp version to the new values. You can also upload new image data. - Update the master version.
To transfer the changes to the master version, check in the temp version. - Update the live version.
To transfer the changes to the live version, publish the master version.
EXAMPLE: Modifying an image by the ID of its master version
The example below shows you how to modify an image by the ID of its master version. For more information about working with the ID of the live version, see For developers: Edit content in For developers: Content lifecycle.
The following code modifies an image by the ID of its master version.
Native API
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity.Workflow;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.Images
{
public partial class ImageSnippets
{
public void ModifyImageNativeAPI(Guid masterImageId, string newTitle, Stream imageStream, string imageFileName, string imageExtension)
{
LibrariesManager librariesManager = LibrariesManager.GetManager();
//Get the master version.
Image master = librariesManager.GetImages().Where(i => i.Id == masterImageId).FirstOrDefault();
if (master != null)
{
//Check out the master to get a temp version.
Image temp = librariesManager.Lifecycle.CheckOut(master) as Image;
//Make the modifications to the temp version.
temp.Title = newTitle;
temp.LastModified = DateTime.UtcNow;
temp.UrlName = Regex.Replace(newTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
temp.MediaFileUrlName = Regex.Replace(imageFileName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
librariesManager.Upload(temp, imageStream, imageExtension);
librariesManager.RecompileItemUrls(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);
}
}
}
}
First, you get an instance of the LibrariesManager class. Then, you get the master version with the corresponding ID. Then, to get a temp version of the image, you call Lifecycle.CheckOut with the master version as an argument. You make all the modifications to the temp version. In this example, you update the title of the image and its URL, and upload new image data. Then, to transfer the changes to the master version, you call Lifecycle.CheckIn with the temp version as an argument. By default, when calling the CheckIn method, the temp version gets deleted. To persist the changes, you call SaveChanges. Finally, to publish the master version, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.
Fluent API
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Libraries.Model;
using Telerik.Sitefinity.Workflow;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.MediaModules.Images
{
public partial class ImageSnippets
{
public void ModifyImageFluentAPI(Guid masterImageId, string newTitle, Stream imageStream, string imageFileName, string imageExtension)
{
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 = newTitle;
image.LastModified = DateTime.UtcNow;
image.UrlName = Regex.Replace(newTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
image.MediaFileUrlName = Regex.Replace(imageFileName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
}).UploadContent(imageStream, imageExtension).CheckIn().Publish().SaveChanges();
}
}
}
}
First, you check whether an image with the specified ID exists. Then, you get the singular image facade of the master version with the specified ID. Then, to get the facade for the temp version, you call the CheckOut method. You make all the modifications in the Do method of the temp facade. In this example, you update the title of the image and its URL, and upload new image data. To upload new image data, you call the UploadContent and pass the data as an argument. Then, to transfer the changes to the master version of the item, you call CheckIn. To save the changes, you call SaveChanges. Finally, to publish the image, you call the MessageWorkflow method of the WorkflowManager class and pass the required parameters.