Bulk Create Sitecore Users

Created: 23 Sep 2015, last update: 30 Jan 2022

c# Script for create Sitecore users in bulk quantity

I need a lot of users in my Sitecore instance for performance testing purpose an wrote a small script to do that job. Programmatically adding users to Sitecore is not difficult. You can get more code examples here: programmatically add / edit users or sitecore users and c#

Things to know for a Sitecore account.

1) You need a Domain, But it is also possible to have a user without an domain, create a user and throw the corresponding domain away, is a way to do this via the Sitecore interface.

The script below use the extranet domain, exist default in Sitecore. Everything about Domains see Create and edit a security domain

2) A Sitecore user has a optional Profile, this script do not add a Named Profile same as the default\Anonymous and the extranet\Anonymous user they do not have an additional profile.

3) Sitecore use Microsoft Membership providers. So basically well documented.

4) When working with large amout of users you can not use the Sitecore.Security.Accounts.UserManager.Provider.GetUsers() Methode, To Slow. When you have more domains, you can use for example DomainManager.GetDomain("extranet").GetUsers(); on domains with a small amount of users. (note: if the domain not exist you get a null reference exception you need to check and after that do the GetUsers) Else use paging. .Take(xx) but if you want to search and filter on a large set of users, it take lots of time, so for that do not use the Sitecore.Security API. Off course you can fast select a single user by name. Sitecore.Security.Accounts.User.FromName("extranet\\Anonymous",false); Creating a index is also possible, see http://www.alen.me.uk/2013/11/index-users-with-lucene-in-sitecore.html It is using the same GetUsers() Methode but it is okay if indexing take some time.

Similar to the Sitecore FillDB.aspx find here below the FillDbWithExtranetUser.aspx

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Page Language="C#" Debug="true" %>
<!--
Create Sitecore Dummy Users
Place this script below the /sitecore/admin folder 
And run with url:  /sitecore/admin/FillDbWithExtranetUser.aspx
-->
<HTML>
   <script runat="server" language="C#">
       string log = string.Empty;
    public void Page_Load(object sender, EventArgs e)
    {
        var start = 0;
        var end = 150000;
        var action = Request["action"];
        Sitecore.Diagnostics.Log.Info("Mirabeau FillDbWithExtranetUser call action: "+action, this);
        if (action == "start" || action == "delete")
        {
            for (var count=start; count <end; count++)
            {
                var userName = string.Format("extranet\\user{0}", count);
                try {
                    if (action == "start") {
                        Membership.CreateUser(userName, "b", "stockpick@example.com");
                    } else {
                        Membership.DeleteUser(userName);
                    }
                } catch (MembershipCreateUserException ex) 
                {
                    log += string.Format("user: {0} : {1}<br>\n",userName,ex.Message);
                }
            }
            if (string.IsNullOrEmpty(log)) { log = "no errors"; }
        }
    }
   </script>
   <body>
   <h1>Create Sitecore Dummy Users</h1>
   <p>Creating or deleting 150K Sitecore extranet user take some time, Choose:</p>
       <p><a href="?action=start">Start create Extranet users</a></p>
       <p><a href="?action=delete">Delete the FillDbWithExtranetUser created users</a></p>
    <p>
              Error log:<br><%=log %>
          </p>
   </body>
</HTML>

After creating 150K users, there is also a delete option :), It is for dev / test usage.