Wednesday, November 7, 2012

Sync Taxonomy hiddenlist(termstore values)

If you are using taxonomy fields in spSite then you will get a prolem while updating the taxonomy value from centeral admin then updated taxonomy value  won't sync with new value in list items, Even after run Taxonomy Scheduler job. For this we have write the sptimerjob with the following code(Or write a webpart),it should be run under administrative previlages, any wat timer job will run under administrator.
 Namespace required:
Using Microsoft.SharePoint;
Using Microsoft.SharePoint.Taxonomy;

            // Resync the taxonomy hidden list to make sure it is update-to-date
            TaxonomySession.SyncHiddenList(site);


Example webpart souce code:
protected void Button1_Click(object sender, EventArgs e)
{
bool status;
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb rootWeb = site.RootWeb)
{
status = rootWeb.AllowUnsafeUpdates;

rootWeb.AllowUnsafeUpdates =
true;
try
{
SPUser theUser = SPContext.Current.Web.CurrentUser;

if (theUser.IsSiteAdmin)

{
// sync the taxonomy hidden list to make sure it is update-to-date
TaxonomySession.SyncHiddenList(site);

Controls.Add(new LiteralControl("Success"));

}
else
{

Controls.Add(
new LiteralControl("Current loged in user donot have permissions."));

}
}
catch (Exception ex)
{
Controls.Add(
new LiteralControl("Failure: " + ex.ToString()));

}
finally
{
rootWeb.AllowUnsafeUpdates = status;
}

}

}

}


Thanks & Regards,
MTR
SharePoint Deeloper.