You can use the Nintex Forms for Office 365 REST API to import the contents of a Nintex Forms for Office 365 export (.nfp) file into a SharePoint list. This process will import, save, and publish your form where it will be available to users of the SharePoint site.
For more information about the REST resource used to export list form, see Import Form.
Note: This API supports forms designed using the Classic Forms and Responsive Forms designers. Forms designed using the New Responsive Forms designer are not supported.
To avoid surprises while trying to import into a new form, take the following points into consideration:
Ensure that your SharePoint authentication cookie hasn't expired.
This example avoids that issue by getting an authentication cookie from SharePoint every time you run the example, but you can cache an authentication cookie and avoid a round trip to SharePoint as long as the authentication cookie hasn't expired.
Don't modify the contents of the export file.
The export file is a binary file that uses a format specific to Nintex Forms for Office 365. Manipulating the contents can cause unpredictable outcomes and invalidate the format of the export file, making it unusable.
To use the example, ensure that you have the following prerequisites:
This example avoids that issue by getting an authentication cookie from SharePoint every time you run the example, but you can cache an authentication cookie and avoid a round trip to SharePoint as long as the authentication cookie hasn't expired.
The export file is a binary file that uses a format specific to Nintex Forms for Office 365. Manipulating the contents can cause unpredictable outcomes and invalidate the format of the export file, making it unusable.
Access to the NintexWorkflow for Office 365 REST API, and an API key with which to authorize the REST API. For more information, see Authentication and authorization.
You can download the code used in this example from the following location:
O365_RESTAPI_NF_Import.zip
Note: You need to configure the code for the example before you can build and run it. See step 4 in the following example for instructions about how to configure the code for the example.
The following example describes how to create a Visual Studio 2013 project to import a list form to your SharePoint Online site. In this sample you will use the REST API to post the form definition into a SharePoint list on your site.
Create a new Visual Studio 2013 project, using the Console Application template for Visual C#.
Add the following references to your new Visual Studio project.
Microsoft.SharePoint.Client
Microsoft.SharePoint.Client.Runtime
System.Net
System.Net.Http
In your new Visual Studio project, paste the following code into the file named Program.cs, overwriting the existing using statements at the beginning of the file:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.Security;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Net;
using System.IO;
Paste the following code into Program.cs, overwriting the Main() static method already included in the Program static class, and then configure the code as described in the comments:
class Program
{
// The API key and root URL for the REST API.
// TODO: Replace with your API key and root URL.
static private string apiKey = "";
static private string apiRootUrl = "";
// The SharePoint site and credentials to use with the REST API.
// TODO: Replace with your site URL, user name, and password.
static private string spSiteUrl = "";
static private string spUsername = "";
static private string spPassword = "";
// The list form to export, and the name of the destination list for which
// the new form is to be imported.
// TODO: Replace with the path to your form package (NFP or XML)
// and target list title.
static private string importFileName = "";
static private string listId = "";
static private string contentTypeString = "";
/// < summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// Copy a list form from a local form file to a destination list.
CopyFormToList();
Console.Write("Press a key to close the window.");
Console.ReadKey();
}
All the configuration you need to do for this example happens here, and the code provided in subsequent steps uses these variables to get an authentication cookie from SharePoint, and then import into a new list form for a SharePoint list on your site.
static private string GetSPOCookie()
{
// If successful, this variable contains an authentication cookie;
// otherwise, an empty string.
string result = String.Empty;
try
{
// Construct a secure string from the provided password.
// NOTE: For sample purposes only.
var securePassword = new SecureString();
foreach (char c in spPassword) { securePassword.AppendChar(c); }
// Instantiate a new SharePointOnlineCredentials object, using the
// specified username and password.
var spoCredential = new SharePointOnlineCredentials(spUsername, securePassword);
// If successful, try to authenticate the credentials for the
// specified site.
if (spoCredential == null)
{
// Credentials could not be created.
result = String.Empty;
}
else
{
// Credentials exist, so attempt to get the authentication cookie
// from the specified site.
result = spoCredential.GetAuthenticationCookie(new Uri(spSiteUrl));
}
}
catch (Exception ex)
{
// An exception occurred while either creating the credentials or
// getting an authentication cookie from the specified site.
Console.WriteLine(ex.ToString());
result = String.Empty;
}
// Return the result.
return result;
}
The GetSPOCookie static method uses the SharePoint site and credentials that you configured in step 4 to get an authentication cookie from SharePoint.
Paste the following code into Program.cs, immediately after the code you pasted in the previous step:
static private void CopyFormToList()
{
// Create a new HTTP client and configure its base address.
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(spSiteUrl);
// Add common request headers for the REST API to the HTTP client.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Api-Key", apiKey);
// Get the SharePoint authentication cookie to be used by the HTTP client
// for the request, and use it for the Authorization request header.
string spoCookie = GetSPOCookie();
if (spoCookie != String.Empty)
{
var authHeader = new AuthenticationHeaderValue(
"cookie",
String.Format("{0} {1}", spSiteUrl, spoCookie)
);
// Add the defined Authorization header to the HTTP client's
// default request headers.
client.DefaultRequestHeaders.Authorization = authHeader;
}
else
{
throw new InvalidOperationException("Cannot define Authorization header for request.");
}
// Read the contents of our form into a byte array, so that we can send the
// contents as a ByteArrayContent object with the request.
if (System.IO.File.Exists(importFileName))
{
// Read the file.
byte[] exportFileContents = System.IO.File.ReadAllBytes(importFileName);
ByteArrayContent saveContent = new ByteArrayContent(exportFileContents);
// If we're at this point, we're ready to make our request.
// Note that we're making this call synchronously - you can call the REST API
// asynchronously, as needed.
var importFormUri = String.Format("{0}/api/v1/forms/{1},{2}",
apiRootUrl.TrimEnd('/'),
Uri.EscapeUriString(listId, contentTypeString);
HttpResponseMessage saveResponse = client.PutAsync(importFormUri, saveContent).Result;
if (saveResponse.IsSuccessStatusCode)
{
Console.WriteLine("Successfully imported form.");
}
else
{
Console.WriteLine("Failed to import form.");
}
}
}
The ImportForm static method uses an HTTP client to invoke the REST resource provided by the REST API to first import your list form from a local file.
Then, the same HTTP client is used to import the contents of the file into a new list form for the specified SharePoint list, with the specified content type ID. A ByteArrayContent object is used to encapsulate the binary contents of the export file, and the PutAsync method is used to invoke the REST resource.
Build and run the Visual Studio project.
If you've configured the variables provided in step 4 appropriately, running the project produces a copy of the specified list form associated with the specified SharePoint list on your SharePoint site.
Note: The form name does not change. If you already have a list form for the specified SharePoint list with a name that matches the name of the imported form, an error occurs.