Many time there are some cases. when people add or updates the publishing page it requires to set some field values. In this case I have covered Taxonomy Field.

Following code will get current publishing page and set Taxonomy field Page Type to its value.

// Comment

jQuery(document).ready(function () {
var pagelayout = "";

//current context
var context = SP.ClientContext.get_current();

// current web
var web = context.get_web(); 

// get pages library of current site
var currentPageLibrary = web.get_lists().getById(_spPageContextInfo.pageListId); 

// get and load page item which is rendered
var currentPageItem = currentPageLibrary.getItemById(_spPageContextInfo.pageItemId);

 context.executeQueryAsync(function() { 

 // get pagelayoutname
   var itm = currentPageItem.get_item("PublishingPageLayout");
   if(itm != null)
  pagelayout = itm.get_description(); 
  // get id of TaxonomyField Id 
   var countriesControlId =jQuery("div[id*='Taxonomy_ctl02']").attr('id');

  // get all taxonomy from term store and set value to field   

//Set Taxonomy ControlObject value
//webTaggingId - TaxonomyWebTaggingControl ID 
//termValue - represents a single value held in a TaxonomyField object using the following format: NameOfTerm | GUIDOfTerm 

function setTaxonomyControlObjectValue(webTaggingId, termValue) {
    var webTaggingCtl = $get(webTaggingId);
    var taxCtlObj = new Microsoft.SharePoint.Taxonomy.ControlObject(webTaggingCtl);

function TaxonomyOperations(countriesControlId,pagelayoutname) {
    //Current Context
    var ctx = SP.ClientContext.get_current();
    //Current Taxonomy Session
    var taxoSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx );
    //Term Stores
    var trmStores = taxoSession.get_termStores();
    //Name of the Term Store from which to get the Terms.
    var trmStore = trmStores.getByName("Taxonomy_/2w8F+c5EEwppwXCWYn/w=="); // Taxonomy_/2w8F+c5EEwppwXCWYn/w== (Term Store ID)
    //GUID of Term Set from which to get the Terms.
    var termSet = trmStore.getTermSet("---TermStoreGUID----");
 // get all terms
    var terms = termSet.getAllTerms();
    context.executeQueryAsync(function () {
        var termEnumerator = terms.getEnumerator();
        var termList = "";
  // loop all termSet
        while (termEnumerator.moveNext()) {
          var currentTerm = termEnumerator.get_current();
          var termvalue = currentTerm.get_name();
          var termid = currentTerm.get_id();
    // Match term name and pagelayoutname
          if(termvalue == pagelayoutname)
            termList += termvalue +'|'+termid;
   // call function for setting up taxonomy controls
            setTaxonomyControlObjectValue(countriesControlId, termList);
    }, function (sender, args) {

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.