Creating Nintex workflow constants programmatically

Workflow constants in Nintex workflows are useful for storing constants that can be used across a site. For example, if you store a credential as a workflow constant, you can then use the constant within all Nintex actions that require credentials, such as the “Query User Profile” action. Then when the password is changed, the credential only needs to be changed in one place (as opposed to the case where the credentials are stored in each action and need to be changed within each action).

While you can create a site level workflow constant through the UI by going to the site’s settings and then “Manage workflow constants,” it’s possible to create a workflow constant programmatically.

Here is the code for that:

public static void CreateWorkflowConstant(string title, string description, string value, bool sensitive, Guid siteId, Guid webId, WorkflowConstant.WorkflowConstantType type, bool adminOnly) {

var oc = new WorkflowConstant(title, description, value, sensitive, siteId, webId, type, adminOnly);

oc.Update();

}

public static void CreateCredentialWorkflowConstant(string title, string description, CredentialValue cred, bool sensitive, Guid siteId, Guid webId, bool adminOnly) {

if (cred == null) return;

var serializer = new XmlSerializer(typeof(CredentialValue));

var sb = new StringBuilder();

using (var sw = new StringWriter(sb)) {

serializer.Serialize(sw, cred);

}

var value = sb.ToString();

CreateWorkflowConstant(title, description, value, sensitive, siteId, webId, WorkflowConstant.WorkflowConstantType.Credential, adminOnly);

}

static void Main(string[] args) {

try {

using (SPSite site = new Microsoft.SharePoint.SPSite(<SITE URL>)) {

SPWeb web = site.RootWeb;

var cred = new CredentialValue(@”am\jsaito”, “pwd”);

CreateCredentialWorkflowConstant(“title”, “desc”, cred, true, site.ID, web.ID, false);

web.Update();

}

}

catch (Exception e) {

Console.WriteLine(“Error: ” + e);

Console.ReadLine();

}

}

When you run it, you’ll find that a workflow constant is created on the site collection level:

WorkflowConstant

Workflow Constant added at the site level

Advertisements

2 Responses to Creating Nintex workflow constants programmatically

  1. Utsab Lohani says:

    hi Jaclyn, this is very helpful. Thanks for posting!

    Utsab

  2. Siva says:

    hi Jaclyn,
    nice information

    Siva

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: