Copied to clipboard

Flag this post as spam?

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


  • David 19 posts 109 karma points
    21 days ago
    David
    0

    How to check if a member in Umbraco Members exists?

    Hi all,

    I'm creating a controller to help uploading a spreadsheet ( which the user would convert into a .txt file) of members into Umbraco, however I'm not quite sure where to check if the user already exists, and if so how to just update the information instead of creating a new member.

    Here's my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using System.IO;
    
    namespace UmbracoMemberImport.Controllers
    {
    public class MemberSurfaceController :         
    Umbraco.Web.Mvc.SurfaceController
    {
        public ActionResult ImportMembers()
        {
            string _line = "";
            using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/Imports/members.txt")))
            {
                do
                {
                    // Read a line from the csv file
                    _line = sr.ReadLine();
    
                    // Check if the line is not empty
                    if (!String.IsNullOrEmpty(_line))
                    {
                        // Split the line using the ; delimiter
                        string[] _splits = _line.Split(',');
    
                        if (_splits.Length == 8)
                        {
                            // Create a new member instance
                            IMember member = ApplicationContext.Current.Services.MemberService.CreateMember(
                                _splits[3],     // Username
                                _splits[2],     // Email
                                _splits[6],     // Display name
                                "member"    // Member type
                            );
    
                            // Member is approved to login
                            member.IsApproved = true;
    
                            // Check is the branch content picker property exists and set the value
                            //if (member.HasProperty("branch"))
                            //    member.SetValue("branch", _splits[4]);
    
                            // Check is the sales person content picker property exists and set the value
                            //if (member.HasProperty("salesPerson") && !String.IsNullOrEmpty(_splits[5]))
                            //    member.SetValue("salesPerson", _splits[5]);
    
                            // Check is the code property exists and set the value
                            if (member.HasProperty("code"))
                                member.SetValue("code", _splits[0]);
    
                            // Check is the title property exists and set the value
                            if (member.HasProperty("title"))
                                member.SetValue("title", _splits[1]);
    
                            try
                            {
    
                                // Save the member
                                ApplicationContext.Current.Services.MemberService.Save(member);
    
                                // Save the password of the member
                                ApplicationContext.Current.Services.MemberService.SavePassword(member, _splits[7]);
    
                                // Assign the correct role
                                ApplicationContext.Current.Services.MemberService.AssignRole(member.Id, "Member");
    
                            }
                            catch { }
                        }
                    }
                } while (!sr.EndOfStream);
    
            }
    
            return null;
    
        }
    }
    }
    

    Any suggestions would be really appreciated.

    Thanks,

    David

  • Shaishav Karnani from digitallymedia.com 290 posts 1278 karma points
    21 days ago
    Shaishav Karnani from digitallymedia.com
    0

    Hi David,

    You can use MemberAuthorise attribute to ensure that a member must be logged in to access the resource.

    [MemberAuthorize] public class MemberSurfaceController : Umbraco.Web.Mvc.SurfaceController { ....

    Regards,

    Shaishav

  • David 19 posts 109 karma points
    15 days ago
    David
    0

    Thanks a lot Shaishav!

  • Alex Skrypnyk 5333 posts 19667 karma points MVP 3x admin c-trib
    20 days ago
    Alex Skrypnyk
    0

    Hi David

    There is a method in the Member service - Exists(). You can use it with member id or username like this:

    bool isExists = ApplicationContext.Current.Services.MemberService.Exists(id);
    

    Hope it helps, Thanks,

    Alex

  • David 19 posts 109 karma points
    15 days ago
    David
    0

    Thank you Alex!

Please Sign in or register to post replies

Write your reply to:

Draft