Creating User Profiles in the User Profile Store in SharePoint Server 2007
Summary: Learn to create a Microsoft Office SharePoint Server 2007 user profile by using the SharePoint UserProfileService Web service.
Applies to: 2007 Microsoft Office System, Microsoft Office SharePoint Server 2007
Joel Krist, Akona Systems
September 2007
Code It | Read It | Explore It
Code It
The following sections show how you can create a user profile by using the Office SharePoint Server 2007 UserProfileService Web service. The process includes four major steps:
Creating a console application project in Microsoft Visual Studio 2005.
Adding a Web reference to the UserProfileService Web service.
Adding a reference to the Microsoft.Office.Server assembly.
Adding code to the console application that creates an Office SharePoint Server user profile.
Creating a Console Application Project in Visual Studio 2005
This procedure shows how to create a console application project.
To create a console application project in Visual Studio 2005
Start Visual Studio.
On the File menu, point to New, and then click Project.
In the New Project dialog box, in the Project Types pane, expand Visual C# or Visual Basic, and then select Windows.
In the Templates pane, select Console Application.
Type CreateUserProfile for the name of the project.
Specify a location for the project, and then click OK.
Visual Studio generates a console application project with a single source file in it called Program.cs or Module1.vb, depending on which language you selected in Step 3.
Adding a Web Reference to the UserProfileService Web Service
This procedure shows how to add a Web reference to the Web service.
To add a Web reference to the UserProfileService Web service
In Visual Studio Solution Explorer, right-click the CreateUserProfile project, and then click Add Web Reference.
In the Add Web Reference dialog box, type the URL of the UserProfileService Web service. Following is the default Web service location:
http://
SiteURL/_vti_bin/userprofileservice.asmx
Click Go. Visual Studio retrieves and displays the information about the Web service.
In the Web reference name field, type UserProfileService, and then click Add Reference to add the Web reference to the project.
Figure 1. Add Web Reference dialog box
Adding a Reference to the Microsoft.Office.Server Assembly
The following code makes use of the PropertyConstants class in the Microsoft.Office.Server.UserProfiles namespace provided by the Microsoft.Office.Server assembly. You need to add a reference to the Microsoft.Office.Server assembly before you can use the PropertyConstants class.
To add a reference to the Microsoft.Office.Server assembly
If you are running Visual Studio on a server that has Office SharePoint Server 2007 installed:
In Visual Studio Solution Explorer, right-click the CreateUserProfile project, and then click Add Reference.
On the .NET tab of the Add Reference dialog box, locate and select the Microsoft Office Server component (Microsoft.Office.Server.dll).
Click OK to add the reference.
If you are running Visual Studio on a computer that does not have Office SharePoint Server 2007 installed, the Microsoft.Office.Server assembly is not available. In this case:
Copy the Microsoft.Office.Server.dll file from a server that has Office SharePoint Server 2007 installed on it to a local project folder on the development computer. Following is the default assembly location on a computer that is running Office SharePoint Server 2007:
%ProgramFiles%\Common Files\Microsoft Shared\Web server extensions\12\ISAPI
In Visual Studio Solution Explorer, right-click the CreateUserProfile project, and then click Add Reference.
In the Add Reference dialog box, on the Browse tab, navigate to the local folder containing the copy of Microsoft.Office.Server.dll.
Select Microsoft.Office.Server.dll.
Click OK to add the reference.
Implementing the User Profile Creation
After adding the references to the UserProfileService Web service and the Microsoft.Office.Server assembly, the next step is to add the user profile creation code.
To add the user profile creation code
Add the following Imports or using statement to the top of the Program.cs or Module1.vb source file. For the Program.cs file, add the using statement after the using statements that Visual Studio generated when you created the project.
Imports Microsoft.Office.Server.UserProfiles
using Microsoft.Office.Server.UserProfiles;
Note
The Imports and using statements enable you to use the classes and types defined in the referenced namespaces without using fully qualified namespace paths.
Replace the code that Visual Studio generated with the following code. For the Module1.vb file, replace the Module1 module. For the Program.cs file, replace the Program class definition.
Module Module1 ' Declare variables to hold the Web service URL, ' target workbook, worksheet, and named range. Dim serviceURL As String = String.Empty Dim accountName As String = String.Empty Dim preferredName As String = String.Empty Dim departmentName As String = String.Empty Dim cellPhone As String = String.Empty Sub Main() Dim bSuccess As Boolean = True Try ' Check command-line arguments. If Not CheckArgs() Then Return End If ' Create an instance of the Web service proxy class. Dim service As UserProfileService.UserProfileService = _ New UserProfileService.UserProfileService() service.Url = serviceURL ' Specify the network credentials of the current ' security context. For the profile creation to succeed ' the credentials must be for an account with ' permissions to create user profiles. service.Credentials = _ System.Net.CredentialCache.DefaultNetworkCredentials ' An alternative approach is to create a ' NetworkCredential object to connect as a ' specific user. Uncomment the following line ' and replace the UserName, Password, and ' Domain placeholders with the credentials of a real ' account that has permissions to create user profiles. ' ' service.Credentials = _ ' New System.Net.NetworkCredential("UserName", _ ' "Password", "Domain") ' Check whether the user already has a profile. Dim propertyData() As UserProfileService.PropertyData = _ Nothing Try propertyData = _ service.GetUserProfileByName(accountName) Catch ex As Exception End Try If Not propertyData Is Nothing Then Throw New Exception(vbCrLf + _ "User profile already exists.") End If ' The profile does not exist, so create it. service.CreateUserProfileByAccountName(accountName) Dim newdata() As UserProfileService.PropertyData = _ New UserProfileService.PropertyData(2) {} ' Set the preferred name property. newdata(0) = New UserProfileService.PropertyData() newdata(0).Name = PropertyConstants.PreferredName newdata(0).Values = New UserProfileService.ValueData(0) {} newdata(0).Values(0) = New UserProfileService.ValueData() newdata(0).Values(0).Value = preferredName newdata(0).IsValueChanged = True ' Set the department property. newdata(1) = New UserProfileService.PropertyData() newdata(1).Name = PropertyConstants.Department newdata(1).Values = New UserProfileService.ValueData(0) {} newdata(1).Values(0) = New UserProfileService.ValueData() newdata(1).Values(0).Value = departmentName newdata(1).IsValueChanged = True ' Set the cell phone property. newdata(2) = New UserProfileService.PropertyData() newdata(2).Name = PropertyConstants.CellPhone newdata(2).Values = New UserProfileService.ValueData(0) {} newdata(2).Values(0) = New UserProfileService.ValueData() newdata(2).Values(0).Value = cellPhone newdata(2).IsValueChanged = True ' Update the user profile. service.ModifyUserPropertyByAccountName(accountName, _ newdata) Catch ex As Exception bSuccess = False Console.WriteLine(ex.Message) End Try If bSuccess Then Console.Write(vbCrLf + _ "User profile successfully created. ") Else Console.Write(vbCrLf + "User profile creation failed. ") End If Console.Write("Press any key to continue...") Console.ReadLine() End Sub #Region "Commandline Argument Processing" Private Function CheckArgs() As Boolean Dim bArgsOK As Boolean = True ' Check the number of command-line arguments. If (My.Application.CommandLineArgs.Count < 5) Then bArgsOK = False Else ' Parse the command-line arguments. For Each arg As String In My.Application.CommandLineArgs If (arg.ToLower().StartsWith("/service=")) Then serviceURL = arg.Remove(0, 9) End If If (arg.ToLower().StartsWith("/accountname=")) Then accountName = arg.Remove(0, 13) End If If (arg.ToLower().StartsWith("/preferredname=")) Then preferredName = arg.Remove(0, 15) End If If (arg.ToLower().StartsWith("/department=")) Then departmentName = arg.Remove(0, 12) End If If (arg.ToLower().StartsWith("/cellphone=")) Then cellPhone = arg.Remove(0, 11) End If Next arg ' Ensure that the URL for the Web service was specified. If (serviceURL = String.Empty) Then Console.Error.WriteLine("Please specify the URL " + _ "to the Web service with the /service= switch." + _ vbCrLf) bArgsOK = False End If ' Ensure that an account name was specified. If (accountName = String.Empty) Then Console.Error.WriteLine("Please specify an " + _ "account with the /accountname= switch." + vbCrLf) bArgsOK = False End If ' Ensure that a preferred name was specified. If (preferredName = String.Empty) Then Console.Error.WriteLine("Please specify a " + _ "preferred name with the /preferredname= " + _ "switch." + vbCrLf) bArgsOK = False End If ' Ensure that a department was specified. If (departmentName = String.Empty) Then Console.Error.WriteLine("Please specify a " + _ "department with the /department= switch." + _ vbCrLf) bArgsOK = False End If ' Ensure that a cell phone was specified. If (cellPhone = String.Empty) Then Console.Error.WriteLine("Please specify a cell " + _ "phone with the /cellphone= switch." + vbCrLf) bArgsOK = False End If End If If Not bArgsOK Then ShowUsage() End If Return bArgsOK End Function Sub ShowUsage() Console.Error.WriteLine("Usage:" + vbCrLf) Console.Error.WriteLine("CreateUserProfile " + _ "/service=ServiceURL /accountname=AccountName " + _ "/preferredname=PreferredName /department=Department " + _ "/cellphone=CellPhone" + vbCrLf) Console.Error.WriteLine("/service= " + _ "The URL of the Web service,") Console.Error.WriteLine(" " + _ "e.g. /service=" + _ "http://SiteURL/_vti_bin/userprofileservice.asmx" + _ vbCrLf) Console.Error.WriteLine("/accountname= " + _ "The account to create the profile for,") Console.Error.WriteLine(" " + _ "e.g. /accountname=Domain\UserName" + vbCrLf) Console.Error.WriteLine("/preferredname= " + _ "The preferred name of the user to use in the new " + _ "profile,") Console.Error.WriteLine(" " + _ "e.g. /preferredname=""Preferred Name""" + vbCrLf) Console.Error.WriteLine("/department= " + _ "The name of the department of the user in the new " + _ "profile,") Console.Error.WriteLine(" " + _ "e.g. /department=Department" + vbCrLf) Console.Error.WriteLine("/cellphone= " + _ "The cell phone number of the user in the new " + _ "profile,") Console.Error.WriteLine(" " + _ "e.g. /cellphone=""(nnn) nnn-nnnn""" + vbCrLf) Console.Error.Write("Press any key to continue...") Console.ReadLine() End Sub #End Region End Module
class Program { // Declare a varible to hold the Web service URL, // the name of the user to create the profile for, and // their preferred name, department, and cell phone number. static string serviceURL = string.Empty; static string accountName = string.Empty; static string preferredName = string.Empty; static string departmentName = string.Empty; static string cellPhone = string.Empty; static void Main(string[] args) { bool bSuccess = true; try { // Check command-line arguments. if (!CheckArgs(args)) return; // Create an instance of the Web service proxy class. UserProfileService.UserProfileService service = new UserProfileService.UserProfileService(); service.Url = serviceURL; // Specify the network credentials of the current // security context. For the profile creation to // succeed the credentials must be for an account // with permissions to create user profiles. service.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; // An alternative approach is to create a // NetworkCredential object to connect as a // specific user. Uncomment the following lines // and replace the UserName, Password, and // Domain placeholders with the credentials of a real // account that has permissions to create user profiles. // // service.Credentials = // new System.Net.NetworkCredential("Username", // "Password", "Domain); // Check whether the user already has a profile. UserProfileService.PropertyData[] propertyData = null; try { propertyData = service.GetUserProfileByName(accountName); } catch { } if (propertyData != null) { throw new Exception( "\r\nUser profile already exists."); } // The profile does not exist, so create it. service.CreateUserProfileByAccountName(accountName); // Set some of the profile properties. UserProfileService.PropertyData[] newdata = new UserProfileService.PropertyData[3]; // Set the preferred name property. newdata[0] = new UserProfileService.PropertyData(); newdata[0].Name = PropertyConstants.PreferredName; newdata[0].Values = new UserProfileService.ValueData[1]; newdata[0].Values[0] = new UserProfileService.ValueData(); newdata[0].Values[0].Value = preferredName; newdata[0].IsValueChanged = true; // Set the department property. newdata[1] = new UserProfileService.PropertyData(); newdata[1].Name = PropertyConstants.Department; newdata[1].Values = new UserProfileService.ValueData[1]; newdata[1].Values[0] = new UserProfileService.ValueData(); newdata[1].Values[0].Value = departmentName; newdata[1].IsValueChanged = true; // Set the cell phone property. newdata[2] = new UserProfileService.PropertyData(); newdata[2].Name = PropertyConstants.CellPhone; newdata[2].Values = new UserProfileService.ValueData[1]; newdata[2].Values[0] = new UserProfileService.ValueData(); newdata[2].Values[0].Value = cellPhone; newdata[2].IsValueChanged = true; // Update the user profile. service.ModifyUserPropertyByAccountName(accountName, newdata); } catch (Exception ex) { bSuccess = false; Console.WriteLine(ex.Message); } if (bSuccess) Console.Write("\r\nUser profile successfully created."); else Console.Write("\r\nUser profile creation failed. "); Console.Write("Press any key to continue..."); Console.ReadLine(); } #region Commandline Argument Processing private static bool CheckArgs(string[] args) { bool bArgsOK = true; // Check the number of command-line arguments. if (args.Length < 5) { bArgsOK = false; } else { // Parse the command-line arguments. foreach (string arg in args) { if (arg.ToLower().StartsWith("/service=")) serviceURL = arg.Remove(0, 9); if (arg.ToLower().StartsWith("/accountname=")) accountName = arg.Remove(0, 13); if (arg.ToLower().StartsWith("/preferredname=")) preferredName = arg.Remove(0, 15); if (arg.ToLower().StartsWith("/department=")) departmentName = arg.Remove(0, 12); if (arg.ToLower().StartsWith("/cellphone=")) cellPhone = arg.Remove(0, 11); } // Ensure that the URL for the Web service was // specified. if (serviceURL == string.Empty) { Console.Error.WriteLine("Please specify the URL to " + "the Web service with the /service= switch.\r\n"); bArgsOK = false; } // Ensure that an account name was specified. if (accountName == string.Empty) { Console.Error.WriteLine("Please specify an account " + "with the /accountname= switch.\r\n"); bArgsOK = false; } // Ensure that a preferred name was specified. if (preferredName == string.Empty) { Console.Error.WriteLine("Please specify a name " + "name with the /preferredname= switch.\r\n"); bArgsOK = false; } // Ensure that a department was specified. if (departmentName == string.Empty) { Console.Error.WriteLine("Please specify a " + "department with the /department= switch.\r\n"); bArgsOK = false; } // Ensure that a cell phone was specified. if (cellPhone == string.Empty) { Console.Error.WriteLine("Please specify a cell " + "phone with the /cellphone= switch.\r\n"); bArgsOK = false; } } if (!bArgsOK) ShowUsage(); return bArgsOK; } private static void ShowUsage() { Console.Error.WriteLine("Usage:\r\n"); Console.Error.WriteLine("CreateUserProfile " + "/service=ServiceURL /accountname=AccountName " + "/preferredname=PreferredName /department=Department " + "/cellphone=CellPhone\r\n"); Console.Error.WriteLine("/service= " + "The URL of the Web service,"); Console.Error.WriteLine(" " + "e.g. /service=" + "http://SiteURL/_vti_bin/userprofileservice.asmx\r\n"); Console.Error.WriteLine("/accountname= " + "The account to create the profile for,"); Console.Error.WriteLine(" " + "e.g. /accountname=Domain\\UserName\r\n"); Console.Error.WriteLine("/preferredname= " + "The preferred name of the user to use in the new " + "profile,"); Console.Error.WriteLine(" " + "e.g. /preferredname=\"Preferred Name\"\r\n"); Console.Error.WriteLine("/department= " + "The name of the department of the user in the new " + "profile,"); Console.Error.WriteLine(" " + "e.g. /department=Department\r\n"); Console.Error.WriteLine("/cellphone= " + "The cell phone number of the user in the new " + "profile,"); Console.Error.WriteLine(" " + "e.g. /cellphone=\"(nnn) nnn-nnnn\"\r\n"); Console.Error.Write("Press any key to continue..."); Console.ReadLine(); } #endregion }
Build and run the CreateUserProfile application. The CreateUserProfile application accepts the required command-line arguments shown in Table 1.
Table 1. Required command-line arguments accepted by CreateUserProfile
Switch Meaning /service=
The URL of the User Profile Web service, for example:
/service=http://
SiteURL/_vti_bin/userprofileservice.asmx
/accountname=
The account to create the profile for, for example:
/accountname=Domain\UserName
/preferredname =
The preferred name of the user to use in the new profile, for example:
/preferredname="Preferred Name"
/department =
The name of the department of the user in the new profile, for example:
/department=Department
/cellphone=
The cell phone number of the user in the new profile, for example:
/cellphone="(123) 123-4567"
Read It
This article demonstrates how you can create Office SharePoint Server user profiles by using the UserProfileService Web service. To create a user profile, you:
Create a console application project in Visual Studio 2005.
Add a Web reference to the UserProfileService Web service to the Visual Studio project.
Add a reference to the Microsoft.Office.Server assembly.
Add code to the console application that creates an Office SharePoint Server user profile.