November 24th, 2009Modifying User Profile Values Programmatically
SharePoint has pretty amazing “people” searching capabilities. However, if you are like our organization, your Active Directory data may not be as complete as you’d like it to be. Or maybe you manage identities using another system like PeopleSoft. So the question becomes: How do you get identity information into SharePoint?
I decided to develop a small console application that would insert user profile data, pulled from our PeopleSoft database, into the SharePoint User Profile Store:

- What kind of dark magic must be peformed to get information in here!?
Also, we had employee photos, sitting in a web directory on the Central Admin, front-end web server, that I wanted to point to for the profile pictures.
All of this can be done using the SharePoint User Profile Manager class in the SharePoint API.
1. Start a Console Project and add references to System.Web, Microsoft.SharePoint, and Microsoft.Office.Server
(NOTE: If you are deving on a box that doesn’t have WSS or MOSS installed, you won’t be able to debug, however, you can still add the .dll files to your project for compiling. Just go to your MOSS or WSS server and pull the Microsoft.Office.Server.dll and the Microsoft.SharePoint.dll [%Program Files%Common Files\Microsoft Shared\web server extensions\12\ISAPI] into your project)
Below is the code I used. Obviously, there are many variants to this to make it work and no two data structures are the same. However, I think the general concept could work for many different environments.
Essentially, what I did was iterate through a DataTable that had all my employee data in it. I then pulled up the user profile, using the SharePoint Profile Manager, and inserted the profile property values.
All of the values I’m referencing (PhoneNo, JobTitle, Supervisor_NetworkID, etc.) were available in our PeopleSoft database. If you don’t have access to that level of data, or it’s not being collected in your organization…well…this will be kinda hard.
Finally, I committed the changes in SharePoint and moved on to the next record:
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace SharePoint_Employee_Photos
{
class Program
{
static void Main(string[] args)
{
try
{
//Get usernames
//Retrieve E-Mail Addresses
SqlConnection sqlconn1 = new SqlConnection("Data Source=Server;Initial Catalog=Database;User Id=dbuser;Password=dbpassword;");
SqlCommand sqlcomm1 = new SqlCommand("Stored_Procedure", sqlconn1);
sqlcomm1.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(sqlcomm1);
DataTable dt = new DataTable();
sda.Fill(dt);
using (SPSite spSite = new SPSite(@"http://sharepoint.site.org"))
{
ServerContext siteContext = ServerContext.GetContext(spSite);
UserProfileManager pManager = new UserProfileManager(siteContext);
foreach (DataRow dr in dt.Rows)
{
String strUsername = "Domain\\" + dr["NetworkID"].ToString();
UserProfile userProfile = null;
//Check to see if the profile exists in SharePoint
if (pManager.UserExists(strUsername))
{
userProfile = pManager.GetUserProfile(strUsername);
Console.WriteLine("User Success: Domain\\" + dr["NetworkID"].ToString());
//Update any of the user profile property values
//Photos are made available through a separate SSIS task that queries the PeopleSoft database and outputs
//the images to a web folder. The web folder is served on my Central Admin, web front end server.
//Let’s make sure the file is there first.
if (File.Exists(@"[C:\PhotoWebsite\photos\" + dr["EmpID"].ToString() + ".jpg"))
{
userProfile[PropertyConstants.PictureUrl].Value = "http://website.photos.org/photos/" + dr["EmpID"].ToString() + ".jpg";
}
if (!dr["PhoneNo"].Equals(DBNull.Value))
{
if (!dr["PhoneNo"].Equals(String.Empty))
{
userProfile[PropertyConstants.WorkPhone].Value = dr["PhoneNo"].ToString();
}
}
if (!dr["DeptName"].Equals(DBNull.Value))
{
if (!dr["DeptName"].Equals(String.Empty))
{
userProfile[PropertyConstants.Department].Value = dr["DeptName"].ToString();
}
}
if (!dr["JobTitle"].Equals(DBNull.Value))
{
if (!dr["JobTitle"].Equals(String.Empty))
{
userProfile[PropertyConstants.Title].Value = dr["JobTitle"].ToString();
}
}
if (!dr["Supervisor_NetworkID"].Equals(DBNull.Value))
{
if (!dr["Supervisor_NetworkID"].Equals(String.Empty))
{
String strSupervisorUsername = "Domain\\" + dr["Supervisor_NetworkID"].ToString();
if (pManager.UserExists(strSupervisorUsername))
{
userProfile[PropertyConstants.Manager].Value = strSupervisorUsername;
Console.WriteLine("Supervisor Success: Domain\\" + dr["Supervisor_NetworkID"].ToString());
}
}
}
userProfile[PropertyConstants.WorkEmail].Value = dr["Email"].ToString();
//Commit the changes
userProfile.Commit();
Console.WriteLine("Import Success!");
}
}
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
Console.ReadLine();
}
}
}
}