Product DocsMenu

Coveo Platform 6.5 >
Developer Help > Open Converter API > Conversion Scripts > Postconversion > Samples > Sample 1 - Add New Metadata Named AllFieldValues

Sample 1 - Add New Metadata Named AllFieldValues

Postconversion Sample #1 (JScript Version)

// ****************************************************************************
//  This postconversion script sample adds a new metadata named
//  "AllFieldValues". This new metadata contains the name, the value and the
//  type of all document metadata.
// ****************************************************************************
// A double quote and a new line.
var DOUBLE_QUOTE = '"';
var NEW_LINE     = "\r\n";
// This buffer variable holds all metadata names and values.
var allValues = "<AllFieldValues>" + NEW_LINE;
// A collection of all field names.
var fieldNames = new VBArray(DocumentInfo.Fields());
// For each document field name.
for (var i = fieldNames.lbound(); i <= fieldNames.ubound(); ++i) {
  // Get the field value.
  var fieldName  = fieldNames.getItem(i);
  var fieldValue = DocumentInfo.GetFieldValue(fieldName);
  // Add the metadata name and its value to the buffer variable.
  allValues += "<Field";
  allValues += " name="  + DOUBLE_QUOTE + XmlEncode(fieldName)  + DOUBLE_QUOTE;
  allValues += " value=" + DOUBLE_QUOTE + XmlEncode(fieldValue) + DOUBLE_QUOTE;
  allValues += " type="  + DOUBLE_QUOTE + typeof(fieldValue)    + DOUBLE_QUOTE;
  allValues += " />" + NEW_LINE;
}
// Add the new metadata.
DocumentInfo.SetFieldValue("AllFieldValues", allValues + "</AllFieldValues>" + NEW_LINE);
function XmlEncode(value)
{
  var ret = "";
  if (value != null) {
    var val = value + "";
    for (var i = 0; i < val.length; ++i) {
      var ch = val.charAt(i);
      if (ch != ' ' && !(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') && !(ch >= '0' && ch <= '9')) {
        ch = "&#" + ch.charCodeAt(0) + ";";
      }
      ret += ch;
    }
  }
  return ret;
}  

Postconversion Sample #1 (VBScript Version)

'****************************************************************************
' This postconversion script sample adds a new metadata named
' "AllFieldValues". This new metadata contains the name, the value and the
' type of all document metadata.
'****************************************************************************
Option Explicit
' A double quote.
Dim DOUBLE_QUOTE: DOUBLE_QUOTE = chr(34)
' This buffer variable holds all metadata names and values.
Dim allValues: allValues = "<AllFieldValues>" & vbNewLine
' A collection of all field names.
Dim fieldNames: fieldNames = DocumentInfo.Fields
' For each document field name.
Dim fieldName
For Each fieldName In fieldNames
    ' Get the field value.
    Dim fieldValue: fieldValue = DocumentInfo.GetFieldValue(CStr(fieldName))
    ' Add the metadata name and its value to the buffer variable.
    allValues = allValues & "<Field"
    allValues = allValues & " name="  & DOUBLE_QUOTE & XmlEncode(fieldName)  & DOUBLE_QUOTE
    allValues = allValues & " value=" & DOUBLE_QUOTE & XmlEncode(fieldValue) & DOUBLE_QUOTE
    allValues = allValues & " type="  & DOUBLE_QUOTE & TypeName(fieldValue)  & DOUBLE_QUOTE
    allValues = allValues & " />" & vbNewLine
Next
' Add the new metadata.
Call DocumentInfo.SetFieldValue("AllFieldValues", allValues & "</AllFieldValues>" & vbNewLine)
Function XmlEncode(ByVal sVal)
    sVal = CStr(sVal)
    Dim sReturn: sReturn = ""
    Dim i
    Dim oRE: Set oRE = New RegExp : oRE.Pattern = "[ a-zA-Z0-9]"
    For i = 1 To Len(sVal)
        Dim ch: ch = Mid(sVal, i, 1)
        If (Not oRE.Test(ch)) Then
            ch = "&#" & Asc(ch) & ";"
        End If
        sReturn = sReturn & ch
    Next
    XmlEncode = sReturn
End Function

See also: What are Postconversion Scripts?, How to Create a Postconversion Script, How to Setup a Postconversion Script, Postconversion Script Samples