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
    0

    Is it possible to create a member group programmatically

    Hi.

    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

    [HttpPost]
        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();
            }
    
            //FIND MEDIAFOLDER CONTAINER
            var strMediaContainer = CurrentPage.GetPropertyValue("athleteMediaContainer").ToString();
            int intMediaContainer = Int32.Parse(strMediaContainer);
            IMedia MediaFolder = Services.MediaService.CreateMedia(model.Name, intMediaContainer, "Folder");
            Services.MediaService.Save(MediaFolder);
    
            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);
    
            memberService.Save(member);
    
            //FIND ATHLETE CONTAINER PAGE
            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());
            Services.ContentService.SaveAndPublishWithStatus(newAthletePage);
    
            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
    
            memberService.Save(member);
    
            return RedirectToCurrentUmbracoPage();
            //return Redirect("/");
    
        }
    

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

  • Shaishav Karnani from digitallymedia.com 330 posts 1536 karma points
    Aug 16, 2019 @ 06:41
    Shaishav Karnani from digitallymedia.com
    100

    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} };

    mg.Save(memberGroup);

    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:

Draft