'Get all forms stored in hubspot as a Dictionary<Guid, object>()
Is it somehow via the Hubspot form api to get all forms in a particular format.
According to the documentation and the preview what it seem to be returning is a list of json objects.
https://legacydocs.hubspot.com/docs/methods/forms/v2/get_forms
Is it somehow possible to alter this format in someway via the api?
I would very likely have it returned as a dictionary, where the key of each entry is the guid, and value is the form itself.
The only way I see its possible is by postprocessing the received input, and then map it into a dictionary, but that seems a bit redundant if it was possible via the api?
Solution 1:[1]
Using NewtonSoft's JsonConvert and JsonPropertyAttributes to deserialize the JSON:
Create classes with the properties you need using the JsonPropertyAttribute... something like this:
public class FormObj
{
[JsonProperty(PropertyName = "portalId")]
public int PortalId { get; set; }
[JsonProperty(PropertyName = "guid")]
public Guid Guid { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "action")]
public string Action { get; set; }
[JsonProperty(PropertyName = "method")]
public string Method { get; set; }
[JsonProperty(PropertyName = "cssClass")]
public string CssClass { get; set; }
[JsonProperty(PropertyName = "redirect")]
public string Redirect { get; set; }
[JsonProperty(PropertyName = "submitText")]
public string SubmitText { get; set; }
[JsonProperty(PropertyName = "followUpId")]
public string FollowUpId { get; set; }
[JsonProperty(PropertyName = "notifyRecipients")]
public string NotifyRecipients { get; set; }
[JsonProperty(PropertyName = "leadNurturingCampaignId")]
public string LeadNurturingCampaignId { get; set; }
[JsonProperty(PropertyName = "createdAt")]
public long CreatedAt { get; set; }
[JsonProperty(PropertyName = "updatedAt")]
public long UpdatedAt { get; set; }
[JsonProperty(PropertyName = "performableHtml")]
public string PerformableHtml { get; set; }
[JsonProperty(PropertyName = "migratedFrom")]
public string MigratedFrom { get; set; }
[JsonProperty(PropertyName = "ignoreCurrentValues")]
public bool IgnoreCurrentValues { get; set; }
[JsonProperty(PropertyName = "formFieldGroups")]
public FormFieldGroup[] FormFieldGroups { get; set; }
[JsonProperty(PropertyName = "metaData")]
public MetaData[] MetaData { get; set; }
[JsonProperty(PropertyName = "deletable")]
public bool IsDeletable { get; set; }
[JsonProperty(PropertyName = "inlineMessage")]
public string InlineMessage { get; set; }
[JsonProperty(PropertyName = "tmsId")]
public string? tmsId { get; set; }
[JsonProperty(PropertyName = "captchaEnabled")]
public bool IsCaptchaEnabled { get; set; }
[JsonProperty(PropertyName = "campaignGuid")]
public string CampaignGuid { get; set; }
[JsonProperty(PropertyName = "cloneable")]
public bool IsCloneable { get; set; }
[JsonProperty(PropertyName = "editable")]
public bool IsEditable { get; set; }
[JsonProperty(PropertyName = "formType")]
public string FormType { get; set; }
}
public class FormFieldGroup
{
[JsonProperty(PropertyName = "fields")]
public IEnumerable<Field> Fields { get; set; }
// ...
}
public class Field
{
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "label")]
public string DisplayName { get; set; }
// ...
}
public class MetaData
{
// ...
}
The FormFieldGroup, Field and MetaData objects aren't complete, hopefully you can extrapolate the needed properties from the examples. You can name your properties as you would like, you only need to the JsonPropertyAttribute's PropertyName to match the property in the JSON. You also do not have to have every property in the JSON in your objects, you have full control.
It is fairly straight forward to deserialize into an IEnumerable of your FormObj and then convert that to your dictionary.
var formDictionary = JsonConvert.DeserializeObject<IEnumerable<FormObj>>(JSON).ToDictionary(v => v.Guid);
With all that said, creating your own client might look something like this:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace YourNamespaceHere
{
public static class HubSpotClient
{
private static Uri baseUri = new Uri(@"https://api.hubapi.com");
public static async Task<IDictionary<Guid, FormObj>> GetForms()
{
var forms = await Get<IEnumerable<FormObj>>(@"/forms/v2/forms?hapikey=demo");
return forms.ToDictionary(f => f.Guid);
}
private static async Task<T> Get<T>(string relativePath)
{
var json = await CallHubspot(relativePath);
return JsonConvert.DeserializeObject<T>(json);
}
private static async Task<string> CallHubspot(string relativePath)
{
using (var client = new HttpClient())
{
client.BaseAddress = baseUri;
try
{
HttpResponseMessage response = await client.GetAsync(relativePath);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
catch (HttpRequestException e)
{
// add logging or better error handling
throw;
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Larry Dukek |
