Images uploaded with the API show draft status on publish

Posted by Community Admin on 04-Aug-2018 03:05

Images uploaded with the API show draft status on publish

All Replies

Posted by Community Admin on 02-Aug-2012 00:00

Do I have the code wrong?

See attached...they have both the green publishes state, but the text of draft.  If I open one, then click Publish, the text then properly changes to Published though.

So an I missing something?

public void UploadImage(System.IO.Stream stream, string name, string extension, string fullname)
        
            try
            
                this.LibraryManager.Provider.SuppressSecurityChecks = true;
                Telerik.Sitefinity.Libraries.Model.Image newImage = LibraryManager.CreateImage();
 
                //Set its album
                newImage.Parent = this.LibraryManager.GetAlbums().FirstOrDefault(x => x.Title == "Content");
 
                var imageName = name;
                newImage.Title = imageName;
                newImage.UrlName = Util.GetUrl(imageName);
 
                LibraryManager.Upload(newImage, stream, extension);
                LibraryManager.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Image>(newImage);
                LibraryManager.Publish(newImage);
                LibraryManager.SaveChanges();
            catch(Exception ex)
                errorsLiteral.Text = fullname + "<br />";
            
        


Posted by Community Admin on 06-Aug-2012 00:00

Hi Steve,

I have taken a look at your code and the problem most likely comes from the fact that the you have not informed the workflow about the changes. Can you try to run your code with the modifications that I have made to it:

public void UploadImage(System.IO.Stream stream, Guid masterImageId, string name, string extension, string fullname)
  
 
     
      try
      
          this.LibraryManager.Provider.SuppressSecurityChecks = true;
          Telerik.Sitefinity.Libraries.Model.Image newImage = LibraryManager.CreateImage(masterImageId);
 
          //Set its album
          newImage.Parent = this.LibraryManager.GetAlbums().FirstOrDefault(x => x.Title == "Content");
 
          var imageName = name;
          newImage.Title = imageName;
          newImage.UrlName = Util.GetUrl(imageName);
 
          LibraryManager.Upload(newImage, stream, extension);
          LibraryManager.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Image>(newImage);
          LibraryManager.Publish(newImage);
          LibraryManager.SaveChanges();
 
          var bag = new Dictionary<string, string>();
          bag.Add("ContentType", typeof(Image).FullName);
          WorkflowManager.MessageWorkflow(masterImageId, typeof(Image), null, "Publish", false, bag);
      
      catch (Exception ex)
      
           errorsLiteral.Text = fullname + "<br />";
        
  

There is also another way you can approach the uploading process. Here is another sample code:

public void UploadImageFluent()
       
           var request = System.Net.WebRequest.Create("http://localhost:9999/images/test/dog.jpg") as System.Net.HttpWebRequest;
           var response = request.GetResponse() as System.Net.HttpWebResponse;
           using (MemoryStream stream = new MemoryStream())
           
               using (var source = response.GetResponseStream())
               
                   source.CopyTo(stream);
               
 
               var fluent = App.WorkWith();
               var userUploadAlbum = fluent.Albums().Where(a => a.UrlName == "default-album").Get().SingleOrDefault();
               var newImage = fluent.Image().CreateNew().Do(
                   image =>
                   
                       image.Description = "Image Description";
                       image.Author = "Image Author";
                       image.Parent = userUploadAlbum;
                       image.Title = "Image Title";
                       image.UrlName = "image-title";
                       image.Extension = ".jpg";
                   )
               .CheckOut()
               .UploadContent(stream, ".jpg")
               .CheckIn()
               .Do(image => image.ApprovalWorkflowState = "Published"; )
               .Publish()
               .SaveChanges();
           
       

All the best,
Victor Velev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 06-Aug-2012 00:00

Ah thanks Victor, that must be it

Can you create a new PITS issue to wrap that workflow code INTO LibraryManager.Publish(newImage);  It's completely retarded for it to be a separate item when I'm calling a PUBLISH method...it's like saying "Publish this item, but not REALLY..."

Perhaps an override to be like
LibraryManager.Publish(newImage, true);  telling it to call that workflow code?

Posted by Community Admin on 06-Aug-2012 00:00

seems weird indeed...

So what's gonna happen with Dropbox in v5.2 ? Anything added gets 'draft' statement and as soon as a Dropbox hickup occurs and something gets resynced it'll get unpublished?

Jochem.

Posted by Community Admin on 06-Aug-2012 00:00

Hey Victor,
  Okay so I modified it like this

public void UploadImage(System.IO.Stream stream, string name, string extension, string fullname, string libraryName)
    try
    
        Guid imageID = Guid.NewGuid();
        this.LibraryManager.Provider.SuppressSecurityChecks = true;
        Telerik.Sitefinity.Libraries.Model.Image newImage = LibraryManager.CreateImage(imageID);
 
        //Set its album
        var album = this.LibraryManager.GetAlbums().FirstOrDefault(x => x.Title == libraryName);
        if (album.Images().Count(x => x.Title == name) == 0)
        
            newImage.Description = "";
            newImage.Author = "sleuthagain";
            newImage.Parent = album;
            newImage.Title = name;
            newImage.UrlName = Util.GetUrl(name);
            newImage.Extension = extension;
 
            LibraryManager.Upload(newImage, stream, extension);
            LibraryManager.RecompileItemUrls<Telerik.Sitefinity.Libraries.Model.Image>(newImage);
            LibraryManager.Lifecycle.Publish(newImage);
            LibraryManager.SaveChanges();
 
            var bag = new Dictionary<string, string>();
            bag.Add("ContentType", typeof(Image).FullName);
            WorkflowManager.MessageWorkflow(imageID, typeof(Image), null, "Publish", false, bag);
 
            _count++;
        
    catch(Exception ex)
        errorsLiteral.Text = fullname + "<br />";
    

So when I ran the script...it told me 0 items imported, and now when I visit /Sitefinity/Content/Images it just perpetually loads. b/c the images service is returning aborted.

http://screencast.com/t/ZdRDRxMLs1

Posted by Community Admin on 06-Aug-2012 00:00

@Victor missing this?

WorkflowManager.MessageWorkflow(imageID, typeof(Image), LibraryManager.Provider.ApplicationName, "Publish", false, bag);

Posted by Community Admin on 06-Aug-2012 00:00

Ok victor, that fluent code worked, going in published now :)

...however I still can't link those images to a module builder item in code...using the sample code.

screencast.com/.../hxrCnrVuNT

var image = this.MyLibraryManager.GetImages().FirstOrDefault(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true && i.Title.ToString().ToLower() == imageToFind);
                    if (image != null)
                    
                        dictionaryEntryItem.AddImage("MainImage", image.Id);

So clearly it links the right image in because it exists in edit mode...but why do they not show up in the masterview list until re-select and publish?  Makes me think the sample code is missing some critical step.

This thread is closed