A recent project I begin working on required me to pull data from SharePoint MySite User Profiles. Luckily Microsoft provided a great way to access the fields in user profiles. In this post I will croncical how to leverage VB.NET and the SharePoint web services to get the job done.

Step 1

First step is to fire up Visual Studio and create a new project.

Step 2

The next step is to add a simple aspx page to your solution.

Chose Web Form and give it the name Default.aspx and click Add

Step 3

The next step is to add web reference(s) to SharePoint’s web services. In my project it was required that I collect a profile field for each user in SharePoint.

Enter the URL to the web service:

Enter a name for your web service… I called my “wsUP”

Step 4

Switch to the Code-Behind of the default.aspx file and add the following in the page_load event.

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim wsUserProfiles As wsUP.UserProfileService = New wsUP.UserProfileService
        wsUserProfiles.Credentials = System.Net.CredentialCache.DefaultCredentials

        Dim numProfiles As Integer = CType(wsUserProfiles.GetUserProfileCount, Integer)

        For i As Integer = 0 To numProfiles

            Dim profileIndex As wsUP.GetUserProfileByIndexResult = wsUserProfiles.GetUserProfileByIndex(i)

            'Login Name

            'First Name

            'Last Name


    End Sub

End Class

This should get you an idea of what you can do. I reccommend setting a debug point on the “profileIndex” variable to figure out other indices of UserProfile like “work email, department, etc”.

One thing to look out for is users running this code that aren’t administrators. The GetUserProfileCount method needs administrator privileges for the user running the code in Sharepoint. If you need to use credentials different from the default machine credentials you can explicitly set them:

Dim credentials As NetworkCredential = New NetworkCredential("DOMAIN\accountLogin", "mypassword")
wsUserProfiles.Credentials = credentials

Note: you will need to Imports System.Net

I’d like to find a more efficient way to grab specific profile properties from all the users without looping through the whole user profile. If anybody has any ideas please chime in.

Related Links: