I wanted to create some testing data, so I began adding users by hand. One or two users is OK, but I wanted to add many so I started to play with the script that Mitchel Seller's had blogged in May, 2007 (here) . I wanted a stored procuedure I could use to add a user. I would then create a script calling this procedure as required. So here is what I developed:
Create procedure Tressleworks_AddUser
@ForPortalID int,
@NewUserName varchar(256),
@NewPassword varchar(128),
@NewFirstName varchar(50),
@NewLastName varchar(50),
@NewDisplayName varchar(100),
@NewEmail varchar(256),
-- optional below
@NewCity varchar(256) = '',
@NewRegion varchar(256) = '',
@NewCountry varchar(256) = ''
as
SET NOCOUNT ON
DECLARE @ApplicationName nvarchar(256)
DECLARE @NewPasswordSalt nvarchar(128)
DECLARE @NewPasswordQuestion nvarchar(256)
DECLARE @NewPasswordAnswer nvarchar(128)
DECLARE @NewPasswordFormat int
DECLARE @UniqueEmail int
DECLARE @DNNUserId int
DECLARE @PPID int
DECLARE @RC int
DECLARE @IsApproved bit
DECLARE @UserId uniqueidentifier
DECLARE @CurrentTimeUtc datetime
DECLARE @CreateDate datetime
SET @ApplicationName = 'DotNetNuke'
SET @NewPasswordSalt = 'P40ky5tExsx37nUIFnCWZQ=='
SET @NewPasswordQuestion = ''
SET @NewPasswordAnswer = ''
SET @NewPasswordFormat = 0 -- cleartext
SET @UniqueEmail = 0
SET @PPID = 0
SET @DNNUserId = -1
SET @IsApproved = 1
SET @CurrentTimeUtc = GETDATE()
SET @CreateDate = @CurrentTimeUtc
--- do the work
-- 1) Make the stored procedure call
EXEC dbo.aspnet_Membership_CreateUser @ApplicationName, @NewUserName, @NewPassword,
@NewPasswordSalt, @NewEmail, @NewPasswordquestion, @NewPasswordAnswer,
@IsApproved, @CurrentTimeUtc, @CreateDate, @UniqueEmail,
@NewPasswordFormat, @UserId
-- 2) Insert the record into the DotNetNuke users table
INSERT INTO users (Username, FirstName, LastName, IsSuperUser, Email, DisplayName, UpdatePassword)
VALUES(@NewUserName, @NewFirstName, @NewLastName, 0, @NewEmail, @NewDisplayname, 0)
--Get the new userid, from the DNN users table
SELECT @DNNUserId = userid FROM Users WHERE username = @NewUserName
--Print 'UserID = ' + Cast(@dnnuserid as varchar)
-- 3) Now, insert the record into the user portals table
INSERT INTO UserPortals (userId, PortalId, CreatedDate)
VALUES(@DNNUserId, @ForPortalID, GETDATE())
-- 4) Now Give the user permissions to the RECISTERED Users group
INSERT INTO UserRoles (userId, roleId)
SELECT @DNNUserId,
roleId
FROM Roles
WHERE RoleName = 'Registered Users'
-- 5) Add optional properties: city, region, country
if len(@NewCity) > 0
BEGIN
SET @PPID = (SELECT PropertyDefinitionID from dbo.ProfilePropertyDefinition where PortalID = @ForPortalID and PropertyName = 'City' )
EXEC @RC = dbo.UpdateUserProfileProperty null, @DNNUserId, @PPID, @NewCity, 1, @CreateDate
END
if len(@NewRegion) > 0
BEGIN
SET @PPID = (SELECT PropertyDefinitionID from dbo.ProfilePropertyDefinition where PortalID = @ForPortalID and PropertyName = 'Region' )
EXEC @RC = dbo.UpdateUserProfileProperty null, @DNNUserId, @PPID, @NewRegion, 1, @CreateDate
END
if len(@NewCountry) > 0
BEGIN
SET @PPID = (SELECT PropertyDefinitionID from dbo.ProfilePropertyDefinition where PortalID = @ForPortalID and PropertyName = 'Country' )
EXEC @RC = dbo.UpdateUserProfileProperty null, @DNNUserId, @PPID, @NewCountry, 1, @CreateDate
END
SELECT UserID, Username, FirstName, LastName from Users where UserID = @DNNUserId
Return @@rowcount
go
The procedure has several issues that you must be aware. I create the user password in clear text. Remember I was creating test data, so I am not concerned about the security issue of passwords in clear text. Refer to Mitchel's orginal blog to see how to use an encrypted password.
To execute the procedure, use something similar to
exec Tressleworks_AddUser 0, 'NewUser', 'newuser', 'New', 'User', 'New User', 'NewUser@Tressleworks.ca', 'mycity', 'Ontario', 'Canada'
The last 3 parameters are optional as the procedure checks to see if there is any data and only performs the update if data if passed into the procedure.
You can extend the procedure to add additional information, especially in the optional profile section. I only added City, Region, Country. The only requirement to adding new profile field, is the profile fileds MUST be defined prior to attempting to add the users. Also verify the profile name matches the name defined in the stored procedure.
The stored procedure returns the newly created user and a return code of 1 - actually the value is the rowcount of the records returned from the final select. If the an error occurs and the user is not created, a value of 0 is returned - no rows.
Hope this helps.
Paul