Create and provision Plumsail public web forms programmatically

It is possible to create forms from scratch and then provision them programmatically using Plumsail.Forms.Public NuGet package.

Forms can also be edited, as well as deleted using the same NuGet package.

Important

Functionality of this method is limited to web editor’s functionality. Only web editor’s forms can be edited. Forms created this way will also be editable only in web editor.

In order to use the package, your project must be compatible with .NET Standard 2.0

Find an example of how it can be used in our article - Create a contact web form in Visual Studio.

Client

Constructor

Description/Examples

FormsClient(string login, string password);

FormsClient allows you to connect to your account to start working with its forms.

login - your Plumsail Account login

password - your Plumsail Account password


Example:

var client = new FormsClient("login", "password");

FormsClient(HttpClient httpClient);

An alternative constructor of the FormsClient that uses HttpClient instead.

httpClient - HttpClient that you can configure


Example:

using System.Net.Http;
using System.Net.Http.Headers;

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "<token>");
var client = new FormsClient(httpClient);

NewForm(string name)

A method of the FormsClient to create a new public web form with the specified name

name - name of the form


Example:

var form = client.NewForm("Form name");

GetForms()

Async method to get all forms from the account


Example:

// get forms list
var forms = await client.GetForms();

GetForm(string formId)

Async method to get a specific form from the account

formId - ID of the form


Example:

// get forms list
var forms = await client.GetForms();
//get the ID of the first form
var formId = forms.First().Id;
// get the form from the account by its ID
var form = await client.GetForm(formId);

DeleteForm(string formId)

Async method to delete a specific form from the account

formId - ID of the form


Example:

var forms = await client.GetForms();
var form = forms.First();
await client.DeleteForm(form.Id);

Form

Use NewForm(string name) or GetForm(string formId) to start working with a form

Method/Property

Description/Examples

SavePosts

Get or set this boolean property that determines if form submissions will be saved to your Plumsail Account or not.


Example:

form.SavePosts = true;

NotifyOwner

Get or set this boolean property that determines if form submissions will be sent as messages to your email or not.


Example:

form.NotifyOwner = true;

Enabled

Get or set this boolean property that determines if form submissions will be sent to the server or not.


Example:

form.Enabled  = true;

Save()

Async method to save the form layout and settings.


Example:

try
{
    await form.Save();
}
catch(InvalidLoginException)
{
    // Set correct authorization header
}
catch(BadRequestException ex)
{
    // ex.Message
}

Layout

Layout is the main content of the form. It’s the property that holds PC/Tablet/Phone layout data, as well as JavaScript, CSS and even the theme data of each form.

Method/Property

Description/Examples

Css

Get or set CSS code for the form.


Example:

form.Layout.Css = ".fd-form h1 { color: red }";

JavaScript

Get or set JavaScript code for the form.


Example:

form.Layout.JavaScript = "fd.rendered(function(){ fd.field('Name').value = 'John Bull' });";

Theme

Get or set theme used for the form. Use one of predefined themes such as Blue, Compact, Default, Explicit, Gray, Green, Orange, Plumsail, Purple, Red, Smooth, or Soft.


Example:

form.Layout.Theme = new Theme(PredefinedThemes.Compact);

PC/Tablet/Phone

Get or set grid that will nest the rest of the form. At least one of these must be filled before the form is saved.

When creating a grid, make sure that each row’s width is less or equal to 12.


Example:

form.Layout.PC = new Grid(
    new GridRow(
        new GridCell(new Text("Text1")
        {
            // configure control
            Content = "This is form, created with using Designer.Public",
            Class = "text-control-class",
            Style = "border: 1px solid red;"
        }, width: 6)
        { Offset = 2, Class = "grid-cell-class" }, // configure cell
        new GridCell(new Submit("Submit1")
        {
            Width = 300
        }, width: 4)
    )
);

Fields and Controls

These can be placed inside of cells, and configured using their own properties.

Constructor

Description/Examples

new Field/Control(string name)

Create fields or controls by giving them name and setting their properties.

name - name of the field/control


Example:

new GridRow(
    new GridCell(new SingleLineTextField("Name")
    {
        Title = "Name",
        ControlHint = "David Bowie",
        Orientation = Orientation.Vertical
    }, width: 6),
    new GridCell(new DateField("Date")
    {
        Title = "Date",
        ControlHint = "Today's date",
        Required = true,
        Orientation = Orientation.Vertical
    }, width: 6)
),
new GridRow(
    new GridCell(new Submit("SubmitButton")
    {
        Width = 300
    }, width: 6)
)