Dev Notes

Notes on Development with Microsoft Technologies

Updating and Enumerating User Profiles in MOSS

3 Comments

Recently, we came across a situation in our environment in which we needed to enumerate all profiles in MOSS 2007 and add a few new profile properties. One of the properties that needed to be updated is the picture url. All of our employees pictures are located in a separate database and an image handler has already been created to serve out the images. So, we wanted to leverage this to place user pictures into the directory (and turn off the user’s ability to modify them).

This project is a service that will run on my MOSS server a pre-determined intervals. I will eventually add it to SharePoint as a timer job to be done just after the user profile import has completed.

So, here’s the code that made it work:

Imports Microsoft.SharePoint
Imports Microsoft.Office.Server.UserProfiles
Module UserProfileUpdaterService

	Sub Main()
		Dim currentSite As New SPSite("http://mymossserver/")
		Dim context As ServerContext = ServerContext.GetContext(currentSite)
		Dim profileManager As New UserProfileManager(context)
		For Each profile As UserProfile In profileManager
			' Get profile properties
			Dim FirstNameProp As UserProfileValueCollection = profile.Item("FirstName")
			Dim LastNameProp As UserProfileValueCollection = profile.Item("LastName")
			Dim ImageProp As UserProfileValueCollection = profile.Item("PictureURL")
			' Get string values from the properties
			Dim FirstName As String = FirstNameProp.Item(0)
			Dim LastName As String = LastNameProp.Item(0)
			Dim PictureURL As String = ImageProp.Item(0)
			' Set a new picture URL
			PictureURL = String.Format("http://imageserver/GetEmployeeImage.ashx?" & _
			"e={0}",GetEmployeeID(FirstName,LastName))
			' Set the image property and commit the changes
			ImageProp.Item(0) = PictureURL 
			profile.Commit()
		Next
	End Sub
End Module

First, we need to get a SPSite. This is done by creating a new instance of SPSite passing the site URL to the constructor. Next, we have to pass the site context to the user profile manager’s constructor. Then we can iterate through all profiles and retrieve properties of the profile. Each property is returned as a UserProfilePropertyValueCollection. You can get an individual property by calling Prop.Item(“PropertyName”). This will return an array of objects. Since most of the properties I’m working with are strings, there’s not much of a need to cast them to other object types. Finally, I query the database with the method GetEmployeeID(FirstName,LastName) (which I have not detailed here) and write the values back into the image property using ImageProp.Item(0)=PictureURL. To write the changes to the profile, I finally call profile.Commit.

Hope this simple example can help some of you make necessary changes to your user profile stores in MOSS 2007.

Advertisements

Author: Chris Quick

I have been a developer of web based solutions since early 2001 delivering solutions to a wide array of organizations using ASP, ASP.NET and SharePoint. I was introduced to SharePoint in 2003 when the consulting firm I worked for at the time introduced it into the workplace. I began working with MOSS 2007 as soon as Microsoft released the RTM version in November 2006. The platform was implemented at the organization I worked for in 2007 and went live in March of that year. I was tasked with the administration and ongoing development of the platform. I currently work as a SharePoint Architect with Artis Consulting, developing solutions for a wide variety of business problems. The goal of this blog is to share my discoveries developing solutions with SharePoint. I welcome your comments and feedback to any post -- and I welcome suggestions for future topics.

3 thoughts on “Updating and Enumerating User Profiles in MOSS

  1. I haven’t had the need to do this, but you may want to investigate impersonation within SharePoint, depending on your need. You can have a web-part run with elevated priviledges through impersonation. Unfortunately, I haven’t come across this scenario just yet. I do know there are some examples in the SDK of doing this, but I would be careful doing this.

  2. Hi Chris,Nice post…I have a question for you, do you know ift it’s possible to read the user profiles inside MOSS (for example, using a web part)? I have implemented a web part that reads the profiles perfetly when using an administrator account. However, it doesn’t work when using other account without administrator privileges…in the schenario I’m doing my proofs I have an Active Directory domain. I will appreciate any help you can provide me.CheersJcp.d: my e-mail address is jcgonzalez@ciin.es

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