Importing Profiles in SharePoint

Slalom Consultant Maarten Sundman

Slalom Consultant Maarten Sundman specializes in .NET, SharePoint, and Silverlight solutions and has experience in the Financial Services, Software, and Utilities and Energy sectors.

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, language, etc. Normally it’s painful to set up these accounts as typically they end up being setup by hand in the user profile service application; therefore I’ve gone ahead and built a tool which  reads all the attributes for a profile from a CSV and updates the users profiles (code below). This also works for normal accounts if you’re trying to import in bulk via Excel or some other data source for a one time import which can happen during migrations from other systems.


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> UsersToUpdate = new Dictionary<UserProfile,Dictionary>();

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&#8221;);
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 User;
while ((input = reader.ReadLine()) != null)
{
User = UpdatedAttributes(input, out UP);
UsersToUpdate.Add(UP, User);
}
}

static bool UpdateUser(UserProfile Profile, Dictionary 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 UpdatedAttributes(string row, out UserProfile Profile)
{
Dictionary result = new Dictionary();
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;
}
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: