Generating Test Profiles in SharePoint


A common need when testing SharePoint solutions is having test accounts setup a certain way so they can do something. Whether this is a test account for a country or language, or whatever. Normally it is very painful to set these accounts up as typically they end up being setup by hand in the user profile service application. So I’ve gone ahead and built a tool (which the code for is below). Which just reads all the attributes for a profile from a CSV and updates the users profiles.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using System.Web;

namespace WithinSharePointUpdateUserProfile
{
    class Program
    {
        static Char[] Splitter = new Char[] { ',' };
        static UserProfileManager uMan = null;
        static Dictionary<UserProfile,Dictionary<string, string>> UsersToUpdate = new Dictionary<UserProfile,Dictionary<string, string>>();

        static void Main(string[] args)
        {
            if (args.Count() == 1)
            {
                SPSite site = new SPSite(args[0]);
                DisplayProfileAttributes(SPServiceContext.GetContext(site));
            }
            else if (args.Count() == 2)
            {
                try
                {
                    SPSite site = new SPSite(args[0]);
                    uMan = UserManager(SPServiceContext.GetContext(site));
                    ReadFile(args[1]);
                    foreach (UserProfile K in UsersToUpdate.Keys)
                    {
                        UpdateUser(K, UsersToUpdate[K]);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Help();
                }
            }
            else
            {
                Help();
            }
        }

        static void DisplayProfileAttributes(SPServiceContext Context)
        {
            UserProfileConfigManager config = new UserProfileConfigManager(Context);
            ProfilePropertyManager pMan = config.ProfilePropertyManager;
            CorePropertyManager cMan = pMan.GetCoreProperties();
            foreach (CoreProperty prop in cMan.PropertiesWithSection)
            {
                Console.WriteLine("Display Name : " + prop.DisplayName + " Internal Name : " + prop.Name);
            }
        }

        static void Help()
        {
            Console.WriteLine("Usage is: WithinSharePointUpdateUserProfile http://WEBAPPURL C:\\Path\\To\\File");
            Console.WriteLine("To display a list of profile fields: WithinSharePointUpdateUserProfile http://WEBAPPURL");
            Console.WriteLine("File Format is:");
            Console.WriteLine("USERNAME,CORP\\Username,FIELDNAME,FIELDVALUE");
        }

        static UserProfileManager UserManager(SPServiceContext Context)
        {
            return new UserProfileManager(Context, true);
        }

        static void ReadFile(string Path)
        {
            StreamReader reader = new StreamReader(Path);
            string input = null;
            UserProfile UP;
            Dictionary<string, string> User;
            while ((input = reader.ReadLine()) != null)
            {
                User = UpdatedAttributes(input, out UP);
                UsersToUpdate.Add(UP, User);
            }
        }

        static bool UpdateUser(UserProfile Profile, Dictionary<string, string> ProfileChanges)
        {
            Console.WriteLine("Updating user: " + Profile.DisplayName);
            try
            {
                foreach (string k in ProfileChanges.Keys)
                {
                    if (Profile[k] != null)
                    {
                        Profile[k].Value = ProfileChanges[k];
                        Profile[k].Privacy = Privacy.Public;
                    }
                }
                Profile.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error updating profile " + Profile.DisplayName);
                Console.WriteLine(ex.Message);
                return false;
            }
            return true;
        }

        static UserProfile GetProfile(string Username)
        {
            return uMan.GetUserProfile(Username);
        }

        static Dictionary<string, string> UpdatedAttributes(string row, out UserProfile Profile)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();
            string[] split = row.Split(Splitter);
            Profile = GetProfile(split[1]);
            for (int i = 2; i < split.Count(); i++)
            {
                result.Add(split[i], split[i + 1]);
                i++;
            }
            return result;
        }
    }
}

  1. No comments yet.
(will not be published)


%d bloggers like this: