FieldGetFieldCode Method
Returns text between field start and field separator (or field end if there is no separator). Both field code and field result of child fields are included.

Namespace: Aspose.Words.Fields
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 19.12
Syntax
public string GetFieldCode()

Return Value

Type: String
Examples
Inserts a field into a document using DocumentBuilder and FieldCode.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a simple Date field into the document.
// When we insert a field through the DocumentBuilder class we can get the
// special Field object which contains information about the field.
Field dateField = builder.InsertField(@"DATE \* MERGEFORMAT");

// Update this particular field in the document so we can get the FieldResult.
dateField.Update();

// Display some information from this field.
// The field result is where the last evaluated value is stored. This is what is displayed in the document
// When field codes are not showing.
Console.WriteLine("FieldResult: {0}", dateField.Result);

// Display the field code which defines the behavior of the field. This can been seen in Microsoft Word by pressing ALT+F9.
Console.WriteLine("FieldCode: {0}", dateField.GetFieldCode());

// The field type defines what type of field in the Document this is. In this case the type is "FieldDate" 
Console.WriteLine("FieldType: {0}", dateField.Type);

// Finally let's completely remove the field from the document. This can easily be done by invoking the Remove method on the object.
dateField.Remove();
Examples
Shows how to get text between field start and field separator (or field end if there is no separator)
Document doc = new Document(MyDir + "Field.FieldCode.docx");

foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldIf)
    {
        FieldIf fieldIf = (FieldIf)field;

        string fieldCode = fieldIf.GetFieldCode();
        Assert.AreEqual(
            " IF " + ControlChar.FieldStartChar + " MERGEFIELD Q223 " + ControlChar.FieldSeparatorChar + ControlChar.FieldEndChar + " > 0 \" (and additionally London Weighting of  " + ControlChar.FieldStartChar + " MERGEFIELD  Q223 \\f £ " + ControlChar.FieldSeparatorChar + ControlChar.FieldEndChar + " per hour) \" \"\" ",

        if (containsNestedFields)
        {
            fieldCode = fieldIf.GetFieldCode(true);
            Assert.AreEqual(
                " IF " + ControlChar.FieldStartChar + " MERGEFIELD Q223 " + ControlChar.FieldSeparatorChar + ControlChar.FieldEndChar + " > 0 \" (and additionally London Weighting of  " + ControlChar.FieldStartChar + " MERGEFIELD  Q223 \\f £ " + ControlChar.FieldSeparatorChar + ControlChar.FieldEndChar + " per hour) \" \"\" ",
        }
        else
        {
            fieldCode = fieldIf.GetFieldCode(false);
            Assert.AreEqual(" IF  > 0 \" (and additionally London Weighting of   per hour) \" \"\" ",
        }
    }
}
See Also