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;
}
}
}
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:
Any suggestions would be really appreciated.
Thanks,
David
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
Thanks a lot Shaishav!
Hi David
There is a method in the Member service - Exists(). You can use it with member id or username like this:
Hope it helps, Thanks,
Alex
Thank you Alex!
is working on a reply...