Create and Modify User Profiles in the User Profile Store of SharePoint

This example creates a user profile from the master connection configured on the server. It displays the message 'User profile created' after a successful execution.


//Creates a user profile. Obtains the property values from the default
//domain controller or the master connection that is configured on the
//server
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using System.Web;

namespace UserProfilesConsoleApp
{
class Program
{
static void Main(string[] args)
{
try
{
using (SPSite site = new SPSite("http://servername"))
{
ServerContext context =
ServerContext.GetContext(site);
UserProfileManager profileManager = new
UserProfileManager(context);
string sAccount = "domainname\\username";

if (!profileManager.UserExists(sAccount))
{
UserProfile profile = profileManager.CreateUserProfile(sAccount);

if (null == profile)
throw new Exception("Failed to Create User with account :" + sAccount);

else
Console.WriteLine("User profile created.");
}
else
Console.WriteLine("User profile already exists.");

}
}
catch (UserNotFoundException exception)
{
Console.WriteLine(exception.ToString());
}

}
}
}


Now to overrides the preferred name of a user and also overwrites the cell phone number property. It displays the message 'User profile created' after a successful execution.

//Creates a user profile. Obtains the property values from the default
//domain controller or the master connection that is configured on the
//server.
//Also overwrites the Preferred Name and the Cell Phone property for the user.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using System.Web;

namespace UserProfilesApp
{
class Program
{
static void Main(string[] args)
{
try
{
using (SPSite site = new SPSite("http://servername"))
{
ServerContext context = ServerContext.GetContext(site);

UserProfileManager profileManager = new UserProfileManager(context);
UserProfile u = null;
string sAccount = "domainname\\username";
string sPrefered = "preferedname";

if (!profileManager.UserExists(sAccount))
{
u = profileManager.CreateUserProfile(sAccount, sPrefered);
u[PropertyConstants.CellPhone].Value = "nnnnnnnnnn";
u.Commit();


if (null == u)
throw new Exception("Failed to Create User with account :" +
sAccount);
else
Console.WriteLine("User profile created.");

}
else
Console.WriteLine("User profile already exists.");



}
}
catch (UserNotFoundException exception)
{
Console.WriteLine(exception.ToString());
}

}
}
}

Comments

Popular posts from this blog

ContextSwitchDeadlock was detected

Visual Studio setup and deployment projects

Using SvcUtil.exe to generate the proxy class and config file