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