Friday, August 26, 2011
Using TaxonomyWebTaggingControl Programatically
To utilize the taxonomy control, below namespace must be declared
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
Step: 1
Declare taxonomy control Page_Init()
TaxonomyWebTaggingControl _TaxonomyWebTaggingControl = new TaxonomyWebTaggingControl;
_TaxonomyWebTaggingControl.ID = "taxctrl";
_TaxonomyWebTaggingControl.IsMulti = true;
_TaxonomyWebTaggingControl.AllowFillIn = true;
_TaxonomyWebTaggingControl.IsUseCommaAsDelimier = true;
this.form.controls.add(_TaxonomyWebTaggingControl);
//Create taxonomy Session, term store, term set & group to pull metadata from Term Store Management
TaxonomySession _TaxonomySession = new TaxonomySession(SPContext.Current.Site);
TermStore _TermStore = _TaxonomySession.TermStores["Managed Metadata Service"];
Group _Group = _TermStore.Groups["System"];
TermSet _TermSet = _Group.TermSets["Keywords"];
//Assign the Term Store & Term Set to Taxonomy Control which helps to provide suggestions or pick metadata while you type in run time.
_TaxonomyWebTaggingControl.SspId.Add(_TermStore.Id);
_TaxonomyWebTaggingControl.TermSetId.Add(_TermSet.Id);
Step: 2
write the below code in Page_Load() in if(!IsPostBack) block
Below code fetches Metadata from userprofile property & assign to taxonomy field
//Fetching the Taxonomy metadata from SharePoint userprofile property
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;
private string _CurrentUser = SPContext.Web.CurrentUser.ToString();
SPServiceContext _SPServiceContext = new SPServiceContext .Current;
UserProfileManager _UserProfileManager = new UserProfileManager(_SPServiceContext);
UserProfile _UserProfile = _UserProfileManager.GetUserProfile(_CurrentUser);
//Get the control using findcontrol() by Id
TaxonomyWebTaggingControl _TaxonomyWebTaggingControl = (TaxonomyWebTaggingControl) this.form.findcontrol("taxctrl");
//Again we are getting taxonomy Session, term store, term set to create Taxonomy key value pairs(Term|GUID;)
TaxonomySession _TaxonomySession = new TaxonomySession(SPContext.Current.Site);
TermStore _TermStore = _TaxonomySession.TermStores["Managed Metadata Service"];
Group _Group = _TermStore.Groups["System"];
TermSet _TermSet = _Group.TermSets["Keywords"];
//Fetching Multi-value userprofile property "SPS-PastProjects" - which is Taxonomy field
UserProfileValueCollection _UserProfileValueCollection = _UserProfile["SPS-PastProjets"];
for(int i = 0; i < _UserProfilevalueCollection.Count; i++)
{
_TaxonomyWebTaggingControl.Text = String.Contact(_TaxonomyWebTaggingControl.Text, String.Format("{0}|{1};", _UserProfileValueCollection[i].ToString(), _TermSet[_UserProfileValueCollection[i].ToString()].Id.ToString()));
}
Step: 3
To get back the value from TaxonomyWebTaggingControl & update the userprofile property
Write the below code in Save Event
//Get the control using findcontrol() by Id
TaxonomyWebTaggingControl _TaxonomyWebTaggingControl = (TaxonomyWebTaggingControl) this.form.findcontrol("taxctrl");
TaxonomyFieldValueCollection _TaxonomyFieldValueCollection = new TaxonomyFieldValueCollection(String.Empty);
TaxonomyFieldValueCollection.PopulateFromLabelGuidPairs(_TaxonomyWebTaggingControl.Text);
for(int i = 0; i < _TaxonomyFieldValueCollection; i ++)
{
_UserProfile["SPS-PastProjects"].AddTaxonomyTerm(_TermSet.Terms[_TaxonomyFieldValueCollection[i].Label].SourceTerm);
}
Subscribe to:
Post Comments (Atom)
good information! I've been looking for this for a while but didn't know the name of the control to search on so it took me till now. THANKS! :)
ReplyDeleteBTW, not to complain, but your site is almost impossible to read, the code is mashed together with the text and there is no real formatting.