Copied to clipboard

Flag this post as spam?

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


  • Toppers 31 posts 155 karma points
    Aug 06, 2016 @ 08:46
    Toppers
    0

    How to find if MemberGroup Exists

    As a newbie I'm creating MemberGroups programatically but want to check if it already exists before I create it - which routine do I use please?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Aug 06, 2016 @ 10:22
    Sebastiaan Janssen
    100

    You can do that using the Services API:

    var umbracoContext = UmbracoContext.Current;
    var services = umbracoContext.Application.Services;
    var groupExists = services.MemberGroupService.GetAll().Any(x => x.Name == "MyGroupName");
    
  • Toppers 31 posts 155 karma points
    Aug 06, 2016 @ 10:39
    Toppers
    0

    Excellent - Thank you!

  • Toppers 31 posts 155 karma points
    Aug 06, 2016 @ 11:02
    Toppers
    0

    Apologies - should have mentioned I'm using V7 and getting an error on the code:

    services.MemberGroupService.GetAll().Any(x => x.Name == "MyGroupName");
    

    Error is: IEnumerable<IMemberGroup> does not contain a definition for 'Any'

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Aug 06, 2016 @ 11:22
    Sebastiaan Janssen
    0

    Make sure to have a reference to System.Linq:

    using System.Linq;

  • Toppers 31 posts 155 karma points
    Aug 06, 2016 @ 11:55
    Toppers
    0

    Doh!

    Thank you!!!

Please Sign in or register to post replies

Write your reply to:

Draft