If you would like to use the built-in taxonomy controls of SharePoint 2010 your best choice might be to go with the TaxonomyWebTaggingControl. This is the class the field control of the TaxonomyField, TaxonomyFieldControl itself uses internally to render its content, although it does not expose the full capability of the control.
In this post I will show you some features of the TaxonomyWebTaggingControl using it in a visual web part project.
First, let’s see how to use it declaratively. Be sure Microsoft.SharePoint.Taxonomy is registered in your user control (.ASCX) file:
<%@ Register Tagprefix="Taxonomy" Namespace="Microsoft.SharePoint.Taxonomy" Assembly="Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
Having this registration, you can add TaxonomyWebTaggingControl using the following declaration:
<Taxonomy:TaxonomyWebTaggingControl AllowFillIn="true" IsMulti="true" SSPList="{29ff8c4f-37a6-4d42-8cef-f610f0a7c7e9}" TermSetList="{a22cf1f5-427e-41d7-8af4-7aa4dc47197d}" AnchorId="{ec5ee636-f35c-43bd-92a0-56016a0a127c}" runat="server" />
This control instance allows multiple items and filling in values. The SSPList attribute is to specify the Id of the term store(s) and the TermSetList attribute is to specify the Id of the term set(s). Note, that none of these two attributes are automatically suggested by IntelliSense or recognized by the compiler in Visual Studio 2010 beta 2, but they works as expected. You can specify multiple values in both of these attributes, separating them by semicolons (‘;’). It is a great way to combine multiple term sets even from multiple term stores. The AnchorId attribute sets the Id of the term that serves as the starting point of the term hierarchy used in the control. Only terms below the anchor will be suggested in the control.
Although it is not so hard to get these Ids using a little command line utility, specifying these settings by Guid values is not user friendly. So let’s switch to the coding approach, but before doing that, we add some additional content to the user control:
<Taxonomy:TaxonomyWebTaggingControl ID="TestTaxonomyControl" runat="server" />
<asp:Button ID="SendButton" runat="server" Text="Send"
onclick="SendButton_Click" />
<br />
<br />
<asp:Label ID="MetaDataLabel" runat="server" Text=""></asp:Label>
Having this done, we can alter to the code behind of the user control to the following:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint;
using System.Drawing;
namespace VisualWebPartTest1.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SPContext context = SPContext.Current;
SPSite site = context.Site;
TaxonomySession session = new TaxonomySession(site);
TermStore termStore = session.TermStores["MyTermStore"];
Group group = termStore.Groups["MyGroup"];
TermSet productsTermSet = group.TermSets["Products"];
TermSet languagesTermSet = group.TermSets["Languages"];
TestTaxonomyControl.SspId.Add(termStore.Id);
TestTaxonomyControl.TermSetId.Add(productsTermSet.Id);
TestTaxonomyControl.TermSetId.Add(languagesTermSet.Id);
TestTaxonomyControl.AllowFillIn = true;
TestTaxonomyControl.IsMulti = true;
}
protected void SendButton_Click(object sender, EventArgs e)
{
String validationMessage;
bool valid = TestTaxonomyControl.Validate(out validationMessage);
if (valid)
{
MetaDataLabel.Text = String.Empty;
MetaDataLabel.ForeColor = Color.Black;
TaxonomyFieldValueCollection values = new TaxonomyFieldValueCollection(String.Empty);
values.PopulateFromLabelGuidPairs(TestTaxonomyControl.Text);
foreach (TaxonomyFieldValue value in values)
{
MetaDataLabel.Text += value.WssId + " / " + value.TermGuid + " / " + value.Label + "<br />";
}
}
else
{
MetaDataLabel.ForeColor = Color.Red;
MetaDataLabel.Text = validationMessage;
}
}
}
}
As you can see we use the SspId and TermSetId properties of the TaxonomyWebTaggingControl control to specify the term store and term sets. Both of this properties are of type List<Guid>. We specify two term sets for this control, one for the Languages and another one for Products.
Just a thought regarding the figure above. It seems that pressing Tab button to autocomplete the suggested value only works in the case of the first tag in taxonomy controls. I hope it is only a bug in beta 2 and will be corrected in the final version.
We handle the Click event of the SendButton control to validate the values specified in our taxonomy control. We can use the Validate method of the TaxonomyWebTaggingControl control for that. This method returns with a boolean value that represents the result of the validation, and an out String parameter that is the validation error message.
If the validation fails we display the error message. Note that the text box is empty now, since we don’t set its value on post back.
BTW, if you want the control to be able to add new terms:
There should be only a single term set attached to the control.
The term set must be open.
You should set the IsAddTerms property to true.
On success we display the selection values. The Text property of the TaxonomyWebTaggingControl control contains the label – Id pairs of the selected terms. To convert it to TaxonomyFieldValueCollection, we should use the PopulateFromLabelGuidPairs method.
Note, that the WssId is –1 for both values, since in this case the control is not bound to a site, not like in the case of a TaxonomyField (see my former post about the WssId).
Source: https://pholpar.wordpress.com/2010/02/15/build-your-own-user-interface-components-using-the-taxonomy-controls/
In this post I will show you some features of the TaxonomyWebTaggingControl using it in a visual web part project.
First, let’s see how to use it declaratively. Be sure Microsoft.SharePoint.Taxonomy is registered in your user control (.ASCX) file:
<%@ Register Tagprefix="Taxonomy" Namespace="Microsoft.SharePoint.Taxonomy" Assembly="Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
Having this registration, you can add TaxonomyWebTaggingControl using the following declaration:
<Taxonomy:TaxonomyWebTaggingControl AllowFillIn="true" IsMulti="true" SSPList="{29ff8c4f-37a6-4d42-8cef-f610f0a7c7e9}" TermSetList="{a22cf1f5-427e-41d7-8af4-7aa4dc47197d}" AnchorId="{ec5ee636-f35c-43bd-92a0-56016a0a127c}" runat="server" />
This control instance allows multiple items and filling in values. The SSPList attribute is to specify the Id of the term store(s) and the TermSetList attribute is to specify the Id of the term set(s). Note, that none of these two attributes are automatically suggested by IntelliSense or recognized by the compiler in Visual Studio 2010 beta 2, but they works as expected. You can specify multiple values in both of these attributes, separating them by semicolons (‘;’). It is a great way to combine multiple term sets even from multiple term stores. The AnchorId attribute sets the Id of the term that serves as the starting point of the term hierarchy used in the control. Only terms below the anchor will be suggested in the control.
Although it is not so hard to get these Ids using a little command line utility, specifying these settings by Guid values is not user friendly. So let’s switch to the coding approach, but before doing that, we add some additional content to the user control:
<Taxonomy:TaxonomyWebTaggingControl ID="TestTaxonomyControl" runat="server" />
<asp:Button ID="SendButton" runat="server" Text="Send"
onclick="SendButton_Click" />
<br />
<br />
<asp:Label ID="MetaDataLabel" runat="server" Text=""></asp:Label>
Having this done, we can alter to the code behind of the user control to the following:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint;
using System.Drawing;
namespace VisualWebPartTest1.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SPContext context = SPContext.Current;
SPSite site = context.Site;
TaxonomySession session = new TaxonomySession(site);
TermStore termStore = session.TermStores["MyTermStore"];
Group group = termStore.Groups["MyGroup"];
TermSet productsTermSet = group.TermSets["Products"];
TermSet languagesTermSet = group.TermSets["Languages"];
TestTaxonomyControl.SspId.Add(termStore.Id);
TestTaxonomyControl.TermSetId.Add(productsTermSet.Id);
TestTaxonomyControl.TermSetId.Add(languagesTermSet.Id);
TestTaxonomyControl.AllowFillIn = true;
TestTaxonomyControl.IsMulti = true;
}
protected void SendButton_Click(object sender, EventArgs e)
{
String validationMessage;
bool valid = TestTaxonomyControl.Validate(out validationMessage);
if (valid)
{
MetaDataLabel.Text = String.Empty;
MetaDataLabel.ForeColor = Color.Black;
TaxonomyFieldValueCollection values = new TaxonomyFieldValueCollection(String.Empty);
values.PopulateFromLabelGuidPairs(TestTaxonomyControl.Text);
foreach (TaxonomyFieldValue value in values)
{
MetaDataLabel.Text += value.WssId + " / " + value.TermGuid + " / " + value.Label + "<br />";
}
}
else
{
MetaDataLabel.ForeColor = Color.Red;
MetaDataLabel.Text = validationMessage;
}
}
}
}
As you can see we use the SspId and TermSetId properties of the TaxonomyWebTaggingControl control to specify the term store and term sets. Both of this properties are of type List<Guid>. We specify two term sets for this control, one for the Languages and another one for Products.
Just a thought regarding the figure above. It seems that pressing Tab button to autocomplete the suggested value only works in the case of the first tag in taxonomy controls. I hope it is only a bug in beta 2 and will be corrected in the final version.
We handle the Click event of the SendButton control to validate the values specified in our taxonomy control. We can use the Validate method of the TaxonomyWebTaggingControl control for that. This method returns with a boolean value that represents the result of the validation, and an out String parameter that is the validation error message.
If the validation fails we display the error message. Note that the text box is empty now, since we don’t set its value on post back.
BTW, if you want the control to be able to add new terms:
There should be only a single term set attached to the control.
The term set must be open.
You should set the IsAddTerms property to true.
On success we display the selection values. The Text property of the TaxonomyWebTaggingControl control contains the label – Id pairs of the selected terms. To convert it to TaxonomyFieldValueCollection, we should use the PopulateFromLabelGuidPairs method.
Note, that the WssId is –1 for both values, since in this case the control is not bound to a site, not like in the case of a TaxonomyField (see my former post about the WssId).
Source: https://pholpar.wordpress.com/2010/02/15/build-your-own-user-interface-components-using-the-taxonomy-controls/
No comments:
Post a Comment