IDocumentAddedHandler

The IDocumentAddedHandler is used in conjunction with the IDocumentHandler interface, to further specify that the handler should be executed before and after documents are added.

Interface declaration

public interface IDocumentAddedHandler
{
	/// <summary>
	/// Is executed *before* a document is added
	/// </summary>
	/// <param name="document">The document which is going to be added</param>
	Task OnAddingAsync(Document document);

	/// <summary>
	/// Is executed *after* a document has been added
	/// </summary>
	/// <param name="document">The document which was just added</param>
	Task OnAddedAsync(Document document);
}

Example

This example implements a Microsoft Teams handler, which posts information about the added document to a Teams channel.  It uses the "Incoming webhook" connector in Teams, which provides a Webhook URL which this handler posts to.

[DisplayName("Post to Microsoft Teams")]
[Description("Posts added documents to a specified MS Teams channel")]
public class PostToTeamsChannel : IDocumentHandler, IDocumentAddedHandler
{
	[EditorBrowsable]
	[DisplayName("Webhook URL")]
	[Description("Configure the Incoming Webhook connector for the channel you wish to post to.  Paste the webhook URL here.")]
	public string WebhookUrl { get; set; }

	[EditorBrowsable]
	[DisplayName("Message body")]
	[Description("Specify the body of the message.  Use the databinding syntax ({fieldname}) to extract data from the document.")]
	[Editor(PropertyEditors.TextEditor, "")]
	public string MessageBody { get; set; }

	public async Task OnAddedAsync(Document document)
	{
		if (!string.IsNullOrEmpty(WebhookUrl))
		{
			DataBindingContext context = new DataBindingContext(System.Globalization.CultureInfo.CurrentCulture);
			context.AddItem("_Created", document.Created);
			context.AddItem("_DisplayFrom", document.DisplayFrom);
			context.AddItem("_DisplayTo", document.DisplayTo);
			context.AddItem("_Tags", document.TagList);
			context.AddItem("_OwnerId", document.OwnerId);
			context.AddItem("_OwnerName", document.OwnerName);
			context.AddItem("DocumentId", document.Id);
			context.AddItem("UId", document.UID);
			context.AddItem("_OrderIndex", document.OrderIndex);
			context.AddItems(document.FieldValues);

			var messageBody = context.FormatString(MessageBody);

			using (var httpClient = new HttpClient())
			{
				using (var request = new HttpRequestMessage(new HttpMethod("POST"), WebhookUrl))
				{
					request.Content = new StringContent(JsonConvert.SerializeObject(new { text = messageBody }));
					request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
					var response = await httpClient.SendAsync(request);
				}
			}
		}
	}

	public async Task OnAddingAsync(Document document)
	{			
	}
}

Registration

This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).

public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
	services.AddNamed<IDocumentHandler, PostToTeamsChannel>(ServiceLifetime.Transient);
}