Skip to content

Instantly share code, notes, and snippets.

@alirobe
Last active March 25, 2024 14:41
Show Gist options
  • Save alirobe/220f59b486c4b0cd950d33712fa6d423 to your computer and use it in GitHub Desktop.
Save alirobe/220f59b486c4b0cd950d33712fa6d423 to your computer and use it in GitHub Desktop.
Umbraco Forms Workflow - POST to URL as JSON (with optional Bearer Access Token). Just place this file anywhere in your Umbraco+Forms project, and the dependency injection will pick it up. This will allow you to connect to Microsoft Flow or Zapier or any integration web service, from which you can send to Salesforce/Dynamics/etc.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Dynamic;
using System.Net;
using System.Text.RegularExpressions;
using Umbraco.Core.Logging;
using Umbraco.Forms.Core;
using Umbraco.Forms.Core.Attributes;
using Umbraco.Forms.Core.Enums;
public class PostToUrlAsJson : WorkflowType
{
[Setting("Url", description = "Enter the url to post to", view = "TextField")]
public string Url
{
get;
set;
}
[Setting("Bearer Token", description = "Optional: enter a bearer token for API access (don't include the word 'Bearer')", view = "TextField")]
public string BearerToken
{
get;
set;
}
public PostToUrlAsJson()
{
this.Id = new Guid("eb5d0597-4964-43c4-9437-407cba35dfc6");
this.Name = "Send form to URL as JSON object";
this.Description = "Sends the form to a url";
this.Icon = "icon-terminal";
this.Group = "Services";
}
public override List<Exception> ValidateSettings()
{
List<Exception> list = new List<Exception>();
if (string.IsNullOrEmpty(Url))
{
list.Add(new Exception("'Url' setting has not been set"));
}
return list;
}
public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
{
using (WebClient http = new WebClient())
{
http.Headers.Add(HttpRequestHeader.ContentType, "application/json");
if (!string.IsNullOrEmpty(BearerToken))
{
http.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + BearerToken);
http.Encoding = System.Text.Encoding.UTF8;
}
var values = new Dictionary<string, string>();
foreach (RecordField value in record.RecordFields.Values)
{
values.Add(value.Field.Alias, value.ValuesAsString(true));
}
dynamic json = new ExpandoObject();
json.id = record.UniqueId;
json.formId = e.Form.Id;
json.formName = e.Form.Name;
json.ip = record.IP;
json.memberKey = record.MemberKey;
json.time = DateTime.UtcNow;
json.values = values;
string payload = JsonConvert.SerializeObject(json);
try
{
http.UploadData(Url, "POST", System.Text.Encoding.UTF8.GetBytes(payload));
return WorkflowExecutionStatus.Completed;
}
catch (Exception ex)
{
string err = String.Format("There was a problem sending the Record with unique id '{0}' from the Form '{1}' with id '{2}' to the URL Endpoint '{3}' with the method 'POST'", record.UniqueId, e.Form.Name, e.Form.Id, Url);
LogHelper.Error<PostToUrlAsJson>(err, ex);
return WorkflowExecutionStatus.Failed;
}
}
}
}
@alirobe
Copy link
Author

alirobe commented Aug 24, 2021

Ensure you have included it in your VS project file, or maybe add it to the app_code folder if you're not recompiling?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment