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 85 posts 282 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 1302 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 85 posts 282 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 !

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies