Skip to main content

Export Sitecore users to a .csv file

Export Sitecore Users to a File Here is a piece of code which I have written to export Sitecore users to a .csv file. This code reads the list of Sitecore users using UserManager's GetUsers method. See if it helps you.
  

	using Sitecore.Shell.Framework.Commands;
	using System;
	using Sitecore.Web.UI.Sheer;
	using System.IO;
	using Sitecore.SecurityModel;
	using System.Text;
	using Sitecore.Diagnostics;
	namespace SitecoreCustom.Web.Extensions.Commands
	{
	public class ExportSitecoreUsers : Command
	{
		string filepath = string.Empty;
		public override void Execute(CommandContext context)
		{
			try
			{
				CreateFile();
				if (!string.IsNullOrWhiteSpace(filepath))
					SheerResponse.Download(filepath);
			}
			catch (Exception ex)
			{
				Log.Error(ex.Message, ex);
				SheerResponse.Alert("Error Occurred: Please try again");
			}
		}
		/// 
		/// Create csv file with list of users.
		///          
		private void CreateFile()
		{
			string fileExtension = ".csv";
			string path = Sitecore.Configuration.Settings.DataFolder + "Export User";
			if (!Directory.Exists(path))
				Directory.CreateDirectory(path);
			string fileName = "Sitecore_User";
			fileName = fileName + "-" + DateTime.Now.ToString("MM-dd-yyyy_HH-mm-ss");
			filepath = path + @"\" + fileName + fileExtension;

			using (new SecurityDisabler())
			{
				var users = Sitecore.Security.Accounts.UserManager.GetUsers();
				StringBuilder records = new StringBuilder();
				records.Append(string.Format("{0},{1},{2},{3},{4},{5},{6},{7}",
														 "User Name",
														 "Domain",
														 "Fully Qualified Name",
														 "Full Name",
														 "Email",
														 "Comment",
														 "Language",
														 "Locked"));

				foreach (var user in users)
				{
					try
					{
						if (user != null)
						{
							records.Append(Environment.NewLine);
							records.Append(string.Format("{0},{1},{2},{3},{4},{5},{6},{7}",
													   user.LocalName,
													   user.Domain.Name,
													   user.DisplayName,
													   user.Profile.FullName,
													   user.Profile.Email,
													   user.Profile.Comment,
													   user.Profile.ClientLanguage,
													   user.Profile.State));
						}
					}
					catch (Exception ex)
					{
					    Log.Error(ex.Message, ex);
					}
				}

				using (var writer = new StreamWriter(filepath, false))
				{
					writer.WriteLine(records.ToString());
					writer.Close();
				}
			}
		}
	}
}
  
These custom code files can also be downloaded from here

Comments

Popular posts from this blog

GraphQL for Sitecore: A beginner walkthrough

If you are a beginner in GraphQL and you are looking step-by-step by process to setup GraphQL API on Sitecore, please proceed further and setup up GraphQL API for Sitecore Before we jump start Sitecore GraphQL API, let us start some basic introduction of GraphQL. What is GraphQL GraphQL is a query language GraphQL provides a complete and understandable description of the data in your API It gives the power to ask exactly what you need and nothing more Designed to support the needs of the front-end Strongly typed, self-documenting Bandwidth efficient Easy real-time data Sitecore GraphQL API The Sitecore GraphQL API is a generic GraphQL service platform on top of Sitecore. It hosts your data and presents it through GraphQL queries. The API supports real-time data using GraphQL subscriptions Get GraphQL by installing the JSS server components package Sitecore GraphQL does not have any GraphQL endpoints defined by default Define at least one endpoint to use the GraphQL...

Custom Fields in Sitecore (Custom DropLink, DropTree, MultiList, TreeList and Required Field Validator)

Custom Fields in Sitecore Just thought to share few of the custom Sitecore fields that I have created for one of the sample project. This may help the community memebers to easily incorporate them into their project if they need any of them. Here are few. 1. Custom DropLink using Sitecore.Data.Items; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Web; namespace SitecoreCustom.Web.Extensions.CustomFields { public class CustomDropLink : Sitecore.Shell.Applications.ContentEditor.LookupEx { protected override void DoRender(System.Web.UI.HtmlTextWriter output) { this.ExcludeItems(); base.DoRender(output); } private void ExcludeItems() { Item item = Sitecore.Context.ContentDatabase.GetItem(base.ItemID); var LinkedField= Sitecore.StringUtil.ExtractParamete...

Sitecore 9.3 Installation | Common Issues and Resolutions

Recently, I have installed and setup Sitecore 9.3 on another machine. I faced similar/same kind of issues which I faced earlier. Thought, it would be helpful to the Sitecore Community if I capture and document them along with resolution steps I which took and be successful in settting up. Here are few of them I could document them. 1. Failed to start service 'Sitecore Marketing Automation Engine - sitecore93xconnect.dev.local-MarketingAutomationService (sitecore93xconnect.dev.local-MarketingAutomationService)'. Solution : This issue occurs because of non-self-signed certificates, hence please follow the below steps to resolve your issue. Execute below PowerShell script to find out if there are any non-self-signed certificates: 1. Open the PowerShell Console (Admin mode recommended) 2. Execute below command Get-Childitem cert:\LocalMachine\root -Recurse | Where-Object {$_.Issuer -ne $_.Subject} 3. If it shows any results listing any non-self-signed certificates...