Copied to clipboard

Flag this post as spam?

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

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Aug 15, 2019 @ 10:17
    Jimmy Dan Mortensen

    Is it possible to create a member group programmatically


    I'm creating a memberservice, where I want to do the following:

    1. Create a member
    2. Create a page node for this member
    3. Create a member group
    4. Protect the page node with the group

    I have created a controller and made this public ActionResult

        public ActionResult HandleFormSubmit(FederationCreateAthleteFormViewModel model)
            if (!ModelState.IsValid)
                return CurrentUmbracoPage();
            //Create member
            var memberService = Services.MemberService;
            if (memberService.GetByEmail(model.Email) != null)
                ModelState.AddModelError("", "Atlet findes allerede med denne mail");
                return CurrentUmbracoPage();
            var strMediaContainer = CurrentPage.GetPropertyValue("athleteMediaContainer").ToString();
            int intMediaContainer = Int32.Parse(strMediaContainer);
            IMedia MediaFolder = Services.MediaService.CreateMedia(model.Name, intMediaContainer, "Folder");
            var member = memberService.CreateMemberWithIdentity(model.Email, model.Email, model.Name, "dBwFAthlete");
            member.SetValue("athleteMediaNode", MediaFolder.GetUdi().ToString());
            memberService.SavePassword(member, model.Password);
            Members.Login(model.Email, model.Password);
            var strAthleteContainer = CurrentPage.GetPropertyValue("athletePageContainer").ToString();
            int intAthleteContainer = Int32.Parse(strAthleteContainer);
            var newAthletePage = Services.ContentService.CreateContent(model.Name, intAthleteContainer, "federationAthletePage");
            newAthletePage.SetValue("athleteMediaFolder", MediaFolder.GetUdi().ToString());
            member.SetValue("dBwFAthletePage", newAthletePage.GetUdi().ToString());
            //Create Member group with athlete name.
            //var mg = Services.MemberGroupService;
            //Protect Athletepage with created group
            //add coaches to membergroup
            return RedirectToCurrentUmbracoPage();
            //return Redirect("/");

    But I am stuck where I need to create the member group. Can anybody help me?

  • Shaishav Karnani from 330 posts 1564 karma points
    Aug 16, 2019 @ 06:41
    Shaishav Karnani from

    This code snippet should help you. I have provided snippet for Create Member group & Protect Athletepage with created group.

    //Create Member group with athlete name. var mg = Services.MemberGroupService; var memberGroup = new MemberGroup { Name = {athlete name} };


    var Id = memberGroup.Id;

    //Protect Athletepage with created group var publicAccessService = ApplicationContext.Current.Services.PublicAccessService;

    // Get IContent that should be protectet. var content = contentService.GetById(1000);

    // Get the current role for the content var entry = publicAccessService.GetEntryForContent(content);

    // Update entry entry.LoginNodeId = 1500; entry.NoAccessNodeId = 1660; entry.AddRule("MemberRole", {athlete name}); publicAccessService.Save(entry);

Please Sign in or register to post replies

Write your reply to: