Custom forms in custom site template

Hi,

We use SharePoint Online on Office 365.

If I create a form in a site template using Forms Designer for SharePoint on Office 365, will the form be available in all sites created which are based on that template? I had set up a site template using PowerApps, only to find that PowerApps doesn’t tie into the site template and so any new sites created revert to the standard SharePoint form.

Thanks,

Marko

Hello Marko!

We’re working on a series of articles describing how to provision forms with PnP templates. For now, you can use our API to provision forms from code.

First, you should find two assemblies in the folder where Plumsail Forms is installed on your computer, usually it’s here:

c:\Users{your user}\AppData\Local\Apps\2.0{folder with arbitrary name}{another folder with arbitrary name}\desi{something}\

You need the following assemblies (we will publish them as a Nuget package soon):

FormsDesigner.Data.dll
FormsDesigner.SharePoint.dll

Add references to them in your project. Also, you must install the following Nuget packages:

Newtonsoft.Json
System.Net.Http
System.Net.Http.Formatting.Extension

Then, design a form or open an existing one that you want to provision in Plumsail Forms. Export it to XFDS file with Export button in the ribbon.

Then, use the code below to provision the form as an Edit one to an arbitrary site/list in SharePoint:

static void Main(string[] args)
{
    var login = "your login";
    var password = GetSecureString("your password");
    var webUrl = "SharePoint site URL";

    using (var ctx = new ClientContext(webUrl))
    {
        ctx.Credentials = new SharePointOnlineCredentials(login, password);

        // Specify a list which form you want to replace
        var list = ctx.Web.Lists.GetByTitle("ListName");
        var cts = list.ContentTypes;

        ctx.Load(list);
        ctx.Load(cts);
        ctx.ExecuteQuery();

        // Specify a content type which form you want to replace
        var contenType = cts.FirstOrDefault(ct => ct.Name == "Item");

        var forms = new FormsDesigner.SharePoint.FormsManager(ctx, list.Id, contenType.Id.ToString());

        // Load your XFDS-file
        var layout = XDocument.Load("Item_Edit.xfds");

        FormsDesigner.SharePoint.Config.PlumsailServicesBaseAddress = new Uri("https://forms.plumsail.com/");
        Task.Run(async () =>
        {
            var compiledForm = await CompileForm(layout);

            // The form will replace a default Edit form in the target list
            forms.GenerateForms(Guid.Empty, FormsDesigner.Data.SharePoint.FormTypes.Edit, layout, compiledForm);
        }).Wait();
        
    }
}

private static async Task<CompiledForm> CompileForm(XDocument layout)
{
    HttpClient httpClient = new HttpClient();
    httpClient.BaseAddress = new Uri("https://forms.plumsail.com/");
    httpClient.DefaultRequestHeaders.Accept.Clear();
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var response = await httpClient.PostAsJsonAsync($"api/sharepoint", layout);
    response.EnsureSuccessStatusCode();
    return await response.Content.ReadAsAsync<CompiledForm>();
}

private static SecureString GetSecureString(string s)
{
    SecureString result = new SecureString();
    foreach (char c in s.ToCharArray())
    {
        result.AppendChar(c);
    }

    return result;
}

Feel free to ask your questions.

Thanks for this - does this work on SharePoint Online for Office 365 (the 2016 version)?

Dear Marko,
The answer posted by Dmitry refers to Plumsail Forms for SharePoint Online Modern UI. If you are using Plumsail Forms, it’s going to work with Office365 SharePoint as it’s the only platform supported currently.

If you are using Forms Designer instead, then this code wouldn’t work. Let us know if this is the case.

Hey is there any guides yet on how to apply the form templates with PNP?

I have the files in my templated site in the right location and would just like to attach them to the lists easily.

Dear Chris,

Please consider this article: https://plumsail.com/docs/forms-sp/how-to/provision.html