Copied to clipboard

Flag this post as spam?

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


  • Proxicode 127 posts 323 karma points
    Mar 06, 2019 @ 21:03
    Proxicode
    0

    Save object to Member via MemberService

    Hey Everyone,

    I have extended the Member in BackOffice to have a Nested Content object on it called badges. Basically a logged in user can do things on the website to unlock/earn badges. I have an UmbracoAPIController handling the call from the front end which looks as shown

        [System.Web.Http.HttpGet]
        public IContent IsNewBadge(int newSteps)
        {
            var contentService = ApplicationContext.Services.ContentService;
            var memberService = ApplicationContext.Current.Services.MemberService;
    
            // all available badges
            var allBadges = contentService.GetById(1144).Children();
            var secMember = Membership.GetUser();
    
            if (secMember != null)
            {
                var currentMember = memberService.GetByUsername(secMember.UserName);
    
                // Get all badges this member currently has - or return a new empty list if null
                var allMemberBadges = currentMember.GetValue<List<IContent>>("badges") ?? new List<IContent>();
    
                foreach (var badge in allBadges)
                {
                    // check if the passed in number meets or exceeds a badge's required number
                    if (newSteps >= badge.GetValue<int>("badgeStepsRequired"))
                    {
                        // if the badge is not already in the list
                        if (!allMemberBadges.Contains(badge))
                        {
                            allMemberBadges.Add(badge);
                            currentMember.SetValue("badges", new List<IContent>(allMemberBadges));
                            memberService.Save(currentMember);
                            return badge;
                        }
                    }
                }
            }
            return null;
        }
    

    My question is specifically around the "SetValue" method. Is this even the correct method that I should be using? It seems to only want (string,string) or (string,filestream).

    The error that I get is

    "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetPropertyValue(string, string)' has some invalid arguments'"

    "The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetPropertyValue(string, string)' has some invalid arguments"

    How can I update my custom member property via API?

    Thanks for any suggestions! -Roger

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Mar 07, 2019 @ 15:49
    Alex Skrypnyk
    100

    Hi Roger

    Yes, this is the right method - "SetValue"

    I think in this row you need to convert List to string, serialize the list to JSON for example.

    currentMember.SetValue("badges", new List<IContent>(allMemberBadges))
    

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft