Is it possible to have an Import Definition for members that allows the user to select the role to assign at the point they import the members in the Content dashboard control? I don't really want to give the client access to the developer area but using the Content Dashboard they don't have the option to assign roles which are likely going to be different for each import they do.
It's not possible out of the box. But what you could do is add an extra property to the datasource that contains the role and add an event that automatically assigns the roles based on the datasource value. Below some example code. It assumes the member roles are stored in the roles column.
The code requires references to the default Umbraco dll's and CMSImport.Extensions
Will this help you?
using CMSImport.Extensions.Import;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.member;
namespace AssignMemberGroups
{
public class AutoAssignMemberGroups : ApplicationBase
{
public AutoAssignMemberGroups()
{
MemberImport.RecordImporting += MemberImport_RecordImporting;
}
void MemberImport_RecordImporting(object sender, RecordImportingEventArgs e)
{
var member = sender as Member;
if (member != null)
{
//Assuming Roles is the column in the datasource
string rolesCsv = string.Format("{0}", e.Items["Role"]);
foreach(string role in rolesCsv.Split(','))
{
//You might want to cache this
var memberGroup = MemberGroup.GetByName(role);
member.AddGroup(memberGroup.Id);
}
}
}
}
}
That looks great - thanks Richard I will give it a try today and report back. I might even take that a step further and have it create new roles if they don't already exist.
It works perfectly! I re-factored your example a little because some of the methods are now obsolete in the version I am working with (v4.8.1) and I also added the feature to create a role if it doesn't already exist.
namespace MyClient.BusinessLogic.CmsImport
{
using System.Web.Security;
using umbraco.businesslogic;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.member;
using CMSImport = CMSImport.Extensions.Import;
public class MemberImport : ApplicationStartupHandler
{
/// <summary>
/// Initializes a new instance of the <see cref="MemberImport"/> class.
/// </summary>
public MemberImport()
{
CMSImport.MemberImport.RecordImporting += MemberImport_RecordImporting;
}
/// <summary>
/// Handles the RecordImporting event of the MemberImport class.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="CMSImport.RecordImportingEventArgs"/> instance containing the event data.</param>
private void MemberImport_RecordImporting(object sender, CMSImport.RecordImportingEventArgs e)
{
var member = sender as Member;
if (member != null)
{
// Get the specificed roles (MemberGroups) for the current record
string roles = string.Format("{0}", e.Items["Roles"]);
// Loop over each role
foreach (string role in roles.Split(','))
{
// Get the role(MemberGroup) if it exists, otherwise creates it
var memberGroup = MemberGroup.GetByName(role.Trim()) ?? MemberGroup.MakeNew(role.Trim(), User.GetCurrent());
// Add the member to the role (MemberGroup)
Roles.AddUserToRole(member.LoginName, memberGroup.Text);
}
}
}
}
}
CMSImport is the gold standard for Umbraco data imports!
This is exactly what I was looking for. But... How would one implement this? Can I simply create this as a class and add the .cs file to the App_code folder. Will it then automatically be complied when umbraco is loaded for the first time ? Sorry, I'm new to umbraco so I'm a bit stumped on how/where to place this into Umbraco.
Yes you can add this as a .cs file in the app_code folder. Umbraco will pick this class up because it derives from a startup handler. It's also possible to create a dll of course like any .net project. It's what you prefer.
Member Import with Role Selection at Import Time
Hi Richard,
Is it possible to have an Import Definition for members that allows the user to select the role to assign at the point they import the members in the Content dashboard control? I don't really want to give the client access to the developer area but using the Content Dashboard they don't have the option to assign roles which are likely going to be different for each import they do.
Thanks, Simon
Hi Simon,
It's not possible out of the box. But what you could do is add an extra property to the datasource that contains the role and add an event that automatically assigns the roles based on the datasource value. Below some example code. It assumes the member roles are stored in the roles column.
The code requires references to the default Umbraco dll's and CMSImport.Extensions
Will this help you?
Cheers,
Richard
That looks great - thanks Richard I will give it a try today and report back. I might even take that a step further and have it create new roles if they don't already exist.
Cheers, Simon
Cool yes please post the results.
It works perfectly! I re-factored your example a little because some of the methods are now obsolete in the version I am working with (v4.8.1) and I also added the feature to create a role if it doesn't already exist.
CMSImport is the gold standard for Umbraco data imports!
Thanks Simon, I'm sure this will help others! #h5yr!
This is exactly what I was looking for. But... How would one implement this? Can I simply create this as a class and add the .cs file to the App_code folder. Will it then automatically be complied when umbraco is loaded for the first time ? Sorry, I'm new to umbraco so I'm a bit stumped on how/where to place this into Umbraco.
Hi,
Yes you can add this as a .cs file in the app_code folder. Umbraco will pick this class up because it derives from a startup handler. It's also possible to create a dll of course like any .net project. It's what you prefer.
Hope this helps,
Richard
is working on a reply...