Creating a new member programmatically in Umbraco
Asked Answered
R

3

7

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?

Reflex answered 7/1, 2015 at 13:5 Comment(0)
L
12

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;
}
Latty answered 18/8, 2015 at 12:40 Comment(0)
S
3

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("/");
        }
    }
}
Suborbital answered 8/1, 2015 at 13:21 Comment(0)
I
0

Depending on the Umbraco version the code-sample provided is out of date.

Umbraco 4 changed the Umbraco membership model to use the ASP.NET Membership Provider model, meaning that all the abstract classes which are provided Out-Of-The-Box with ASP.NET are capable of accessing the Umbraco Member. There's plenty of good resources on MSDN (and other sites) for using ASP.NET Membership, a suggested starting point is here.

Articles:

http://our.umbraco.org/wiki/how-tos/membership-providers

http://msdn.microsoft.com/en-us/library/tw292whz.aspx

Intercolumniation answered 7/1, 2015 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.