0

I am accessing the user profile property from code but it returns null value as well as false in the boolean type property.

My code is:

if (web != null)
{

    SPList list = ListHelper.GetListByUrl(web.Lists,ListNames.DepartmentsUrl);


    if (list != null)
    {
        SPQuery query = new SPQuery();
        query.Query = string.Format(CamlQueries.Department.QueryForDepartmentUrl, properties.Web.Name);
        SPListItemCollection coll = list.GetItems(query);

        if (coll != null)
        {
            SPListItem item = coll[0];
            var taxonomyField = item.Fields.GetFieldByInternalName(FieldNames.Department) as TaxonomyField;
            var fieldValue = item[taxonomyField.Id] as TaxonomyFieldValue;
            SPServiceContext contex = SPServiceContext.GetContext(properties.Web.Site);
            if (contex != null)
            {
                 UserProfileManager upm = new UserProfileManager(contex);
                 #region Change User Profile Properties
                 if (upm != null)
                 {

                        SPSecurity.RunWithElevatedPrivileges(delegate()
                                {
                            UserProfile u = upm.GetUserProfile(user[0]);

                            if ((bool)u[FieldNames.Manager].Value)
                            {
                             properties.ErrorMessage = "Manager is already assigned to other department";
                            properties.Status = SPEventReceiverStatus.CancelWithError;
                            properties.Cancel = true;




                            }
                            else
                            {
                                if (groupAdded.Name.EndsWith(UserGroups.Managers.Name))
                                     ....

Here in the condition :if ((bool)u[FieldNames.Manager].Value) I am getting false but in the user profile property from central admin it is true.

What should I do?

N.B. The property's privacy is set to Everyone.

2 Answers 2

1

Try this : u[PropertyConstants.Manager].Value; instead of u[FieldNames.Manager].Value .

Else How to get user profile property value when privacy for property set to "Private" (only me)

2
  • Thanks for reply sir, but the [FieldNames.Manager] returns the name of the custom property of user profile. So how would I get that custom property in property. Actually manager is boolean in my case
    – users1100
    Oct 23, 2013 at 4:09
  • Not sure i understood you correctly . But I have a bool custom property ABCD-DEFGFG with Display Name as ABC .I use the below code . UserProfileValueCollection valuesr = profile["ABCD-DEFGFG"]; bool VOlla = (bool)valuesr.Value; if (VOlla) { i++; } else{ }
    – Nikhil J
    Oct 23, 2013 at 17:49
-1

I guess you need to add "AllowUnsafeUpdates" in your code. Adding this might help.

 web.AllowUnsafeUpdates=true;
 web.Update();

Hope this helps. :)

1
  • I see no updates being made, how will this help? Oct 22, 2013 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.