Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new member programmatically in Umbraco

I'm trying to create a new member for my Umbraco site programmatically but I'm not confident that I am doing this correctly.

My code looks like this:

 MemberType demoMemberType = new MemberType(1040); //id of membertype ‘demo’
 Member newMember = Member.MakeNew(newEmployee.FirstName + " " + newEmployee.LastName, demoMemberType, new umbraco.BusinessLogic.User(0));

 newMember.Email = "[email protected]";
 newMember.Password = "password";
 newMember.LoginName = "Test";
 newMember.getProperty("firstName").Value = "test";

 newMember.Save();

But when I run my code, I can't see anything appearing in my Umbraco. Can someone please tell me what I've done wrong?

like image 742
Sean Avatar asked Jan 07 '15 13:01

Sean


2 Answers

If you are using umbraco 7 it is best to user the member service. Below is a simple approach you could employ to achieve this.

Please note ApplicationContext no longer exists for new umbraco versions think from 8+ So just ommit that. Everything else works fine.

public int RegisterMember(string memberName, string emailAddress, string memberPassword, string memberTypeAlias, string memberGroupName)
{
        int umbracoMemberId = -1;

        if (!MemberExists(emailAddress))
        {
            IMember newMember = ApplicationContext.Current.Services.MemberService.CreateMember(emailAddress, emailAddress, memberName, memberTypeAlias);

            try
            {
                ApplicationContext.Current.Services.MemberService.Save(newMember);
                ApplicationContext.Current.Services.MemberService.SavePassword(newMember, memberPassword);
                ApplicationContext.Current.Services.MemberService.AssignRole(newMember.Id, memberGroupName);
                umbracoMemberId = newMember.Id;
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to create new member " + ex.Message);
            }
        }

        return umbracoMemberId;
}


public bool MemberExists(string emailAddress)
{
        return (ApplicationContext.Current.Services.MemberService.GetByEmail(emailAddress) != null);
}

For a more authentic v8 approach you will want to discard ApplicationContext completely and instead use the dependency injection engine as below. This example assumes you are not able to inject the dependency in this case IMemeberService through the constructor so should cover most cases for most people at least from what I have come across so far especially for those developing applications with umbraco

public int RegisterMember(string memberName, string emailAddress, string memberPassword, string memberTypeAlias, string memberGroupName)
{
        int umbracoMemberId = -1;

        if (!MemberExists(emailAddress))
        {
            try
            {

                IMemberService _memberService = Current.Factory.GetInstance<IMemberService>();

                IMember newMember = _memberService.CreateMember(body.Email, body.Email, celebName, "Member");

                _memberService.Save(newMember);
                _memberService.SavePassword(newMember, body.Password);
                _memberService.AssignRole(newMember.Id, CMSContentConstants.RootNodes.gGourpAliasCelebrityMember);

                umbracoMemberId = newMember.Id;
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to create new member " + ex.Message);
            }
        }

        return umbracoMemberId;
}
like image 178
Francis Benyah Avatar answered Sep 23 '22 22:09

Francis Benyah


The subject is quite involved but the following is some code for your model and controller which should put you on the right track. Hopefully you know enough about MVC to get this going.

Your model could contain something like the following and be populated with the input from your view

    using System.ComponentModel.DataAnnotations;
    using System.Web;

    namespace MyProject.Models
    {
        public class MemberModel 
        {

            [Required]
            public string Name { get; set; }

            [Required]
            [EmailAddress]
            public string Email { get; set; }

            [Required]
            public string Password { get; set; }
        }
}

Your controller could be something like the following:

using System.Web.Mvc;
using MyProject.Models;
using Umbraco.Web.Mvc;

namespace MyProject.Controllers
{
    public class MemberController : SurfaceController
    {
        public ActionResult SignUp(MemberModel model)
        {
            if (!ModelState.IsValid) 
                return CurrentUmbracoPage();

            var memberService = Services.MemberService;
            if (memberService.GetByEmail(model.Email) != null)
            {
                ModelState.AddModelError("", "Member already exists");
                return CurrentUmbracoPage();
            }
            var member = memberService.CreateMemberWithIdentity(model.Email, model.Email, model.Name, "MyMemberType");

            memberService.Save(member);

            memberService.SavePassword(member,model.Password);

            Members.Login(model.Email, model.Password);

            return Redirect("/");
        }
    }
}
like image 45
wingyip Avatar answered Sep 23 '22 22:09

wingyip