Bulk Loading Profile Properties


So, I got asked this morning for a simple way to bulk load and map user profile fields so that profile properties could easily be copied from one environment to the next. Makes sense, so I built a little command line tool that reads an XML file for doing just that. When I was about mid-way through I remember Gary Lapointe had already built a tool that does exactly the same thing! So I scrapped what I was doing and recommended his excellent tools for doing the job. However, I thought it would be interesting to at least show how this stuff all works. :)

Importing Profile Properties – Gary Lapointe admin extensions

First bit was making an XML template that could be easily followed for first checking existing properties, and secondly adding any properties that didn’t already exist. I opted to not publish all possible options, because in this case a few of the options could be predetermined for all fields:

<?xml version=“1.0” encoding=“utf-8” ?>
<Properties>
<Property>
<Name></Name>
<DisplayName></DisplayName>
<Type></Type>
<Description></Description>
<Length></Length>
</Property>
</Properties>

Type can be Choice, string, numeric etc. The rest are pretty self-explanatory. Next comes the guts of the process :) dirty but functional. Split in to two main components, first opens up the site, gets the current property store and reads the xml. The second is just a little tiny thing that see’s if a property already exists before we try and build the new property.

using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;//Check existence of property
internal static bool Exists(string property, PropertyCollection properties)
{
foreach (Property prop in properties)
{
if (prop.Name == property)
{
return true;
}
}
return false;
}

//main function
SPSite site = new SPSite(args[0].ToString());
ServerContext _srv = ServerContext.GetContext(site);
UserProfileManager profileMgr = new UserProfileManager(_srv, true);
UserProfileConfigManager profileConfig = new UserProfileConfigManager(_srv);
PropertyCollection userProperties = profileConfig.GetProperties();
XmlTextReader reader = new XmlTextReader(“properties.xml”);
string name;

while (reader.Read())
{
reader.ReadToFollowing(“Property”);
reader.ReadToFollowing(“Name”);
name = reader.Value;

if (!Exists(name, profileMgr.Properties))
{
bool exception = false;
try
{
Property newProp = userProperties.Create(false);
newProp.Name = name;
reader.ReadToFollowing(“DisplayName”);
newProp.DisplayName = reader.Value;
reader.ReadToFollowing(“Type”);
newProp.Type = reader.Value;
newProp.PrivacyPolicy = PrivacyPolicy.OptIn;
reader.ReadToFollowing(“Description”);
newProp.Description = reader.Value;
newProp.IsSearchable = true;
newProp.IsVisibleOnEditor = true;
newProp.IsAlias = false;
reader.ReadToFollowing(“Length”);
newProp.Length = Convert.ToInt16(reader.Value);
newProp.IsUserEditable = false;
userProperties.Add(newProp);
}

catch (Exception ex)
{
Console.WriteLine(“Error adding new property: “ + ex.Message);
}

finally
{
if (!exception)
{
Console.WriteLine(“Added property: “ + name);
}
}
}
}
site.Dispose();

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


%d bloggers like this: