Showing posts with label User Profile Manager. Show all posts
Showing posts with label User Profile Manager. Show all posts

Thursday, January 19, 2012

SharePoint 2010 - Update user's infomation using UserProfileManager programmatically.

Goal

To update a user's information using C#. In the below given sample i am changing the Email(WorkEmail) of a user.

Important facts

  • User trying to update a profile should be updating own record or should be an "Admin in User Profile Service". If the user is not an Admin in User Profile Service the error will come "You must have manage user profiles administrator rights to use administrator mode.".
  • In below given code I am using true for IgnoreUserPrivacy. Default it should be false but i also wanted to make this work.
    UserProfileManager(SPServiceContext.Current , true)
  • Most Important : The user i am using to run this is an Admin in User Profile Service. So the code is working fine.

Code

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite _site = new SPSite(SPContext.Current.Site.Url))
    {
        using (SPWeb _web = _site.OpenWeb())
        {
            //Gets the profile manager object
            UserProfileManager _profileManager = new UserProfileManager(SPServiceContext.Current , true);
                           
            //UserName
            string _userName = @"MyMachine\User";

            //Gets the profile of a User
            UserProfile _userProfile = _profileManager.GetUserProfile(_userName);
            if (_userProfile != null)
            {
                //Change the Email value
                ((UserProfileValueCollection)_userProfile["WorkEmail"]).Value = "sample@someDomain.com";
                               
                //Save the record
                _userProfile.Commit();
                               
            }
        }
    }
});

This code works absolutely fine. For the developers who are getting issue like "You must have manage user profiles administrator rights to use administrator mode." please click the error to visit the solution.

Friday, November 4, 2011

SharePoint 2010 - Get My Personal Site URL and all other settings programmatically.

Summary

Guys, I found out a good way to retrieve the My Site > My Content(Personal Site) url using C#.

Solution

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    //Trying to get the Personal Site link or Url
    //SPContext.Current.Site = http://sp2010/
    //_url = http://sp2010:50000/personal/administrator/
                
    //The main site.
    SPSite _site = SPContext.Current.Site;
                
    //Gets the service context of the site.
    SPServiceContext _serviceContext = SPServiceContext.GetContext(_site);
                
    //On the basis of Service Manager 
    UserProfileManager _userProfileManager = new UserProfileManager(_serviceContext);
               
    //Get the User profile from User Profile Manager
    //True is passed in the constructor to create a My Site User Profile if it doesnot exists.
    UserProfile _userProfile = _userProfileManager.GetUserProfile(true);

    //Check if the Personal Site exists
    if (_userProfile.PersonalSite != null)
    {
        //This should give the link in the my site                   
        string _url = _userProfile.PersonalUrl.OriginalString;
    }
    else
    {
        //My Content(Personal Site) Does Exists
    }
});

Hope this helps someone facing issue to find a piece of code to get the url. The same way we can use _userProfile to get all the many other My Site Settings.

Wednesday, August 31, 2011

"User Information List" instead of "UserProfileManager"

For all those friends who were trying to find out an option for "UserProfileManger" to get the information of user, I found "User Information List" on the root. I am still not sure if it gets sync directly to the active directory or any other user list. But will write a blog on this in future once i find it.