Progromatic Album and Image Creation Issue

Posted by Community Admin on 04-Aug-2018 17:35

Progromatic Album and Image Creation Issue

All Replies

Posted by Community Admin on 12-Dec-2014 00:00

I am able to programmatically create an album, and images. But when I try to publish the images I get the following error.

Telerik.Sitefinity.Libraries.Model.Album, Telerik.Sitefinity.Model was not granted ManageImage in Image for principals with IDs 00000000-0000-0000-0000-000000000000 

 Note:  This code is "simplified" greatly, but shows the flow of my actual implementation to a degree.

bool origSuppressSecurityChecksValue = WorkflowManager.GetManager().Provider.SuppressSecurityChecks;
LibrariesManager libMgr = LibrariesManager.GetManager();
Album album = libMgr.GetAlbums().SingleOrDefault(i => i.Title == BlogImageLibraryName);
if (album == null)
   libMgr.Provider.SuppressSecurityChecks = true;
   //Create the album.
   album = libMgr.CreateAlbum();
   //Set the properties of the album.]
   //Recompiles and validates the url of the album.
   libMgr.RecompileAndValidateUrls(album);
 
   //Save the changes.
   libMgr.SaveChanges();
   libMgr.Provider.SuppressSecurityChecks = false;
string imageTitle = Path.GetFileNameWithoutExtension(fileName);
Image image = album.Images().FirstOrDefault(i => i.Title == imageTitle);
if (image == null)
 
   libMgr.Provider.SuppressSecurityChecks = true;
   image = libMgr.CreateImage(); // actually create an image
   image.Parent = album;
   [//Set the properties of the album post.]
 
    //Upload the image file.
    libMgr.Upload(image, imageStream, Path.GetExtension(fileName));
    //Save the changes.
    libMgr.SaveChanges();
    libMgr.Provider.SuppressSecurityChecks = false;
     
    //Publish the Albums item. The live version acquires new ID.
    var bag = new Dictionary<string, string>();
    bag.Add("ContentType", typeof(Image).FullName);
     
    WorkflowManager.GetManager().Provider.SuppressSecurityChecks = true;
     
    // THIS LINE WILL THROW THE EXCEPTION ABOVE
    WorkflowManager.MessageWorkflow(image.Id, typeof(Image), null, "Publish", false, bag);
  

I'm missing a concept here somehow...maybe the default provider is not the one I need for the Workflow Suppress?

Posted by Community Admin on 12-Dec-2014 00:00

Hi Stephen,

You can use this code to create a new image and publish it:

var librariesManager = LibrariesManager.GetManager(LibrariesManager.GetDefaultProviderName());
 
// Create a new image
var image = librariesManager.CreateImage();
 
// Assign the right library based on the culture
if (Equals(culture, CultureInfo.GetCultureInfo("nl"))) image.Parent = parentNl;
if (Equals(culture, CultureInfo.GetCultureInfo("en"))) image.Parent = parentUs;
 
image.Title[culture] = title;
image.UrlName[culture] = Regex.Replace(title.ToLower(), UrlNameCharsToReplace, UrlNameReplaceString);
 
// Set the right workflow state
image.ApprovalWorkflowState.Value = ApprovalWorkflowStatePublished;
 
// Upload
librariesManager.Upload(image, foundFile.OpenRead(), foundFile.Extension);
 
librariesManager.RecompileItemUrls(image);
 
// Publish
librariesManager.Lifecycle.Publish(image, culture);
 
// Save changes
librariesManager.SaveChanges();

I use this static string to hold the right workflow state

public static readonly string ApprovalWorkflowStatePublished = "Published";

It is easier to use the LifeCycle methods from the manager, rather than the WorkFlowManager.

I hope this is of any help.

Best regards,
Daniel

Posted by Community Admin on 15-Dec-2014 00:00

[quote]Daniel Plomp said:

 

It is easier to use the LifeCycle methods from the manager, rather than the WorkFlowManager. 

I hope this is of any help.[/quote]

It sure was Daniel - was exactly what I needed!  Everything else went smoothly, and now this is as well. 

Thank you, kindly!

This thread is closed