Tuesday, December 1, 2015

Copy taxonomy field values using javascript object model in Sharepoint

Suppose that we have file in source list and we need to copy this file with metadata to target list. Source and target lists may be located on different site collections. In this post I will show how to copy metadata form source file to target using javascript object model. This approach may be useful when you need to perform similar task in Sharepoint Online.

Let’s assume that we need to copy 3 taxonomy fields: Region, Country and Language. It can be done using the following code:

   1: var ctx = ...
   2: var sourceItems = ...
   3: var targetList = ...
   4: var targetItem = ...
   5:  
   6: var enumerator = sourceItems.getEnumerator();
   7: enumerator.moveNext();
   8: var sourceItem = enumerator.get_current();
   9:  
  10: var fieldRegion = targetList.get_fields().getByInternalNameOrTitle("Region");
  11: var fieldRegionTax = ctx.castTo(fieldRegion, SP.Taxonomy.TaxonomyField);
  12: ctx.load(fieldRegionTax);
  13:  
  14: var fieldCountry = targetList.get_fields().getByInternalNameOrTitle("Country");
  15: var fieldCountryTax = ctx.castTo(fieldCountry, SP.Taxonomy.TaxonomyField);
  16: ctx.load(fieldCountryTax);
  17:  
  18: var fieldLanguage = targetList.get_fields().getByInternalNameOrTitle("Language");
  19: var fieldLanguageTax = ctx.castTo(fieldLanguage, SP.Taxonomy.TaxonomyField);
  20: ctx.load(fieldLanguageTax);
  21:  
  22: ctx.executeQueryAsync(
  23:     Function.createDelegate(this, function (sender, args) {
  24:         getTaxonomyFieldsSuccess(ctx, sourceItem, targetItem, fieldRegionTax,
  25:     fieldCountryTax, fieldLanguageTax); }),
  26:     Function.createDelegate(this, function (sender, args) {
  27:         console.log("Load taxonomy fields failed: " + args.get_message() + "\n" +
  28:             args.get_stackTrace()); }));
  29:  
  30: function getTaxonomyFieldsSuccess(ctx, sourceItem, targetItem,
  31:     fieldRegionTax, fieldCountryTax, fieldLanguageTax) {
  32:     if (sourceItem.get_item("Region") != null) {
  33:         var targetValue = new SP.Taxonomy.TaxonomyFieldValue();
  34:         targetValue.set_label(sourceItem.get_item("Region").get_label());
  35:         targetValue.set_termGuid(sourceItem.get_item("Region").get_termGuid());
  36:         targetValue.set_wssId(sourceItem.get_item("Region").get_wssId());
  37:         fieldRegionTax.setFieldValueByValue(targetItem, targetValue);
  38:     }
  39:  
  40:     if (sourceItem.get_item("Country") != null) {
  41:         var targetValue = new SP.Taxonomy.TaxonomyFieldValue();
  42:         targetValue.set_label(sourceItem.get_item("Country").get_label());
  43:         targetValue.set_termGuid(sourceItem.get_item("Country").get_termGuid());
  44:         targetValue.set_wssId(sourceItem.get_item("Country").get_wssId());
  45:         fieldCountryTax.setFieldValueByValue(targetItem, targetValue);
  46:     }
  47:  
  48:     if (sourceItem.get_item("Language") != null) {
  49:         var targetValue = new SP.Taxonomy.TaxonomyFieldValue();
  50:         targetValue.set_label(sourceItem.get_item("Language").get_label());
  51:         targetValue.set_termGuid(sourceItem.get_item("Language").get_termGuid());
  52:         targetValue.set_wssId(sourceItem.get_item("Language").get_wssId());
  53:         fieldLanguageTax.setFieldValueByValue(targetItem, targetValue);
  54:     }
  55:  
  56:     targetItem.update();
  57:     ctx.executeQueryAsync(
  58:         Function.createDelegate(this, function (sender, args) {
  59:             console.log("Metadata is copied successfully");
  60:         }),
  61:         Function.createDelegate(this, function (sender, args) {
  62:             console.log("Copying metadata failed: " + args.get_message() + "\n" +
  63:                 args.get_stackTrace());
  64:         }));
  65: }

At first we load taxonomy fields from the target list (lines 10-21). Then having these fields set their value using label, term guid and wssId properties of the source field value (lines 32-54).

No comments:

Post a Comment