+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Fluent API Image Upload - Unable to query uploaded image by ID

Fluent API Image Upload - Unable to query uploaded image by ID

2 posts, 0 answered
  1. Tomasz
    Tomasz avatar
    5 posts
    Registered:
    27 Nov 2014
    27 Nov 2014
    Link to this post

    Hi,

     I'm using Sitefinity 7.0 to programmatically access the Sitefinity's content management system.

    Here is how I upload the image:

    public static Guid CreateImage(Guid imageId, Guid parentAlbumId, string title, Stream stream, string extension)
            {
                var count = 0;
                imageId = Guid.NewGuid();
     
                App.WorkWith().Albums().Where(i => i.Id == parentAlbumId).Count(out count);
     
                if (count > 0)
                {
                    App.WorkWith().Album(parentAlbumId).CreateImage().CheckOut()
     
                    .Do(image =>
                    {
                        imageId = image.Id;
                        image.Title = title;
                        image.DateCreated = DateTime.UtcNow;
                        image.PublicationDate = DateTime.UtcNow;
                        image.LastModified = DateTime.UtcNow;
                        image.Urls.Clear();
                        image.UrlName = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
                    })
     
                    .UploadContent(stream, extension)
     
                    .SaveChanges();
     
                    var bag = new Dictionary<string, string>();
                    bag.Add("ContentType", typeof(Image).FullName);
                    WorkflowManager.MessageWorkflow(imageId, typeof(Image), null, "Publish", true, bag);
                     
                }
     
                return imageId;
            }

     This is taken from Sitefinity's examples. The image uploads to given album successfully. However when I try to query for the image programmatically like this:

    public static string GetMediaUrlByImageId(Guid imageId, Guid albumId, bool resolveAsAbsolutUrl)
            {
                Album album = App.WorkWith().Albums().Where(a => a.Id == albumId).Get().FirstOrDefault();
                IQueryable<Image> images = album.Images();
                Image image = images.Where(i => i.Id == imageId).FirstOrDefault();
     
                Image imagee1 = App.WorkWith().Images().Where(i => i.Id == imageId).Get().FirstOrDefault();
     
                var manager = LibrariesManager.GetManager();
     
                //var image = manager.GetImages().FirstOrDefault(i => i.Id == imageId);
     
                var mediaUrl = String.Empty;
     
                if (image != null)
                {
                    mediaUrl = image.ResolveMediaUrl(resolveAsAbsolutUrl);
                }
     
                return mediaUrl;
            }

    There is no image with given ID which I received from the file upload function CreateImage , when debugging both variables image and imagee1 are null. Could you please help me in establishing what I'm doing wrong ?

  2. Tomasz
    Tomasz avatar
    5 posts
    Registered:
    27 Nov 2014
    27 Nov 2014
    Link to this post
    Ok, nevermind , resolved , got my ID under OriginalContentId property of an image.
2 posts, 0 answered