Friday, June 4, 2010

Add a custom User Property and Set / Retrieve its value via code in Sharepoint 2010

In Central Admin you need to get to the Manage User Properties page. To get there you need to do the following:

Under Application Management click: Manage service applications -> User Profile Service Application -> Under People click: Manage User Properties

Once on the Manage User Properties page, click the "New Property" link. We'll set some required information here like Name and Display Name. The Name field is the name we'll reference in code.





Continue on, we'll click the "Allow users to edit values for this property" under Edit Settings. If you want to allow users to edit this value in their Profile, you will also need to check "Show in the profile properties.." and "Show on the Edit Details page" under Display Settings.



Click OK, and you'll now see your custom property in the list. If you selected to allow users to edit this via their profile, you can surf to your user profile and edit it, and you should see this property available to you.



Now in Visual Studio, we can create a VisualWebPart, and add references to the following assemblies:
Microsoft.Office.Server
Microsoft.Office.Server.UserProfiles



Now in the page load we'll call 2 different methods to demostrate setting and retrieving the custom property:


protected void Page_Load(object sender, EventArgs e)

        {

            string emptyPetName = GetPetName();

            SetPetName();

            string assignedPetName = GetPetName();



The GetPetName method simply creates a profile object using the UserProfileManager and checks the the property we are looking for, in this case PetName. Something to note here:
1. The property value must be cast to its proper type.


/// <summary>

        /// Get a custom User Property from the User Profile

        /// </summary>

        /// <returns>User's Pet Name</returns>

        private String GetPetName()

        {

            var profileManager = new UserProfileManager(SPServiceContext.Current);

            var profile = profileManager.GetUserProfile(false);

            if (profile != null)

            {

                var property = profile["PetName"];

                if (property != null)

                {

                    return (String)property.Value;

                }

                else

                    return "Pet Name property could not be found.";

            }

            else

                return "The profile could not be found.";

        }



The SetPetName method sets the PetName property, using a similar method to the GetPetName method. A couple things to note here:
1. After setting the property value, you must call the Commit() method on the profile object.
2. Before calling Commit(), you must set AllowUnsafeUpdates to true on the SPContext.Current.Web object, then set it back to false after the commit (since we are calling commit on a Page Load/GET and not a Post).


/// <summary>

        /// Sets a custom User Property from the User Profile

        /// </summary>

        private void SetPetName()

        {

            var profileManager = new UserProfileManager(SPServiceContext.Current);

            var profile = profileManager.GetUserProfile(false);

            if (profile != null)

            {

                var property = profile["PetName"];

                if (property != null)

                {

                    property.Value = "Fluffy";

                    SPContext.Current.Web.AllowUnsafeUpdates = true;

                    profile.Commit();

                    SPContext.Current.Web.AllowUnsafeUpdates = false;

                }

            }

        }



Now running this code, the first time we hit the GetPetName method, the name will come back as null (assuming it hasn't been set yet). The next call to the SetPetName method will set the value to Fluffy, and the second call to GetPetName will return Fluffy.



Now, we can surf back to the user profile and see that our Pet Name is showing up in the User Profile.

1 comment:

  1. Hi Atkinson,

    Am using the code given above. But when i retrieve the value it is throwing the error:

    Property not defined:
    An administrator must create this property in the profile administration tool

    Kindly help me.

    ReplyDelete