I have a list in which there is one column which stores the Employee Department and it is of type Taxonomy field and it is stored in the user profile property.
I want to get the display value of that field as string.
My code is:
protected void Page_Load(object sender, EventArgs e)
{
string dept = null;
SPSite site = SPContext.Current.Site;
SPWeb web = SPContext.Current.Web;
SPUser user = web.EnsureUser(web.CurrentUser.LoginName);
List<string> department = UserProfileHelper.GetUserProfilePropertyValue(site.Url,"Department",user,false);
SPQuery query = new SPQuery();
query.Query="<Where><Eq><FieldRef Name=\"Department\" /><Value Type=\"TaxonomyFieldType\">"+department[0]+
"</Value></Eq></Where>";
query.ViewFields = "<FieldRef Name='Title'/>";
SPList list = web.Lists["Departments"];
SPListItemCollection items = list.GetItems(query);
foreach (SPListItem item in items)
{
TaxonomyField taxField = item["Department"] as TaxonomyField;
dept = taxField.GetFieldValue("Department").ToString();
}
string url = web.Url + "/" + dept;
HttpContext.Current.Response.Redirect(url);
}
In my code.. GetUserProfilePropertyValue()
method returns the user profile property specified in the method.
I debugged the program and getting error at the line... TaxonomyField taxField = item["oiplbDepartment"] as TaxonomyField;
that "Value does not fall in range".
The whole thing I am applying is in Visual Web Part.
How do I read the taxonomy field value?