Copied to clipboard

Flag this post as spam?

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


  • David Armitage 414 posts 1655 karma points
    Jun 07, 2020 @ 02:41
    David Armitage
    0

    Umbraco 8 | How to check the member doc type schema?

    Hi Guys,

    A slightly unusual requirement.

    I have some events that when someone saves a content node. Lets say for example a published event. This is just a standard content node (not member).

    As part of this even I want to check the member schema for specific fields. This is not related to a specific member I just literally want to check a property exists against the member schema.

    I am not going to get too into the why but as a simple explanation I am created a marketing package which uses custom member fields to do some filters. The idea is the user will enter the custom member field alias when creating the filter. I then want to check this exists against the member schema or not. If not I will throw and error.

    I know how to do all the events, throw the errors. My question is does anyone know how to request the member dot type schema so I can see what fields do and don't exists. I am guessing this is possible because the export doc type pretty much would need to do what I need.

    I thought of a hack around. 1. Always having a member created and never deleted. 2. Looking up this member to check the field exists against that member. 3. I can then obviously be sure the field exists against the schema.

    I was just wondering if anyone knows the correct way of doing this?

    Kind Regards

    David

  • Nik 1413 posts 6212 karma points MVP 3x c-trib
    Jun 07, 2020 @ 14:23
    Nik
    100

    Hi David,

    You could retrieve the Member Types using the Member Type service. Then could look to see if the property you are looking for is defined on there?

    Nik

  • David Armitage 414 posts 1655 karma points
    Jun 08, 2020 @ 01:25
    David Armitage
    1

    Hi Nik,

    Thanks for pointing me in the right direction that was exactly what I was looking for.

    https://our.umbraco.com/documentation/reference/management/services/membertypeservice/

    For completion here is the code I wrote.

    I am using the SavingEvent when users are adding new member marketing filters. I then use the MemberTypeService to check these fields exist and they are the correct datatype.

    If the field alias doesn't exist or the datatype is incorrect for the type of filter they are adding then an error is thrown and the filter isn't saved.

    private void ContentService_Saving(IContentService sender, ContentSavingEventArgs args)
            {
                foreach (var node in args.SavedEntities)
                {
                    try
                    {
                        string propertyType = "";
                        string fieldAlias = "";
    
                        if (node.ContentType.Alias == "umb8smmBooleanFilter")
                        {
                            propertyType = "Umbraco.TrueFalse";
                            fieldAlias = (string)node.Properties["memberFieldAlias"].GetValue();
                        }
                        else if (node.ContentType.Alias == "umb8smmDateFilter")
                        {
                            propertyType = "Umbraco.DateTime";
                            fieldAlias = (string)node.Properties["memberFieldAlias"].GetValue();
                        }
                        else if (node.ContentType.Alias == "umb8smmDecimalFilter")
                        {
                            propertyType = "Umbraco.Decimal";
                            fieldAlias = (string)node.Properties["memberFieldAlias"].GetValue();
                        }
                        else if (node.ContentType.Alias == "umb8smmNumericFilter")
                        {
                            propertyType = "Umbraco.Integer";
                            fieldAlias = (string)node.Properties["memberFieldAlias"].GetValue();
                        }
                        else if (node.ContentType.Alias == "umb8smmTextFilter")
                        {
                            propertyType = "Umbraco.TextBox";
                            fieldAlias = (string)node.Properties["memberFieldAlias"].GetValue();
                        }
    
                        if(!string.IsNullOrEmpty(propertyType) && !string.IsNullOrEmpty(fieldAlias))
                        {
                            var memberType = _memberTypeService.GetAll().FirstOrDefault();
                            if (memberType != null)
                            {
                                var boolField = memberType.PropertyTypes.FirstOrDefault(x => x.Alias == fieldAlias && x.PropertyEditorAlias == propertyType);
                                if (boolField == null)
                                {
                                    args.Cancel = true;
                                    args.Messages.Add(new EventMessage("Error Adding Filter!", "No " + propertyType + " field matching the provided alias (" + fieldAlias + ") was found on the member type (" + memberType.Alias + ").", EventMessageType.Error));
                                }
                            }
                            else
                            {
                                args.Cancel = true;
                                args.Messages.Add(new EventMessage("Error Adding Filter!", "There was no member type found. Please check your default Umrbaco member section setup.", EventMessageType.Error));
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.Error<SavingEvent>("ContentService_Saving | Exception: {0} | Message: {1}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "");
                    }
                }
            }
    

    Thanks for your assistance Nik & Ravi

    Regards

    David

  • Ravi Motha 289 posts 499 karma points MVP 4x c-trib
    Jun 07, 2020 @ 18:27
    Ravi Motha
    0

    If you have a member and you know the username or guid for the member you can use the memberservice to get that member and check the schema for fields and values as you need them

    https://our.umbraco.com/apidocs/v8/csharp/api/Umbraco.Core.Services.IMemberService.html

    Then you can check the values of the member type as if it was a doctype and this includes custom fields you have created

    https://our.umbraco.com/forum/developers/razor/68287-testing-if-current-member-has-a-property

    Like nik said he was just quicker than me

  • 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