How to create custom action for workflow in share point 2013
1. Flexmind Solutions www.flexmindsolutions.com
Email: support@flexmindsolutions.comContact: 080- 41115849
Steps to Create a Custom Action for Workflow in SharePoint 2013 [WF4]
Download: The solution can be downloaded from Microsoft Technet Gallery here:
http://gallery.technet.microsoft.com/Create-Query-XML-workflow-70c8369b
Action Name: - Build XML Document
Purpose: - To generate the XMLDocument object from the given XML input as a string.
Arguments Type
InArgument /XMLString String
OutArgument /XMLDOcument XMLDocument
Steps:-
1) Create the VSTS 2010 empty solution.
2) Create new Workflow code activity project, select the frame work version 4.
3) Give the name of code activity file as BuildXMLDocument.cs
4) Open the code file
5) Declare one input argument and one output argument
publicInArgument<String>XMLString { get; set; }
publicOutArgument<XmlDocument>XMLDocument { get; set; }
6) In execute method, place the below code
protectedoverridevoid Execute(CodeActivityContext context)
{
// Obtain the runtime value of the Text input argument
StringXMLString = context.GetValue(this.XMLString);
byte[] byteArray = Encoding.ASCII.GetBytes(XMLString);
XmlDocumentxmldoc = newXmlDocument();
using (MemoryStream stream = newMemoryStream(byteArray))
{
xmldoc.Load(stream);
}
XMLDocument.Set(context, xmldoc);
}
7) Build the solution. Now you can find your activity in the VSTS tool box
2. Flexmind Solutions www.flexmindsolutions.com
Email: support@flexmindsolutions.comContact: 080- 41115849
Action Name: - Query XML Data
Purpose: - To query the XML using XPath, result output as collection of string, or return the single text
value
Arguments/Parameter Types
InArgument / XMLData XMLDocument
InArgument / XPathQuery String
OutArgument / Value String
OutArgument / MultiValue System.Collections.Generic.IList<String>
Steps:
1) Add new item to the existing project, workflow code activity name it as QueryXMLData.cs.
2) Declare argument as
publicInArgument<XmlDocument>XMLFileData { get; set; }
publicInArgument<String>XPathQuery { get; set; }
publicOutArgument<String> Value { get; set; }
publicOutArgument<System.Collections.Generic.IList<String>>MultiValue { get; set; }
3) Execute method
protectedoverridevoid Execute(CodeActivityContext context)
{
// Obtain the runtime value of the Text input argument
XmlDocumentXMLDoc= context.GetValue(this.XMLFileData);
StringXPathQuery = context.GetValue(this.XPathQuery);
Value.Set(context, string.Empty);
// Dictionary<string, string>multivalue = new Dictionary<string, string>();
IList<String>multivalue = newList<string>();
XmlNodeList _xmlNameSpaceList = XMLDoc.SelectNodes(@"//namespace::*[not(. =
../../namespace::*)]");
XmlNamespaceManager _xmlNSmgr = newXmlNamespaceManager(XMLDoc.NameTable);
// Read all the name space in the XML
foreach (XmlNodensNodein _xmlNameSpaceList)
{
// Console.WriteLine(nsNode.LocalName + " " + nsNode.Value);
3. Flexmind Solutions www.flexmindsolutions.com
Email: support@flexmindsolutions.comContact: 080- 41115849
if (nsNode.LocalName.ToLower() != "xmlns")
_xmlNSmgr.AddNamespace(nsNode.LocalName, nsNode.Value);
else
_xmlNSmgr.AddNamespace("def", nsNode.Value);
}
XmlNodeListnodelist = XMLDoc.SelectNodes(XPathQuery, _xmlNSmgr);
///catalog/cd[price>10.0] //cd[@country='UK'] //catalog/cd/title
//def:ProjectDataSet/def:Assignment/def:PROJ_UID
if (nodelist.Count> 1)
{
// intiPos = 0;
foreach (XmlNodeisbninnodelist)
{
//multivalue.Add(iPos.ToString(), isbn.InnerText);
multivalue.Add(isbn.InnerText);
// iPos++;
}
MultiValue.Set(context, multivalue);
}
elseif (nodelist.Count == 1)
{
foreach (XmlNodeisbninnodelist)
{
Value.Set(context, isbn.InnerText);
}
}
}
4) Build the project, verify the activity appear in VSTS tool box
5) Now we will create the workflow using above activity we build
6) Open the Activity.xamlfile , place the activity from VSTS tool box.
4. Flexmind Solutions www.flexmindsolutions.com
Email: support@flexmindsolutions.comContact: 080- 41115849
7) Create 2 input parameter
8) Create variables
9) Set the input and output parameter for BuildXMLDocument activity
5. Flexmind Solutions www.flexmindsolutions.com
Email: support@flexmindsolutions.comContact: 080- 41115849
10) Similarly set the property forQueryXMLData
11) Place the foreach action to iterate through the collection
12) Build the project
13) To test the workflow, create the workflow console application, add the reference of the
workflow project created above.
Dictionary<string, Object> param2 = newDictionary<string, object>();
param2.Add("InputXML", @"<?xml version='1.0' ?>
<catalog>
<cd country='USA'>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>