Index was out of range. Must be non-negative and less than the size of the collection. errro while setting user profile property.

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);

What am I missing?

  • Edited by Mohemmad K Wednesday, February 05, 2014 4:52 AM Added content
February 5th, 2014 7:50am

Can it be the profile property is not editable? you need to manually make the property editable. Also does the current user has permission to update the profile propery?
Free Windows Admin Tool Kit Click here and download it now
February 5th, 2014 9:28am

Yes sir, the property is editable, because when I set the property a string value i.e.
up["oiplbNativeDepartment"].Value = "Department 1";
then it stores the value. The issue arise only when I use
up["oiplbNativeDepartment"].AddTaxonomyTerm()
method.
February 5th, 2014 9:36am

Did you get anything regarding this?
  • Proposed as answer by Srinu16 20 minutes ago
  • Unproposed as answer by Srinu16 20 minutes ago
Free Windows Admin Tool Kit Click here and download it now
February 20th, 2014 8:17am

Did you get anything regarding this?
  • Proposed as answer by Srinu16 31 minutes ago
  • Unproposed as answer by Srinu16 31 minutes ago
February 20th, 2014 8:17am

can you try like this:

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

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

userprofile.Commit();

Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2015 6:47am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics