Skip to content

Instantly share code, notes, and snippets.

@nul800sebastiaan
Created June 2, 2016 09:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nul800sebastiaan/ce94b1b4d1bd0bf1fecb1ea82341d4c1 to your computer and use it in GitHub Desktop.
Save nul800sebastiaan/ce94b1b4d1bd0bf1fecb1ea82341d4c1 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Umbraco.Forms.Core.Enums;
using System.Net;
using System.Collections.Specialized;
using Umbraco.Core.Logging;
using Umbraco.Forms.Core.Providers.Models;
namespace Umbraco.Forms.Core.Providers.WorkflowTypes {
public class PostToUrl : WorkflowType {
[Attributes.Setting("Url", description= "Enter the url to post to", view = "TextField")]
public string Url { get; set; }
[Attributes.Setting("Method", description = "POST or GET", prevalues = "POST,GET,PUT,DELETE", view = "Dropdownlist")]
public string Method { get; set; }
[Attributes.Setting("Fields", description = "Map the needed fields (if not mapped it will use all fields)", view = "FieldMapper")]
public string Fields { get; set; }
[Attributes.Setting("User", description = "(optional)", view = "TextField")]
public string Username { get; set; }
[Attributes.Setting("Password", description = "(optional)", view = "Password")]
public string Password { get; set; }
public PostToUrl() {
this.Id = new Guid("FD02C929-4E7D-4f90-B9FA-13D074A76688");
this.Name = "Send form to URL";
this.Description = "Sends the form to a url, either as a HTTP POST or GET";
this.Icon = "icon-terminal";
this.Group = "Legacy";
}
public override List<Exception> ValidateSettings() {
List<Exception> l = new List<Exception>();
if (string.IsNullOrEmpty(Url))
{
l.Add(new Exception("'Url' setting has not been set"));
}
if (string.IsNullOrEmpty(Method))
{
l.Add(new Exception("'Method' setting has not been set"));
}
return l;
}
public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
{
var mappings = new List<FieldMapping>();
if (string.IsNullOrEmpty(Fields) == false)
{
mappings = JsonConvert.DeserializeObject<IEnumerable<FieldMapping>>(Fields).ToList();
}
using (var client = new WebClient())
{
var values = new NameValueCollection();
if (mappings.Any())
{
foreach (var fieldMapping in mappings)
{
string value = "";
string alias = fieldMapping.Alias;
if (string.IsNullOrEmpty(fieldMapping.StaticValue) == false)
{
value = fieldMapping.StaticValue;
}
else
{
var rf = record.RecordFields[new Guid(fieldMapping.Value)];
value = rf.ValuesAsString(false);
}
values.Add(alias, value);
}
}
else
{
foreach (RecordField rf in record.RecordFields.Values)
{
values.Add(rf.Field.Caption.Replace(" ", ""), rf.ValuesAsString());
}
}
//Check to see if a username & password set
//Use as Basic Auth in a POST
if (string.IsNullOrEmpty(Password) == false && string.IsNullOrEmpty(Username) == false)
{
client.Credentials = new NetworkCredential(Username, Password);
}
try
{
//GET, POST, PUT, DELETE the form fields to the URL
client.UploadValues(Url, Method, values);
return WorkflowExecutionStatus.Completed;
}
catch (Exception ex)
{
var message = 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 '{4}'", record.UniqueId, e.Form.Name, e.Form.Id, this.Url, this.Method);
LogHelper.Error<PostToUrl>(message, ex);
return WorkflowExecutionStatus.Failed;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment