nickornotto
nickornotto

Reputation: 2156

How to initialize umbraco MemberService in published model?

I am using Umbraco 7.6 and ModelsBuilder

I am trying to extend my CustomMember model and add some custom properties. I want to get IMember as a property. For this I need to initialize MemberService to use GetById() method.

How to initialize MemberService in partial CustomMember?

public partial class CustomMember 
{
    private readonly MemberService _memberService;

    public CustomMember(IPublishedContent content) : base(content) // this is not valid as the contructor already exists in the umbraco generated partial model
    {
        this._memberService = (MemberService)ApplicationContext.Current.Services.MemberService; // This is coming null
    }

    public IMember Member
    {
        get
        {
            return _memberService.GetById(this.Id);
        }
    }
}

Upvotes: 0

Views: 153

Answers (1)

Robert Foster
Robert Foster

Reputation: 2316

Use the ApplicationContext.Current.Services collection to access the MemberService singleton.

https://our.umbraco.com/Documentation/Reference/Management/Services/MemberService/Index

Specifically:

The MemberService is available through the ApplicationContext, but the if you are using a SurfaceController or the UmbracoUserControl then the MemberService is available through a local Services property.

You should also take a look at this article as well:

https://our.umbraco.com/documentation/Reference/Common-Pitfalls

Upvotes: 1

Related Questions