Copied to clipboard

Flag this post as spam?

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


  • Simon Dingley 1470 posts 3427 karma points c-trib
    Aug 07, 2013 @ 10:58
    Simon Dingley
    0

    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

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Aug 08, 2013 @ 07:49
    Richard Soeteman
    100

    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?

    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);
                }
            }
        }
      }
    }
    

    Cheers,

    Richard

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Aug 08, 2013 @ 09:07
    Simon Dingley
    0

    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

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Aug 08, 2013 @ 10:23
    Richard Soeteman
    0

    Cool yes please post the results.

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Aug 08, 2013 @ 13:49
    Simon Dingley
    0

    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!

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Aug 08, 2013 @ 14:03
    Richard Soeteman
    0

    Thanks Simon, I'm sure this will help others! #h5yr!

  • AM 1 post 21 karma points
    Sep 19, 2013 @ 04:08
    AM
    0

    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. 

  • Richard Soeteman 4035 posts 12842 karma points MVP
    Sep 19, 2013 @ 05:25
    Richard Soeteman
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft