1

I want to set the user profile property from code behind. The property is of type taxonomy(single value).

Following is a code which is I am executing:

SPList events = web.Lists["Calendar"];
//  Term term = null;
//  Term term1 = null;
SPListItem item = events.GetItemById(3);
if (item != null)
{
    TaxonomyFieldValueCollection valueColl = item["oiplbDepartment"] as TaxonomyFieldValueCollection;
    TaxonomyFieldValue value = valueColl[0];
    TaxonomyField field = item.Fields.GetFieldByInternalName("oiplbDepartment") as TaxonomyField;
    if (value != null)

    {
        TaxonomySession session = new TaxonomySession(site);
        TermStore termStore = session.TermStores[0];
        TermSet termSet = termStore.GetTermSet(field.TermSetId);
        //term = termSet.GetTerm(new Guid(value.TermGuid));
        //term1 = termSet.GetTerm(new Guid(valueColl[1].TermGuid));
        SPUser user = web.EnsureUser(@"xyz");
        if (user != null)
        {
            SPServiceContext serviceContext = SPServiceContext.GetContext(site);                                
            UserProfileManager upm = new UserProfileManager(serviceContext);
            UserProfile up = upm.GetUserProfile(@user.LoginName);
            up["oiplbNativeDepartment"].Clear();
            for (int i = 0; i < valueColl.Count; i++)
            {
                Guid guid = new Guid(valueColl[i].TermGuid);
                Term t = termSet.GetTerm(guid);
                up["oiplbNativeDepartment"].AddTaxonomyTerm(t);
            }
            up.Commit();

        }


    }
}

I debugged the program and the error which is mentioned above comes at line up["oiplbNativeDepartment"].AddTaxonomyTerm(t); I referred this post to set the user profile property.

What am I missing?

1

Ok, firstly, make sure that the property exists and isUserEditable as per below:

Guid guid = new Guid(valueColl[i].TermGuid);
Term t = termSet.GetTerm(guid);
var prop = up.Properties.GetPropertyByName("oiplbNativeDepartment");
if (prop != null && prop.IsUserEditable)
{
    up[prop.Name].Value = value;
}
  • 1
    I want to set the value to the user profile property not to the list. :-) – users1100 Feb 4 '14 at 7:22
  • Apologies.. Check that? – Fox Feb 4 '14 at 7:37
  • Check condition fails at the statement if(prop != null && prop.IsUserEditable) but when I explicitly assign value like up["oiplbNativeDepartment"].value = "Department 1" then it assigns successfully.. What should I do to assign the term instead of string value? – users1100 Feb 4 '14 at 8:47
  • Try to rather assign a TermValue than a string. A TermValue contains a Guid and Label for the term. But you're saying the Check condition fails.. What happens when you debug.. Is the prop null? THat means the property doesn't exist. – Fox Feb 4 '14 at 10:03
  • When I debug the code it does not go to the if condition's statements. And if the property is not exist, then how would I able to store the .value as string? like up["oiplbNativeDepartment"].value = "Department 1" – users1100 Feb 4 '14 at 10:16
0

Instead of the for loop given below:

for (int i = 0; i < valueColl.Count; i++)
{
    Guid guid = new Guid(valueColl[i].TermGuid);
    Term t = termSet.GetTerm(guid);
    up["oiplbNativeDepartment"].AddTaxonomyTerm(t);
}

Can you try the following:

Try up["oiplbNativeDepartment"].Value = value;

  • It gives compile time error that indicates: Property or indexer "Microsoft.Office.Server.UserProfiles.UserProfile.this[string]" cannot be assigned to read only property. – users1100 Feb 4 '14 at 6:02
  • Please check update – Arsalan Adam Khatri Feb 4 '14 at 6:20
  • Tried that way that throws an error: 'up["oiplbNativeDepartment"].Value' threw an exception of type 'System.InvalidCastException' – users1100 Feb 4 '14 at 6:22
  • And one more thing I want to mention you sir, is that I have done this thing in "Multi Value" string taxonomy user profile property and this works perfectly. The problem persists for single value string user profile property only. – users1100 Feb 4 '14 at 6:25
  • Sorry I missed that, if its single value why are you trying looping and adding all the Terms? It will accept an object of TaxonomyFieldValue as value, check the update – Arsalan Adam Khatri Feb 4 '14 at 6:28
0

Try like this:

userprofile["propertyname"].Add(" ");

userprofile["propertyname"].AddTaxonomyTerm(taxonomyterm);

userprofile.Commit();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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