DotNetNuke Powered!
          
John Mitchell's (mostly DotNetNuke) Blog - Importing Users to DotNetNuke
 Wednesday, November 08, 2006

I needed to transfer some users from an old membership database to my DotNetNuke database so I made a quick import routine and thought I would share in case anyone has to do something simular. 

What you need to do is get your users into an XML format like that shown below, and use the attached ImportUsers.aspx page from your DNN website.

<Users>
   <User>
      <Username>User1</Username>
      <Email>user1@yahoo.com</Email>
      <FirstName>User</FirstName>
      <LastName>One</LastName>
      <Password>password</Password>
      <Unit></Unit>
      <Street></Street>
      <City></City>
      <Region></Region>
      <PostalCode></PostalCode>
      <Country></Country>
      <Telephone></Telephone>
   </User>
   <User>
      <Username>User2</Username>
      <Email>user2@yahoo.com</Email>
      <FirstName>User</FirstName>
      <LastName>Two</LastName>
      <Password>password</Password>
      <Unit></Unit>
      <Street></Street>
      <City></City>
      <Region></Region>
      <PostalCode></PostalCode>
      <Country></Country>
      <Telephone></Telephone>
   </User>
</Users>


I quickly made an XML file like the one above from my User DB by running the Following SQL:

SELECT
'<Username>' + UL.Username + '</Username>',
'<Email>' + S.Email + '</Email>',
'<FirstName>' + S.FirstName + '</FirstName>',
'<LastName>' + S.LastName + '</LastName>',
'<Password>' + UL.Password + '</Password>'

FROM UserLogins UL Left Outer Join Subscribers S On UL.UserName = S.UserName
where S.Username is not null

Of course you will probably have different tables in your User DB, but you should get the idea.

As you can see from the SQL above, not all the nodes have to be in the XML for it to work.

 

DNN-ImportUsers1.zip (2.26 KB)
11/8/2006 2:30:52 PM (Central Standard Time, UTC-06:00)  #    Comments [26]
11/17/2006 1:32:03 PM (Central Standard Time, UTC-06:00)
THANK YOU!!!!

I have been trying to figure out how to import a 1000+ users without losing sleep or going crazy.

Mike
11/17/2006 2:47:20 PM (Central Standard Time, UTC-06:00)

Great!, glad I could help.
12/28/2006 11:54:41 AM (Central Standard Time, UTC-06:00)
Awesome!
Thanks
Will P
1/9/2007 5:02:30 AM (Central Standard Time, UTC-06:00)
hi john ,

how could i attche import user in to dnn website could you please explain me thantk
2/7/2007 6:40:22 PM (Central Standard Time, UTC-06:00)
Man, thanks for the info. I was struggling with this for some time.

I am curious though... I am trying to understand how profile definition such as address, city, ... are added to the profile.

I see that in your code behind, the meat of it is essentially the call to 'UserController.CreateUser(oUserInfo)'

When I look into the source at file DotNetNuke.Library/components/Users/User/UserController.vb, in function CreateUser, UserCreateStatus.AddUser and memberProvider.CreateUser(objUser) are called.

Now, the way I understand it, these are essentially the SPs called AddUserand aspnet_Membership_CreateUser

AddUser is fairly simple. It inserts some basic attributes into table Users, but no address and so forth.

aspnet_Membership_CreateUser is a bit more compplicated but I see that it takes in some basic profile params but not address, city, ... It executes asp_netApplications_CreateApplication and then aspnet_Users_CreateUser which inserts some values into table aspnet_Users and finally AddUserRole is called.

Nowhere do I see how the default or any custom definitions are added.

I did use your ImportUsers page and it worked for the code below, but man, I don't see how it is doing it. Any chance you could take the time to explain it please?
Frank
2/7/2007 9:01:09 PM (Central Standard Time, UTC-06:00)
Hi Frank,

Thanks, I'm glad you found it useful.

The beauty of a framework is that it can do alot of stuff behind the scenes without us ever having to know exactly how it does it.

That being said, you may be trying to figure out what is going on to get a better understanding for some other task, so that is understandable.

Trouble is, I'm not sure exactly what I should explain in more detail. It sounds like you are trying to figure out where use profiles are stored. That actually just changed in recent versions of DNN. They are now stored in the UserProfile table with the definition of those properties being in the ProfilePropertyDefinitions table.

If you have more specific questions, feel free to ask over at my DotNetNuke Support Site http://dotnetnuke-forums.com
2/13/2007 7:50:32 AM (Central Standard Time, UTC-06:00)
Hi John,

Thanks for the tip - I am using another BulkUploader (BulkReg from SC) but it does the same as this effectively. What I am battling with is getting the XML export correct - how do you extract yours?

I am using a system SP to extract the data with "FOR XML, ELEMENTS" which works well, but it took me ages to tweak the table column names and manage the data - is there an easier way to convert SQL to XML files?

Thanks
Rodney

2/13/2007 7:58:00 AM (Central Standard Time, UTC-06:00)
Hi Rodney,

I use UltraEdit to do some manipulation of large xml files.
It has a macro recorder that allows you to perform an operation and then repeat it many times.

However, if I were going to be doing it more than once, I would probably just program the export into the same routine that does the import. In other words I would start the routine by opening up the DB to get the users, then loop through them with code to apply the the records one at a time and create the new users, therby skipping the XML step.
2/22/2007 12:04:32 PM (Central Standard Time, UTC-06:00)
Does this script work for a users table that is using Active Directory?

Does this script work for the export/import of users for DNN 3 to DNN 4?

Just curious.

Thanks!
Heather G
2/26/2007 3:47:54 PM (Central Standard Time, UTC-06:00)
Thanks for such a time saver John! This worked great!
2/28/2007 8:01:35 PM (Central Standard Time, UTC-06:00)
Hi John. Thank you for this code, it is sooooooo helpful.

I wonder if you could guide me a bit further. I am currently exracting about 7000 user accounts out of an Access97 db and importing them into DNN 4.4.1. Problem is, the old db was really poorly designed. Account #'s can be duplicated, and so can names or emails.

I intended on using the names (this is one field for both the first and last name)from the old db as the User Names in the DNN db. When I ran your script however, it bounced a good number of users because the User Name already existed.

I looked at the script hoping I could figure out a way to use a try\catch or something, so that is a user creation fails, I would simply append a number to the end of the User Name and try again. So if TestName already existed in the db, the script would append a 1 and try the creation again with the TestName1

To tell the truth, I am still unsure of how the DNN code works as a whole but I took a look at function cmdImportUsers_Click and I know what I want to do needs to occur in the following else block:

Else
responseBuilder.Append(String.Format("...User '{0}' creation failed: {1}", oUserInfo.Username, [Enum].GetName(GetType(UserCreateStatus), createStatus)))

[BTW, what does '[Enum].GetName' do?]


Could I do something like this?:

Else
Dim i = 1
Do While createStatus = UserCreateStatus.UsernameAlreadyExists
oUserInfo.Username += oUserInfo.Username + i.ToString()
i++
Loop

That should work, right?


3/2/2007 11:05:25 AM (Central Standard Time, UTC-06:00)
I built on the code in the previous post and I have what I need. Thanks anyways. Peace.
3/27/2007 12:50:15 AM (Central Daylight Time, UTC-05:00)
Pretty nice code thanks. Have you written anything like that for the new Events module? The export funtionality was not working in newer versions and is absent in the latest beta's. We have data from an old custom ASP Calendar application that is stored in an Access database. I would like to use your SQL method above to create the XML from the Access file for the import.
Any help would be appreciated,
Thanks,
Duane
3/29/2007 4:19:11 PM (Central Daylight Time, UTC-05:00)
If your existing user table is in MS SQL 2005 (or you can get it in SQL 2005), you can use the enhancements to the FOR XML clause to make the XML output fit the input format that John has defined.
Here's an example that worked for me:

SELECT
UserName, Email, FirstName, LastName, Password,
PostalCode, TimeZone
FROM
OldUserTable
FOR XML RAW ('User'), ELEMENTS, ROOT ('Users')

There may be a way to do it in SQL 2000 too, but probably not nearly this simple.
Jordan
4/9/2007 12:50:05 PM (Central Daylight Time, UTC-05:00)
This is great thanks soooo much for this, just imported 5000 users and works great.

had to merge the ascx and .vb into a dnn templated module but works great.

if anyone needs help getting the xml or creating a dnn 4.x module to house the page to import the users mail me and ill do what i can.

7/16/2007 2:16:26 PM (Central Daylight Time, UTC-05:00)
Hi John!
I have one question I was hoping you could help with. Your improt works great! The one thing I've noticed, is your sql (which i used and taylored to my tables)
does not populate the Display Name field in DNN.
I've tried figuring it out myself...useing DisplayName and also just Name.
My import is successful, however the field is still blank for every user.
The biggest part is this is what is displayed to the user, and is also a Required Field when setting up a new user, so not sure why the import works....do you have any suggestions on how to populate the Display Name field for each user?
Thanks so much,
Jen
7/16/2007 2:32:34 PM (Central Daylight Time, UTC-05:00)
Hi Jen,
I think this import was created before "Display Name" was a property of the user info object. Even if it wasn't that's my story and I'm sticking to it ;)

If you edit the code-behind file you can add a line like this:

oUserInfo.DisplayName = GetXmlItemValue(xmlItem, "DisplayName")

Or if you don't want to go through the trouble of creating the display name in your XML and just want to use First and Last Name you could do this:

oUserInfo.DisplayName = oUserInfo.FirstName & " " & oUserInfo.LastName
7/30/2007 1:45:16 PM (Central Daylight Time, UTC-05:00)
Does anyone know how to add users to roles once they have been added or while they ar been added? I need to add users to roles
Not all users will have the same roles, i need to add many users to the role Students, many users to the role parents, etc
Ill appreciate any help

Thank you
8/3/2007 4:37:06 AM (Central Daylight Time, UTC-05:00)
Just a quick response for Alexei Rodriguez about assigning roles at the same time as creating users.

You can add a user to any role (or number of roles) by using the AddUserRole method of DotNetNuke.Security.Roles.RoleController. there is some sample code below;
(please note vb is not my dev language, this example s pretty haxy.)


======================
' code from
' forum thread...
' ......
' Create Account
Dim createStatus As UserCreateStatus = UserController.CreateUser(oUserInfo)

' Account Role
If createStatus = UserCreateStatus.Success Then
' Role for the user
Dim objRolecontrol As New DotNetNuke.Security.Roles.RoleController

' RoleA
If GetXmlItemValue(xmlItem, "RoleA").ToString() = 1 Then
objRolecontrol.AddUserRole(PortalId, oUserInfo.UserID, objRolecontrol.GetRoleByName(PortalId, "RoleB").RoleID, Nothing)

' RoleB
ElseIf (GetXmlItemValue(xmlItem, "RoleB").ToString() = 1) Then
objRolecontrol.AddUserRole(PortalId, oUserInfo.UserID, objRolecontrol.GetRoleByName(PortalId, "RoleB").RoleID, Nothing)

End If

End If ' end add roles
==========================



Also, if like me you had to create users for authorisation at a later date then you will also want
=====================
' %%% MJR 03/070/7 : This should be a case or vb equivalent i suppose...
' Account Status (active or not, i.e. can login or not)
If GetXmlItemValue(xmlItem, "SomeStatusFlag").ToString() = 0 Then
oUserInfo.Membership.Approved = False
ElseIf GetXmlItemValue(xmlItem, "SomeStatusFlag").ToString() = 1 Then
oUserInfo.Membership.Approved = True
ElseIf GetXmlItemValue(xmlItem, "SomeStatusFlag").ToString() = 2 Then
oUserInfo.Membership.Approved = False
End If
===================

Hope this helps

Matt

8/23/2007 11:12:05 AM (Central Daylight Time, UTC-05:00)
Thanks so much, John. This bulk import was exactly what I was looking for!
9/1/2007 3:44:18 PM (Central Daylight Time, UTC-05:00)
Thanks John,

This was exactly the start I needed. I had additional information about each user that I also wanted to import. So, I added the following code snippet so that I could import this information. Your users may find this as usefull as I have.

In ImportUsers.aspx, right before (without spaces) < /User > I added (without spaces):

< Profile >
< Cell >< /Cell >
< Fax >< /Fax >
< Website >< /Website >
< IM >< /IM >
< /Profile >

Any Portal specific Profile attributes can also be added here. I had a few more, like Birthday, Hobbies, etc.

In ImportUsers.aspx.vb, right before:

Dim createStatus As UserCreateStatus = UserController.CreateUser(oUserInfo)

I added:

' vvvvvvvvvv Profile Extention added by wcleveland vvvvvvvvvv
For Each xmlProfile As XmlElement In xmlItem("Profile")
oUserInfo.Profile.SetProfileProperty(xmlProfile.Name, xmlProfile.InnerText)
Next
' ^^^^^^^^^^ End of Profile Extention added by wcleveland ^^^^^^^^^^

Thanks again for this little tool.
9/30/2007 3:08:31 PM (Central Daylight Time, UTC-05:00)
This looks great... but the page is throwing an error when I point to http://mysite.com/ImportUsers.aspx :

Compiler Error Message: BC30002: Type 'UserInfo' is not defined.

Source Error:



Line 45: For Each xmlItem As XmlElement In xmlDoc.SelectNodes("/Users/User")
Line 46:
Line 47: Dim oUserInfo As New UserInfo
Line 48: oUserInfo.PortalID = 0
Line 49: oUserInfo.Membership.Username = GetXmlItemValue(xmlItem, "Username")


any ideas on this?

10/11/2007 11:04:19 AM (Central Daylight Time, UTC-05:00)
I have an excel speadsheet that has UserID's, Passwords etc, from an old DNN (2.x?) site. I wrote an SQL Server SSIS package to import the speadsheet into SQL Server, then wrote an Select statement similar to the ones above and was delighted with the results. I set up my dnn site and it works like a charm - v 4.6.2 and copied the aspx and the vb file sourced at the top of this page. I went to the page and pasted my perfectly formed xml file over top of the stuff that was there. Clicked the ImportUsers Link and received the error message "User '' creation failed: InvalidUserName". I know enough VB to make me dangerous. What can I do? I tried with just one User. Checked that Everything was good... do I need to make the aspx. and vb file into a DNN module? If so how do I do this.

Cheers,
Scott
4/26/2008 2:11:48 PM (Central Daylight Time, UTC-05:00)
Generic soma no prescription
4/26/2008 6:19:05 PM (Central Daylight Time, UTC-05:00)
Cheapest Fioricet Uses
5/23/2008 2:08:35 PM (Central Daylight Time, UTC-05:00)
I realize this is a rather old post, but it's exactly what I'm trying to do right now. I have an Excel spreadsheet that I imported into an SQL 2000 database table. Now I have to import the users from the table into a DNN site. Problem is, I can't say I fully understand using SQL to create the XML file needed for this import and am wondering if I could get some more help with that? I can be reached Monday-Friday from 8-5 CST at my email address brady@notrs.com
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):