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
}