lmsasu
lmsasu

Reputation: 7583

Delete user programmatically in dotnetnuke

I'm using DNN 5.x;I want to delete an user from C# code. The code I tested is:

UserInfo objUserInfo = DotNetNuke.Entities.Users.UserController.GetUser(0, userid, false);
UserController.DeleteUser(ref objUserInfo, false, false);

Howver, the correponding record remains in tables: aspnet_users, aspnet_membership, users. What should I use to get full deletion?

Upvotes: 2

Views: 5071

Answers (4)

Reddy
Reddy

Reputation: 87

Use UserController.RemoveUser(objUserInfo) after the DeleteUser is called. So your code should look like

UserInfo objUserInfo = DotNetNuke.Entities.Users.UserController.GetUser(0, userid, false);
UserController.DeleteUser(ref objUserInfo, false, false);
UserController.RemoveUser(objUserInfo);

Upvotes: 5

creator
creator

Reputation: 171

I hacked DeleteUserPortal sproc to hard delete users in DNN5.02.03

ALTER PROCEDURE [dbo].[DeleteUserPortal] 
    @UserID     int,
    @PortalID   int
AS
    IF @PortalID IS NULL
        BEGIN
            UPDATE dbo.Users
                SET
                    IsDeleted = 1
                WHERE  UserId = @UserID
        END
    ELSE
        BEGIN
            UPDATE dbo.UserPortals
                SET
                    IsDeleted = 1
                WHERE  UserId = @UserID
                    AND PortalId = @PortalID
        END

-- Custom modification to delete user records from db instead of soft delete
-- Code above is redundant

DECLARE @userName nvarchar(100)
DECLARE @aspnetUserId uniqueidentifier

SELECT @userName = username FROM dbo.Users WHERE UserId = @UserID
SELECT @aspnetUserId = UserId FROM dbo.aspnet_Users WHERE UserName = @userName

DELETE FROM Users WHERE UserId = @UserID
DELETE FROM aspnet_Membership WHERE UserId = @aspnetUserId
DELETE FROM aspnet_Users WHERE UserId = @aspnetUserId

Upvotes: 0

Johannes
Johannes

Reputation: 2992

I use the following SQL statements to delete a user. You can easily integrate that into .net code...

DECLARE @NumTablesDeletedFrom int; 
DECLARE @userid INT;

EXECUTE [aspnet_Users_DeleteUser] @ApplicationName = 'DotNetNuke', @UserName = 'testUser', @TablesToDeleteFrom = 15, @NumTablesDeletedFrom = 1; 

SELECT @userid = UserID from users where Lower(username) = 'testUser'; 
EXECUTE [DeleteUser] @userid;

Upvotes: 1

bdukes
bdukes

Reputation: 155935

I don't believe they've implemented complete deletion in DNN 5.x yet (in DNN 4, they had full deletion, then moved to soft deletion in DNN 5). You may want to check the source code for that DeleteUser method in DNN 4.9.x or 4.8.x and see what it's doing (probably just a difference in what the stored procedure is doing). You should then be able to copy that stored procedure implementation and call it directly.

Upvotes: 2

Related Questions