0

I want to assign a taxonomy field value to the user profile property which is fetched from ListItem. The taxonomy field is single selection valued.

The user profile property name is: Dept.

SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            SPListItem item = properties.ListItem;
            string url = Convert.ToString(item[FieldNames.DepartmentUrl]);
            string title = Convert.ToString(item[FieldNames.DepartmentTitle]);
            string desc = Convert.ToString(item[FieldNames.DepartmentDescription]);

            //SPWeb newWeb = properties.Web.Webs.Add(url,title,desc,1033,Webs.Department.WebTempalate,false,false);

            List<string> nativeMembers = new List<string>();

            SPFieldUserValueCollection nMembers = (SPFieldUserValueCollection)item["oiplbNativeMembers"];

            foreach (SPFieldUserValue field in nMembers)
            {
                SPUser user = field.User;
                nativeMembers.Add(user.LoginName);
            }


            var taxonomyField = item.Fields.GetFieldByInternalName(FieldNames.Department) as TaxonomyField;
            if (taxonomyField.AllowMultipleValues)
            {

            }
            else
            {
              var fieldValue = item[taxonomyField.Title] as TaxonomyFieldValue;    
               var fieldValue = item[taxonomyField.Title] as TaxonomyFieldValue;
                SPServiceContext contex = SPServiceContext.GetContext(properties.Site);
                UserProfileManager upm = new UserProfileManager(contex);                
                UserProfile up = upm.GetUserProfile(nativeMembers[0]);
                up["Dept"].Value = fieldValue.Label;
                up.Commit();
            }



        });

I am stuck by reaching the specific stage and don't know what do? I debugged the program and found that var fieldvalue is assigned taxonomy value successfully.

Please help.

1 Answer 1

1

Get TaxonomyFieldValue from the list. If it is multi-valued field, use this code to get each value

TaxonomyFieldValueCollection tfvc = Item[taxonomyField.Title] as TaxonomyFieldValueCollection; 
foreach (TaxonomyFieldValue e in tfvc)   {   if (!string.IsNullOrEmpty(e.Label))    {     // e.Label;   }   }

Here is my article which explains how to do various operations on user profile

10
  • Thanks sir for replying, I tried your code but I am not getting the property changed, please see my changed code.
    – users1100
    Commented Oct 21, 2013 at 4:23
  • Just some thoughts. 1. First, check if the user exists or not? 2. Is it only problem with this field? 3. Did you try updating any other field? 4. If update doesn't works, try to add a new user through code and verify code is working fine. 5. Debug and see what is the value of up["Dept"].Value after the assignment. Commented Oct 21, 2013 at 4:58
  • Sir, I debugged the whole program and it is running without any error, and checked the user also exists.
    – users1100
    Commented Oct 21, 2013 at 5:00
  • Should I use the property name or the property Display Name?
    – users1100
    Commented Oct 21, 2013 at 5:00
  • Sir, I checked the property LastName of user is being changed but not the custom property.
    – users1100
    Commented Oct 21, 2013 at 5:09

Your Answer

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

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