private static void AddUser()
{
try
{
using (SPSite site = new SPSite("http://test1"))
{
ServerContext context = ServerContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);
if (!profileManager.UserExists("senfo"))
{
UserProfile profile = profileManager.CreateUserProfile("senfo");
profile[PropertyConstants.WorkEmail].Value = "me@myaddress.com";
profile.Commit();
}
else
{
Console.WriteLine("User already exists...");
}
}
}
catch (UserNotFoundException err)
{
Console.WriteLine(err.ToString());
}
}
It's important to note that if you're using multiple membership providers that this code will throw the following Exception "No mapping between account names and security IDs was done". To correct the issue, add "MembershipProviderName:" in front of the username. For example, "SqlMembershipProvider:senfo".
No comments:
Post a Comment