Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Henrik Sunesen 84 posts 281 karma points
    Aug 29, 2016 @ 15:21
    Henrik Sunesen
    0

    Get custom property from image/media in Surface controller

    Hello,

    I'm trying to get a collection af alle my images with a specific value in one of the properties:

    model.memberid = int (the value is currect).

    var currectImage = Umbraco.TypedMedia(1291).Children.Where(x => x != null && x.GetProperty("customCreatorId").Value.Equals(model.memberid));
    

    it returns "Object reference not set to an instance of an object."

    You can see the whole code below:

     var currectImage = Umbraco.TypedMedia(1291).Children.Where(x => x != null && x.GetProperty("customCreatorId").Value.Equals(1288));
    
    
                if (currectImage != null)
                {
                    if (currectImage.Count() > 1)
                    {
                        var imageToDelete = currectImage.FirstOrDefault();
                        var deleteMedia = Services.MediaService.GetById(imageToDelete.Id);
    
                        Services.MediaService.Delete(deleteMedia);
                    }
    
                    member.SetValue("image", currectImage.FirstOrDefault().Id);
                }
    

    Hope someone can tell me why it fails.

  • Damiaan 442 posts 1301 karma points MVP 6x c-trib
    Aug 29, 2016 @ 15:23
    Damiaan
    1

    It's probably the .Value in .GetProperty("customCreatorId").Value

    Is it possible that there is a child node with an empty customCreatorId?

    You can try .GetPropertyValue<string>("customCreatorId")

  • Henrik Sunesen 84 posts 281 karma points
    Aug 29, 2016 @ 15:30
    Henrik Sunesen
    0

    Hi Damiaan,

    I feel so stupid, you were right, that one of the images needed a property value.

    thanks !

Please Sign in or register to post replies

Write your reply to:

Draft