Showing posts with label Workflow. Show all posts
Showing posts with label Workflow. Show all posts

Publish Nintex workflow file to all sites and libraries using code

Nintex workflows - They are easy to build workflows and customize. I used them in couple of projects and it supports many activities which we can use and build workflows according to requirements. But, this time I got a big project which has plenty of sub sites in a site collection. All sub sites are having the same site template and it has the same structure. Each sub sites has many libraries and out of them 8 are having workflow enabled.
When I got a requirement to change something in workflow then I am in trouble like how to publish the new changed workflow to all libraries. Right now I have 148 sub sites. It means I have to publish the nintex workflow to 148 * 8  = 1184 libraries. Which is not at all possible with the manual upload process. So, the only way would be writing code to publish them automatically by running it.

Few days back, I have written the post which describes the same without coding here.  But, that needs lots of prerequisites and will apply to only one site. The solution which I have written needs the updated nintex workflow file [.NWF] as input, web url and site url. It loops through all sites in the site collection and updates the each and every library with the updated nintex file successfully.

Below is the code which does that for one site:
private static void UpdateWokflowToOneSite()
{
string siteUrl = ConfigurationManager.AppSettings["SiteUrl"];
List<string> listNames = new List<string>() { "List - 1", "List - 2", "List - 3", "List - 4", "List - 5"};
using (SPSite site = new SPSite(siteUrl))
{
string webUrl = ConfigurationManager.AppSettings["WebUrl"];

if (string.IsNullOrEmpty(webUrl))
{
webUrl = "/";
}
using (SPWeb web = site.OpenWeb(webUrl))
{
byte[] rawData = File.ReadAllBytes("UpdatedNintexWorkflow.nwf");
NintexWorkflowWS.NintexWorkflowWS ws = new NintexWorkflowWS.NintexWorkflowWS();
ws.Url = web.Url + "/_vti_bin/NintexWorkflow/workflow.asmx";
ws.UseDefaultCredentials = true;

int i = 1;
foreach (string listName in listNames)
{
ws.PublishFromNWF(rawData, listName, string.Format("NintexWorkflow-{0}", i), false);
i++;
}
}
}
}

Below are the prerequisites for running above code.
  1. I have created a console application and writing code in it. So that I will get EXE as output and running it wherever needed [different servers by changing configuration file]. 
  2. We have to add the Nintex workflow web service reference to the project. So that we will call it and use it in code. The below line in the code is web service instantiation.
    NintexWorkflowWS.NintexWorkflowWS ws = new NintexWorkflowWS.NintexWorkflowWS();
  3.  The user who is logged in has the permissions needed to publish the nintex workflow. The administrator access are needed to run the above code.
  4. The NWF file location according to above code should be in the same location where EXE is present. By default it will be project location/bin/debug.
  5.  listNames is the variable which has all list/library names in the site to which we have to publish the workflow. In case if you want to publish to all list/libraries then replace the listNames in foreach with web.Lists.
Now, the app.config entries are configurable. Below are the configuration changes needed.
<appSettings>
<add key="SiteUrl" value="http://sharepointsite"/>
<add key="WebUrl" value="/"/>
</appSettings>
<system.serviceModel>
<bindings />
<client />
</system.serviceModel>
<applicationSettings>
<DeployWorkflowNWF.Properties.Settings>
<setting name="DeployWorkflowNWF_NintexWorkflowWS_NintexWorkflowWS"
serializeAs="String">
<value>http://sharepointsite/_vti_bin/NintexWorkflow/Workflow.asmx</value>
</setting>
</DeployWorkflowNWF.Properties.Settings>
</applicationSettings>
In this, change the configuration as needed and use it.

You can use the same code loop through each and every web and publish it to all webs. I mean one more loop is enough to do that job as shown below.
private static void UpdateWokflowToAllWebs()
{
string siteUrl = ConfigurationManager.AppSettings["SiteUrl"];
List<string> listNames = new List<string>() { "List - 1", "List - 2", "List - 3", "List - 4", "List - 5"};
using (SPSite site = new SPSite(siteUrl))
{
foreach(SPWeb web in site.AllWebs)
{
byte[] rawData = File.ReadAllBytes("UpdatedNintexWorkflow.nwf");
NintexWorkflowWS.NintexWorkflowWS ws = new NintexWorkflowWS.NintexWorkflowWS();
ws.Url = web.Url + "/_vti_bin/NintexWorkflow/workflow.asmx";
ws.UseDefaultCredentials = true;

int i = 1;
foreach (string listName in listNames)
{
ws.PublishFromNWF(rawData, listName, string.Format("NintexWorkflow-{0}", i), false);
i++;
}
}
}
}

There we are done. Just run the EXE and it will update the workflow to each and every library in a web.

Complete project is available for download here. Please let me know, if you need any more help. Read More...

Role of ItemMetadata.xml in Infopath forms [SharePoint workflow].

Introduction:

ItemMetadata.xml is the file used to store/transfer the data from one Infopath to another. The name of the file is case - sensitive.

For example, in a sharepoint workflow, there are 10 steps, in each step we are using infopath and we want send some information from one to the next infopath form, the information won't save any where. we need to tell explicitly to infopath to store in xml file. that is nothing but ItemMetadata.xml.

It acts as the secondary data source to the infopath.

To compensate for the possibility of schema differences between the task data and the form it only stores the schema instead of data.

Example schema is

<z:row xmlns:z="#RowsetSchema" ows_fieldname=""/>

Note: Every field name should be prefixed with ows

In programming, we can retrieve the data from info path using the property called ExtendedProperties.

afterProperties.ExtendedProperties["filedName"].ToString(); Read More...

Conditional Statement example in SharePoint workflow.

In SharePoint workflow, when we are using the if/else activities, the if/ else methods contains Event argument called ConditionalEventArgs.
if you set e.Result=true, then if block will executes,
if you set e.Result=false, then else block will executes.

private void IfMethod(object sender, ConditionalEventArgs e)

{

if (isTrue)

e.Result = true;

else

e.Result = false;

}

private void ElseMethod(object sender, ConditionalEventArgs e)

{

if (!isTrue)

e.Result = true;

else

e.Result = false;

}

depending on the boolean variable “isTrue”, the if/else blocks will executes…
In the above case, i am setting “isTrue” variable depending on my logic, before calling if/else statements.
For while activity also, same logic applies.
if you set e.Result=true, it will iterate until you set e.Result=false.
Read More...

Creating new instance of SharePoint workflow through C# code.

In SharePoint 2007, very good feature I like much is that framework supports for writing custom events, features, workflows or any other stuff... We can customize complete SharePoint system at any context.
While working with SharePoint custom workflows, we get some scenarios where we need to stop a workflow or start a new instance of workflow through coding. So, I did some research on the MSDN library and saw all the dll's by doing some reflection on the libraries and found some functions and classes which supports this.

Here, I want to present a small code snippet which does terminating currently running wokflow and start a new instance of the workflow on the same list item.
SPListItem listItem = workflowProperties.Item;
SPList spList = workflowProperties.List;
string initData = workflowProperties.InitiationData;

const string WF_GUID = “The GUID of workflow template”;
workflowProperties.Site.WorkflowManager.RemoveWorkflowFromListItem(workflowProperties.Workflow);
workflowProperties.Site.WorkflowManager.Dispose();

SPWorkflowAssociation associationTemplate= spList.WorkflowAssociations.GetAssociationByBaseID(new Guid(WF_GUID));

workflowProperties.Site.WorkflowManager.StartWorkflow(listItem, associationTemplate, initData);

Hope this will give you a good start to code on SharePoint workflow start and terminate through coding. Let me know, what you think.

Read More...
Related Posts with Thumbnails
GiF Pictures, Images and Photos