Thursday, September 24, 2009

How to Update User Profile using C#

using (SPSite site = new SPSite(“http://portal”))

{
Microsoft.Office.Server.ServerContext Context = ServerContext.GetContext(site);
Microsoft.Office.Server.UserProfiles.UserProfileManager Mngr = new UserProfileManager(Context);
Microsoft.Office.Server.UserProfiles.UserProfile Profile = Mngr.GetUserProfile(“litwareinc\ashishk”);
System.Console.Write(Profile[“Title”].Value.ToString());
Profile[“Title”].Value = “New Title”;
Profile.Commit();
}


Bingo, here ashishk Title is updated with "New Title"

Tuesday, September 22, 2009

How to retrieve account names from UserProfile

It is quite difficult to get accountnames from userprofile database. Using SDK, you can get the account by following example

using (SPSite site = new SPSite(http://portal/))
{
        ServerContext context = ServerContext.GetContext(site);
        UserProfileManager profileManager = new UserProfileManager(context);

        foreach (UserProfile profile in profileManager)
       {
             Console.WriteLine(profile[PropertyConstants.AccountName]);
       }
}