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?