How to retrieve the filename for email attachment (Graph Mail API)


How is it possible to retrieve the original filename of an attachment when using Graph Mail API as an image import profile?


This may be achieved by creating a new empty text field in the document definition and assigning it a value using a custom script rule, as so:

  • Create the necessary text field (Field2 in this case);
  • In this field's properties, create a new Rule and set its type to a Script rule;
  • On the Rule Settings tab disable the "Read-only" option for the field in question;
  • Review and edit (if necessary) the following script and paste it into the Script Edit window:
    using System;
    using System.Text.RegularExpressions;

    string path = Context.Document.Pages[0].ImageSource;;
    string [] parts = path.Split('\\');
    string name = parts[parts.Length - 1];

    Regex ex = new Regex(@"\""(\S*.\S)\""$", RegexOptions.Compiled);
    MatchCollection matches = ex.Matches(name);
    string resultingName = "";

    foreach(Match match in matches)
    GroupCollection groups = match.Groups;
    resultingName = groups[1].Value;

    Context.Field("Field2").Value = resultingName;
  • Save the script and the document definition;


This will provide the following result when processing documents:



Note: the provided code snippet should be treated "as-is", it may not fit all project configurations and may need alterations. 

Was this article helpful?

0 out of 0 found this helpful

Have more questions? Submit a request



Please sign in to leave a comment.

Recently viewed