Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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)

share|improve this answer
    
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 '13 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 Oct 23 '13 at 17:49

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

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

Hope this helps. :)

share|improve this answer
    
I see no updates being made, how will this help? –  Robert Lindgren Oct 22 '13 at 15:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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