MappedDataFieldCollectionContainsKey Method
Determines whether a mapping from the specified field in the document exists in the collection.

Namespace: Aspose.Words.MailMerging
Assembly: Aspose.Words (in Aspose.Words.dll) Version: 20.1
Syntax
public bool ContainsKey(
	string documentFieldName
)

Parameters

documentFieldName
Type: SystemString
Case-sensitive name of the mail merge field in the document.

Return Value

Type: Boolean
True if item is found in the collection; otherwise, false.
Examples
Shows how to map data columns and MERGEFIELDs with different names so the data is transferred between them during a mail merge.
public void MappedDataFieldCollection()
{
    // Create a document and table that we will merge
    Document doc = CreateSourceDocMappedDataFields();
    DataTable dataTable = CreateSourceTableMappedDataFields();

    // We have a column "Column2" in the data table that doesn't have a respective MERGEFIELD in the document
    // Also, we have a MERGEFIELD named "Column3" that does not exist as a column in the data source
    // If data from "Column2" is suitable for the "Column3" MERGEFIELD,
    // we can map that column name to the MERGEFIELD in the "MappedDataFields" key/value pair
    MappedDataFieldCollection mappedDataFields = doc.MailMerge.MappedDataFields;

    // A data source column name is linked to a MERGEFIELD name by adding an element like this
    mappedDataFields.Add("MergeFieldName", "DataSourceColumnName");

    // So, values from "Column2" will now go into MERGEFIELDs named "Column3" as well as "Column2", if there are any
    mappedDataFields.Add("Column3", "Column2");

    // The MERGEFIELD name is the "key" to the respective data source column name "value"
    Assert.AreEqual("DataSourceColumnName", mappedDataFields["MergeFieldName"]);
    Assert.True(mappedDataFields.ContainsKey("MergeFieldName"));
    Assert.True(mappedDataFields.ContainsValue("DataSourceColumnName"));

    // Now if we run this mail merge, the "Column3" MERGEFIELDs will take data from "Column2" of the table
    doc.MailMerge.Execute(dataTable);

    // We can count and iterate over the mapped columns/fields
    Assert.AreEqual(2, mappedDataFields.Count);

    using (IEnumerator<KeyValuePair<string, string>> enumerator = mappedDataFields.GetEnumerator())
        while (enumerator.MoveNext())
            Console.WriteLine(
                $"Column named {enumerator.Current.Value} is mapped to MERGEFIELDs named {enumerator.Current.Key}");

    // We can also remove some or all of the elements
    mappedDataFields.Remove("MergeFieldName");
    Assert.False(mappedDataFields.ContainsKey("MergeFieldName"));
    Assert.False(mappedDataFields.ContainsValue("DataSourceColumnName"));

    mappedDataFields.Clear();
    Assert.AreEqual(0, mappedDataFields.Count);

    // Removing the mapped key/value pairs has no effect on the document because the merge was already done with them in place
    doc.Save(ArtifactsDir + "MailMerge.MappedDataFieldCollection.docx");
}

/// <summary>
/// Create a document with 2 MERGEFIELDs, one of which does not have a corresponding column in the data table.
/// </summary>
private static Document CreateSourceDocMappedDataFields()
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert two MERGEFIELDs that will accept data from that table
    builder.InsertField(" MERGEFIELD Column1");
    builder.Write(", ");
    builder.InsertField(" MERGEFIELD Column3");

    return doc;
}

/// <summary>
/// Create a data table with 2 columns, one of which does not have a corresponding MERGEFIELD in our source document.
/// </summary>
private static DataTable CreateSourceTableMappedDataFields()
{
    // Create a data table that will be used in a mail merge
    DataTable dataTable = new DataTable("MyTable");
    dataTable.Columns.Add("Column1");
    dataTable.Columns.Add("Column2");
    dataTable.Rows.Add(new object[] { "Value1", "Value2" });

    return dataTable;
}
See Also