0

I want to add value to the managed metadata column of list item. The term which is being inserted is selected from taxonomy web tagging control.

My declarative part is:

<tr>
    <td>
        <asp:Label ID="lblProjectOwnerDepartment" runat="server" Text="Owner Department"></asp:Label>
    </td>
    <td>
        <Taxonomy:TaxonomyWebTaggingControl OnLoad="taxProjectOwnerDepartment_Load" ID="taxProjectOwnerDepartment" runat="server" Visible="true" IsDisplayPickerButton="true" IsMulti="false"
              AllowFillIn="false" IsAddTerms="false" IsIncludePathData="false"></Taxonomy:TaxonomyWebTaggingControl>

        </td>
    <td>

        <asp:CustomValidator ID="cvProjectOwnerDepartment" runat="server" OnServerValidate="cvProjectOwnerDepartment_ServerValidate"  />
        <asp:Label ID="lblError" runat="server" Visible="false" />
    </td>
</tr>  

Code to insert term is:

SPWeb web = site.AllWebs["projects"];

         SPList list = web.Lists["Project List"];

         SPListItem item = list.AddItem();

        lblError.Visible = false;
        TaxonomyFieldValueCollection taxFldColl = new TaxonomyFieldValueCollection(string.Empty);
        taxFldColl.PopulateFromLabelGuidPairs(taxProjectOwnerDepartment.Text);

        if (taxFldColl.Count != 0 )
        {
            if (taxFldColl[0].TermGuid == "00000000-0000-0000-0000-000000000000")
            {
                lblError.Text = "Please Enter Valid Department";
                lblError.ForeColor = System.Drawing.Color.Red;
                lblError.Visible = true;
                return;
            }
            else
            {
                TaxonomyFieldValueCollection values = new TaxonomyFieldValueCollection(string.Empty);
                values.PopulateFromLabelGuidPairs(taxProjectOwnerDepartment.Text);

                foreach (TaxonomyFieldValue fldValue in values)
                {
                    TaxonomyField taxField = (TaxonomyField)item["Dept"];
                    TaxonomyFieldValue taxFieldValue = new TaxonomyFieldValue(taxField);
                    taxFieldValue.TermGuid = fldValue.TermGuid;
                    taxFieldValue.Label = fldValue.Label;
                    item[taxField.Id] = taxFieldValue;
                    item[taxField.TextField] = taxFieldValue;
                    item.Update();
                }


            }
        }

When I run this code the error is thrown intimating that the Value cannot be null.

What am I missing?

4

You should check more carefully what you are doing in the code. For example in the line

TaxonomyField taxField = (TaxonomyField)item["Dept"];

you are trying to cast TaxonomyFieldValue object to TaxonomyField, which is incorrect, so you get ArgumentNullException on the next line because taxField becomes null. And you are resetting item field value for each entry in the collection, which seems to be incorrect logic if you want to set field value to a collection of terms.

Since you have TaxonomyFieldValueCollection object, you can use TaxonomyField.SetFieldValue function instead of setting item["FieldNameHere"] = "DesiredValueHere"

Like so:

TaxonomyFieldValueCollection values = new TaxonomyFieldValueCollection(string.Empty);
values.PopulateFromLabelGuidPairs(taxProjectOwnerDepartment.Text);
var field = (TaxonomyField)list.Fields.GetFieldByInternalName("Dept");
field.SetFieldValue(item, values);
  • Thank you for reply sir, I tried your suggested code but I am getting error at line field.SetFeldValue(item,values) that specified method is not supported. – users1100 Oct 4 '13 at 12:39
  • 1
    Check MSDN article for the method, it says NotSupportedException generated if you field is not multi-valued. You can use another TaxonomyFieldValue object instead of TaxonomyFieldValueCallection as parameter in the method if your field allows only one term to be set – Tim Oct 4 '13 at 12:50

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.