jan-h
3/11/2014 - 11:27 PM

Create an SDL Tridion user trhough CoreService. Assumes having the Tridion Core Service Client DLL config as app.config

Create an SDL Tridion user trhough CoreService. Assumes having the Tridion Core Service Client DLL config as app.config

using System;
using System.ServiceModel;
using Tridion.ContentManager.CoreService.Client;

namespace CoreServiceSandbox
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var client = new CoreServiceClient("basicHttp_2013");
            try
            {
                Console.WriteLine("The current user is {0}\n", client.GetCurrentUser());

                UserData user = client.GetDefaultData(ItemType.User, null, new ReadOptions()) as UserData;
                Console.Write("Username (domain\\username): ");
                user.Title = Console.ReadLine();
                
                Console.Write("Full name: ");
                user.Description = Console.ReadLine();

                Console.Write("Is Tridion administrator: ");
                user.Privileges= ("yes".Equals(Console.ReadLine()) ? 1 : 0);

                Console.WriteLine("\nCreating user...");
                user = client.Create(user, new ReadOptions()) as UserData;
                Console.WriteLine("Created user {0}\n  Username {1}\n  Description {2}\n  Privleges {3}", user.Id, user.Title, user.Description, user.Privileges);
            }
            catch (FaultException<CoreServiceFault> e)
            {
                Console.WriteLine("Error; Core Service said: {0}; {1} ", e.Detail.ErrorCode, string.Join(", ", e.Detail.Messages));
            }
            catch (CommunicationException e)
            {
                Console.WriteLine("Error; Could not connect to the Core Service\n {0}\n {1}", e.Message, e.InnerException);
                client.Abort(); //The channel is aborted and the resources released.
            }
            catch (TimeoutException e)
            {
                Console.WriteLine("Error; Could not connect to the Core Service\n {0}\n {1}", e.Message, e.InnerException);
                client.Abort(); //The channel is aborted and the resources released.
            }
            catch (Exception e)
            {
                Console.WriteLine("Error; Could not connect to the Core Service\n {0}\n {1}", e.Message, e.InnerException);
            }

            Console.WriteLine("\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}