|
SharePoint Designer |
Visual Studio 2005 Designer for Windows Workflow Foundation.
|
|
Can write only sequential workflows. |
Can write both sequential and state machine workflows. |
|
Automatic deployment against the specific list or library against which workflow is being designed. |
Can be deployed as a feature. |
|
Logic is defined declaratively using Steps which comprises of Conditions and Actions |
Logic could be defined through custom code written using C# or VB.NET. |
|
Workflows could be associated to a specific list or library. |
Workflow can be authored as Template which once deployed could be associated with any list or library. |
|
Workflow modifications not possible. |
Workflow modifications are possible using Modification forms built using ASP.NET or InfoPath form. |
|
Workflow markup, rules all are stored as a document library on the site. |
Workflows are compiled as an .NET assembly. |
|
Can’t be debugged. |
Debugging is possible using Visual Studio. |
Using Workflow Object Model in SharePoint.
I was assigned a task to create a simple aspx page where the user could see all the all the different documents, workflows running against them, workflows task information as well as workflow history.
Here we can make use of SPWorkflow and SPWorkflowTask class.
The page would be displaying the information in the following manner
Workflow status for following document :-SampleDocument1
Workflow name :- Approval Workflow
Workflow Task Title
First Team Task
Second Team Task
Third Team Task
Workflow History Description
The approval workflow has started waiting for and Second Team to respond
Task has been created and assigned to First and Second Team
First and Second team has completed their task
Workflow status for following document :- SampleDocument2
Workflow name :- Approval Workflow
Workflow Task Title
First Team Task
Workflow History Description
The approval workflow has started waiting for First and Second Team to respond
The sample code for getting the above information
protected void Page_Load(object sender, EventArgs e)
{
//SPWorkflowManager myWFMgr = new SPWorkflowManager();
SPSite objSite = new SPSite(“http://servername:port”);
SPWeb objWeb = objSite.OpenWeb();
SPList myList = objWeb.Lists[“ListName”];
// for each document within the Library
foreach (SPListItem myListItem in myList.Items)
{
Response.Write(“<b>Workflow status for following document :-</b>” + myListItem[“Title”].ToString());
Response.Write(“</br>”);
// Get the workflows associated
foreach (SPWorkflow myWF in myListItem.Workflows)
{
// Get the name of the workflow
Response.Write(“Workflow name :- “+ myWF.ParentAssociation.Name);
Response.Write(“</br>”);
Response.Write(“<b>Workflow Task Title </b>”);
Response.Write(“</br>”);
// for each workflow running get the workflow tasks and history information
foreach (SPWorkflowTask myWFTask in myWF.Tasks)
{
Response.Write(myWFTask[“Title”].ToString());
Response.Write(“</br>”);
}
// for each workflow running get the history information
Response.Write(“<b>Workflow History Description</b> “);
Response.Write(“</br>”);
SPList myList1 = objWeb.Lists[“Workflow History”];
SPQuery query = new SPQuery();
query.Query = “<OrderBy><FieldRef Name=”ID”/></OrderBy>” +
“<Where><Eq><FieldRef Name=”WorkflowInstance”/>” +
“<Value Type=”Text”>{“ + myWF.InstanceId.ToString() + “}</Value>” +
“</Eq></Where>”;
SPListItemCollection historyListItems = myList1.GetItems(query);
foreach (SPListItem i in historyListItems)
{
Response.Write( i[“Description”].ToString());
Response.Write(“</br>”);
}
}
Response.Write(“</br>”);
}
}
That’s it …
Managing Tasks Permissions Programmatically within SharePoint using event reciever or using special permissions property
I was writing a workflow using SharePoint designer wherein at certain steps tasks were getting created and assigned to different user. But the problem with that was that any user having appropriate rights on the tasks list was able to edit the task.
Below are the two methods using which we can have only the assigned to user having the rights on that task.
It can be done using Event Receiver or within the SharePoint workflow using special permissions property.
public override void ItemAdded(SPItemEventProperties properties)
{
// Name of the List
if (properties.ListTitle == “Tasks”)
{
// Get the SPSite Object
SPSite objSite = new SPSite(“http://servername:portname”);
// Point to the top level web site within it
SPWeb objWeb = objSite.OpenWeb();
// get the task list item getting created
SPListItem myListItem = properties.ListItem;
// get the id of the assigned to user
// we want that only assigned to user should have full rights on that task
string userAssignedTo=myListItem[“Assigned To”].ToString();
int index = userAssignedTo.IndexOf(‘;’);
int id = Int32.Parse(userAssignedTo.Substring(0, index));
// get the SPUser from the id
SPUser user = objWeb.SiteUsers.GetByID(id);
// break the role inheritance
myListItem.BreakRoleInheritance(false);
// webroledefinitions – Full Right, Design, Contribute and Read
SPRoleDefinitionCollection webroledefinitions = objWeb.RoleDefinitions;
SPRoleAssignment roleassignment = new SPRoleAssignment(user);
roleassignment.RoleDefinitionBindings.Add(webroledefinitions[“Full Control”]);
myListItem.RoleAssignments.Add(roleassignment);
// give full control right to the assigned to user
roleassignment.Update();
}
}
Or within workflow as
// handler for create task activity
private void createTask1_MethodInvoking(object sender, EventArgs e)
{
//Specify properties for the task
createTask1.TaskProperties.AssignedTo = @”domainusername”;
createTask1.TaskProperties.Title = @”Please complete the task”;
createTask1.TaskProperties.Description = “This is sample SharePoint Task”;
createTask1.TaskProperties.DueDate = DateTime.Now.AddDays(7);
createTask1.TaskProperties.EmailBody = “This is the sample<b><i> email body </b></i>”;
createTask1.TaskProperties.SendEmailNotification = true;
// Define a HybridDictionary object
HybridDictionary permsCollection = new HybridDictionary();
// Give Administrator rights to the user to whom the task has been assigned
permsCollection.Add(createTask1.TaskProperties.AssignedTo, SPRoleType.Administrator);
// SpecialPermissions -the SpecialPermissions property in your code will strip out all existing permissions inherited from
// the parent list(Workflow Task List) and only adds permissions for each pair you added to the hashtable
createTask1.SpecialPermissions = permsCollection;
}
That’s it ….
Create a custom content type to be used with CreateTaskWithContentType activity in SharePoint Workflow
Create a folder at C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATEFEATURES for e.g. MyCustomContentTask.
Now add three xml files over there
Name them as
· Feature.xml
· MyCustomContentType.xml
· MyCustomContentTypeColumns.xml
First define your custom content type as following (MyCustomContentType.xml)
Our custom content type would be including two custom columns i.e. one for FirstName and other for LastName.
<?xml version=“1.0“ encoding=“utf-8“?>
<Elements xmlns=“http://schemas.microsoft.com/sharepoint/“>
<ContentType ID=“0x0108010011D80FDA18534a3f9A162A1F5D6187F0“
Name=“My Custom Content Type“
Group=“Custom Content Types“
Description=“Custom Content Type“
Version=“0“
Hidden=“FALSE“>
<FieldRefs>
<FieldRef
ID=“{9A447D6C-5BB8-4890-80E5-056B60FF6462}“
Name=“_FirstName“
/>
<FieldRef
ID=“{76CC8E55-59C9-40d6-B46B-A4C1D95B16CB}“
Name=“_LastName“
/>
</FieldRefs>
<XmlDocuments>
<XmlDocument NamespaceURI=“http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url“>
<FormUrls xmlns=“http://schemas.microsoft.com/sharepoint/v3/contenttype/forms/url“>
<New></New>
<Display></Display>
<Edit></Edit>
</FormUrls>
</XmlDocument>
</XmlDocuments>
</ContentType>
</Elements>
ID- Id of the custom content type
0x0108010011D80FDA18534a3f9A162A1F5D6187F0
0x010801 –The id of the base content type the custom content type should derive from
http://msdn.microsoft.com/en-us/library/ms452896.aspx
Followed by 00.
And a new guid 11D80FDA18534a3f9A162A1F5D6187F0.
Name- Name of the custom content type.
Hidden – Whether or not the users should see the content type.
FieldRefs – Collection of column references. Can contain one or more FieldRef.
FieldRef- Define the fields.
Id – Give a new guid.
Name – Name of the field
XmlDocument – Here we could define our custom form that should open when users creates, edit task or when the task is displayed.We can simply let it blank if we want to use the same forms that SharePoint uses for the default task.
Now to define the columns that we have just added we’ll put the following information in our MyCustomContentTypeColumns.xml file
For your custom columns (MyCustomContentTypeColumns.xml)
<?xml version=“1.0“ encoding=“utf-8“?>
<Elements xmlns=“http://schemas.microsoft.com/sharepoint/“>
<Field ID=“{9A447D6C-5BB8-4890-80E5-056B60FF6462}“
Name=“_ FirstName “
DisplayName=“First Name“
Group=“Custom Columns“
Type=“Note“
NumLines=“10“
Required=“ TRUE “>
</Field>
<Field ID=“{76CC8E55-59C9-40d6-B46B-A4C1D95B16CB}“
Name=“_ LastName“
DisplayName=“ Last Name“
Group=“Custom Columns“
Type=“ Note “
NumLines=“10“
Required=“TRUE“>
</Field>
</Elements>
Here we have two field tags corresponding to the FieldRef field defined in MyCustomContentType.xml.
ID- It should the same as defined for Field Ref field
Name- Same as defined for Field Ref field.
Type – Note i.e. appear as multiline text box.
Required – To make the field mandatory.
For more info for Field
http://msdn.microsoft.com/en-us/library/ms437580.aspx
And for the Feature.xml
Id – Generate a new guid for the feature
Title – The name for the feature
ElementManifest- Defining the location of the above two files defining our custom content type.
<?xml version=“1.0“ encoding=“utf-8“?>
<Feature Id=“64CB36F5-BF1A-470e-A27F-213A9F170DA4“
Title=“My Custom Feature“
Description=“Represents a custom content type“
Version=“12.0.0.0“
Scope=“Site“
xmlns=“http://schemas.microsoft.com/sharepoint/“>
<ElementManifests>
<ElementManifest Location=“ MyCustomContentTypeColumns.xml“ />
<ElementManifest Location=“ MyCustomContentType.xml“ />
</ElementManifests>
<Properties>
<Property Key=“Globallyavailable“ Value=“TRUE“ />
</Properties>
</Feature>
Now install the feature and activate it using the stsadm tool
stsadm.exe -o installfeature -filename MyCustomContentTask Feature.xml
stsadm -o activatefeature -filename MyCustomContentTask Feature.xml -URL http://localhost:82
Now to use it
Go to your Lists à Settings àListSettings
Scroll down to find Add from existing site content types
Click on it and Select your new content type from Available Site Content Types.
That’s it..
Publishing workflow as an ASP.NET Web Service.
The workflows developed using windows workflow foundation can be published as a web service. To publish a workflow as a web service, we are provided with two activities. WebServiceInput and WebServiceOutput activity.
To create a workflow to be published as a web service do the following
Create a new Empty Workflow Project.
Add a new interface class to the project which would be used by our WebServiceInput activity.
Suppose this is our interface class.
interface IAddInterface
{
int AddNumber(int firstNumber, int secondNumber);
}
Now right click on the project and add a new Sequential Workflow.
Now drag and drop three activities. First WebServiceInput, Code and WebServiceOutput.
For webServiceInputActivity1 specify following properties.
InterfaceType –IAddInterface.
MethodName– AddNumber
As soon as method is added we’ll see two new properties added over there one for each parameter specified in the interface method.
i.e. firstNumber and secondNumber.
Create two variables to bind with them and one more variable to hold the result.
public int intFirstNumber;
public int intSecondNumber;
public int total;
Specify intFirstNumber, intSecondNumber for firstNumber and secondNumber property of webServiceInputActivity1.
Finally set IsActivating as true;
Now right click code activity and select generate handlers to create a handler method and specify the following code for it
private void codeActivity1_ExecuteCode(object sender, EventArgs e)
{
total = intFirstNumber + intSecondNumber;
}
Now select webServiceOutputActivity1 and set its properties as following
InputActivityName = webServiceInputActivity1
ReturnValue = Activity=AddWebServiceWorkflow, Path=total (i.e. to total variable)
That’s it now we want to publish it as workflow.
For this right click the project and Select Publish as a web servcie option.
This would create a new webservice which would be named as worklflowProjectName.worfklowMethodName_WebService.asmx.
Now we could use it just like any other web service created using ASP.NET.
WebServiceInputActivity recieves input from the client using the webservice. It can be associated with single web service method defined inside the interface. The WebServiceInputActivity can be followed by the set of activities which could be built in or custom activity. If the response has to sent back to the calling client than we need to make use of WebServiceOutputActivity.
Failed on Start (retrying) or Correlation Token in SharePoint Workflow
This error occurs mostly when the Correlation Token values has been not set properly for activities.
Correlation token– A correlation token is essentially a means of uniquely identifying each instance of a workflow, modification or task.
OnWorkflowActivated, OnWorkflowItemChanged,OnWorkflowItemDeleted, SetState, SendEmail and UpdateAllTasks these activities should be using same workflow token for e.g. workflowToken.
CreateTask, CreateTaskWithContentType,UpdateTask, DeleteTask, CompleteTask,RollbackTask, OnTaskChanged, OnTaskDeleted, OnTaskCreated these activites should be using same correlation token for e.g. taskToken.
EnableWorkflowModification, OnWorkflowModified should be sharing same correlation token for e.g. modificationToken.
Each distinct task or modification would be having it own distinct token.
