What is the proper method to programmatically create a copy of a sitefinity Image?
I am using sitefinity 5 and trying to programmatically copy an existing image to the default library but no matter what i try, the thumbnails don't seem to work properly after. my copy code is below. 'existingimage' is the master of the imge to be copied.
public Telerik.Sitefinity.Libraries.Model.Image CopySitefinityImageToDefaultLibrary(string ImageTitle, Telerik.Sitefinity.Libraries.Model.Image existingimage)
Telerik.Sitefinity.Libraries.Model.Image newimage = null;
if (existingimage != null && !String.IsNullOrEmpty(ImageTitle))
LibrariesManager lman = LibrariesManager.GetManager();
Album defaultAlbum = FindAlbums("Default Library").FirstOrDefault();
if (defaultAlbum != null)
newimage = lman.CreateImage();
lman.Copy(existingimage, newimage);
lman.CopyThumbnails(existingimage, newimage );
newimage.Title = ImageTitle;
newimage.Urls.Clear();
newimage.UrlName = Regex.Replace(ImageTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
newimage.DateCreated = DateTime.UtcNow;
newimage.LastModified = DateTime.UtcNow;
newimage.InheritsPermissions = true;
newimage.Parent = defaultAlbum;
try
lman.RecompileItemUrls<
Telerik.Sitefinity.Libraries.Model.Image
>(newimage);
lman.SaveChanges();
var bag = new Dictionary<
string
, string>();
bag.Add("ContentType", typeof(Telerik.Sitefinity.Libraries.Model.Image).FullName);
WorkflowManager.MessageWorkflow(newimage.Id, typeof(Telerik.Sitefinity.Libraries.Model.Image), null, "Publish", false, bag);
catch
return newimage;
UPDATE:
The copy wouldn't work properly so i figured i would just go with moving an image from one library to another. Sitefinity has a function called librarymanager.MoveItemToLibrary. i tried that but still i get problems with the images afterwards. so i started digging into the DB.
Image temp = lman.Lifecycle.CheckOut(imageMaster) as Image;
temp.Album = album;
temp.Parent = album;
temp.Title = newLogoTitle;
temp.FilePath = album.UrlName + "/" + newLogoTitle + imageMaster.Extension;
temp.Urls.Clear();
temp.UrlName = Regex.Replace(newLogoTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
lman.RecompileItemUrls<
Telerik.Sitefinity.Libraries.Model.Image
>(temp);
imageMaster = lman.Lifecycle.CheckIn(temp) as Telerik.Sitefinity.Libraries.Model.Image;
lman.SaveChanges();
var bag = new Dictionary<
string
, string>();
bag.Add("ContentType", typeof(Telerik.Sitefinity.Libraries.Model.Image).FullName);
WorkflowManager.MessageWorkflow(imageMaster.Id, typeof(Telerik.Sitefinity.Libraries.Model.Image), null, "Publish", false, bag);
Hello,
Thank you for contacting Telerik Support.
Actually we would recommend using MoveItemToLibrary, as this is the safest method that at the same time would ensure no duplicate records, since what it actually does is an internal call to ContentManagerBase.ChangeItemParent() and would not copy twice the actual media content, but just arrange additional URLs and Sitefinity Image representation in the desired library.
For the user it would appear as if there are two separate images in both libraries, while actually there's two MediaContent Images (i.e. Sitefinity images) pointing to the same binary content in the database.
You can even copy the image and set new properties to it,, for example you can easily change the title like this:
protected
void
Page_Load(
object
sender, EventArgs e)
var manager = LibrariesManager.GetManager();
var originalAlbum = manager.GetAlbums().Where(al => al.Title ==
"Default Library"
).SingleOrDefault();
var img = manager.GetImages().Where(im => im.Title ==
"Desert"
&& im.Status == ContentLifecycleStatus.Live && im.Parent.Id == originalAlbum.Id).SingleOrDefault();
if
(img !=
null
)
var albumToCopy = manager.GetAlbums().Where(al => al.Title ==
"NewLIbrary"
).SingleOrDefault();
if
(albumToCopy !=
null
)
MoveImageToLibraryAndRename(manager.GetMaster(img), albumToCopy,
"test"
);
manager.SaveChanges();
public
bool
MoveImageToLibraryAndRename(Telerik.Sitefinity.Libraries.Model.Image imageMaster, Album targetAlbum,
string
newLogoTitle)
bool
success =
true
;
var lman = LibrariesManager.GetManager();
try
lman.MoveItemToLibrary(imageMaster, targetAlbum);
lman.SaveChanges();
imageMaster.Title = newLogoTitle;
imageMaster.UrlName = Regex.Replace(newLogoTitle.ToLower(), @
"[^\w\-\!\$\'\(\)\=\@\d_]+"
,
"-"
);
lman.Lifecycle.Publish(imageMaster);
lman.SaveChanges();
catch
success =
false
;
return
success;
Hello Boyan, thanks for your reply. between this and my support ticket i think we have this figured out.
LibrariesManager lman = LibrariesManager.GetManager();
lman.MoveItemToLibrary(imageMaster, album);
lman.SaveChanges();
if (!String.IsNullOrEmpty(newLogoTitle))
lman.ClearItemUrls<
Telerik.Sitefinity.Libraries.Model.Image
>(imageMaster);
lman.SaveChanges();
imageMaster.Title = newLogoTitle;
imageMaster.UrlName = Regex.Replace(newLogoTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
imageMaster.FilePath = imageMaster.UrlName + "/" + newLogoTitle + imageMaster.Extension;
lman.RecompileItemUrls<
Telerik.Sitefinity.Libraries.Model.Image
>(imageMaster);
lman.Lifecycle.Publish(imageMaster);
lman.SaveChanges();