Copied to clipboard

Flag this post as spam?

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


  • Predrag Andrejević 10 posts 84 karma points
    Oct 11, 2019 @ 14:11
    Predrag Andrejević
    1

    Programmatically setting Member Picker property

    I have a document type that has Member Picker property editor and I need to set the value for it programmatically. Member that I need to put into that property is a custom Member Type. When I try to do it in the back office, it works, but I can't get it to work through an API. I've tried few things and it successfully creates node of that document type, but when I access that node through backoffice I always get this exception:

    System.InvalidOperationException: Multiple actions were found that match the request: GetByIds on type Umbraco.Web.Editors.EntityController GetByIds on type Umbraco.Web.Editors.EntityController GetByIds on type Umbraco.Web.Editors.EntityController

    Also, Member Picker doesn't show anything in node editor (as if it has no selected member).

    Thanks in advance!

  • Steve Megson 151 posts 1024 karma points MVP c-trib
    Oct 11, 2019 @ 16:13
    Steve Megson
    0

    What does your call to SetValue look like? That exception generally means that the saved value isn't in the format that the picker expected.

    The multiple actions that the exception refers to accept IDs in different formats. If the provided value doesn't match any of those formats then it tries to pick one of the actions to call with an empty list of IDs, but has no way to decide.

  • Predrag Andrejević 10 posts 84 karma points
    Oct 13, 2019 @ 09:46
    Predrag Andrejević
    0

    Hi Steve, thanks for your reply!

    Here's how my call of Set value looks:

    content.SetValue("prescriptionDoctorId", MemberHelper.SetMemberIdProperty(prescription));
    

    And here's how my Get and Set methods look:

    public static int GetMemberIdProperty(IPublishedContent node, string memberProperty)
        {
            int id = 0;
    
            if (node.HasValue(memberProperty))
            {
                var doctor = node.Value<IPublishedContent>(memberProperty);
                id = doctor.Id;
            }
    
            return id;
        }
    
        public static IPublishedContent SetMemberIdProperty(PrescriptionViewModel prescription)
        {
            IPublishedContent doctor = ContentHelper.umbracoHelper.Member(prescription.DoctorId);
    
            return doctor;
        }
    

    As I mentioned, Set method successfully creates new node, but when Get method is called, it throws this exception:

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    On this line:

    id = doctor.Id;
    

    Also, when I debugged those two methods, I've seen that doctor variable gets null value.

    One more thing. In documentation it says that Member Picker returns IPublishedContent, so I've tried to set it to that same type, but it doesn't work.

    PS PrescrtiptionViewModel is a view model for document type Prescription that has Member Picker property (I use it for CRUD methods to work with JSON that I recieve through a HTTP request).

  • Predrag Andrejević 10 posts 84 karma points
    Oct 13, 2019 @ 11:00
    Predrag Andrejević
    1

    I GOT IT!

    I've had similar problem with setting value for media picker and thought I could try doing the same thing AND IT WORKED!

    Here's how I did it:

    public static IMember SetMemberIdProperty(int memberId, ServiceContext service)
        {
            return service.MemberService.GetById(memberId);
        }
    

    And then in the call:

    content.SetValue("prescriptionDoctorId", MemberHelper.SetMemberIdProperty(prescription.DoctorId, service).GetUdi());
    

    Seems like you need Udi for setting values of all picker data types.

    Hope it helps someone some day too!

  • Steve Megson 151 posts 1024 karma points MVP c-trib
    Oct 14, 2019 @ 12:16
    Steve Megson
    0

    That makes sense, the pickers do store UDIs. When you pass the Member to SetValue, it ends up calling ToString() and storing the result in the database. But calling ToString() just returns "Umbraco.Core.Models.Member".

    Passing in a Member, Content or Media to set a picker seems a perfectly reasonable thing to do, though. It would be good to get SetValue to handle that and store the UDI for you, and similarly handle passing in an IEnumerable<Media> for multiple pickers.

  • David Armitage 508 posts 2078 karma points
    Jun 21, 2020 @ 04:40
    David Armitage
    0

    For completeness as per Predrag's post.

    If anyone is having trouble with Umbraco 8 then this is how I do it.

    You basically need to get the UDI of the media item and use this to pass into the media picker field.

    var content = _contentService.GetById("content-node-id-here");
    if (content != null)
    {
        content.SetValue("basicStringValue", "some-string-content");
        content.SetValue("anotherBasicStringValue", "some-string-content");
    
        IMedia media = _mediaService.GetById(mediaId);
        if(media != null)
        {
            content.SetValue("logo", media.GetUdi());
        }
    
        if(image != null)
             _contentService.SaveAndPublish(content);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft