Tuesday, January 26, 2010

How to terminate/cancel workflow from SharePoint object model

While terminating or cancelling workflow, it will delete all the tasks from the task list and it will marked task status as “Cancelled” into attached list.

Reference Link:


Visual Studio Approval Workflow Example



How to get workflow instance for single workflow into listitem


Getting access denied error while updating list items

I was getting access denied error while updating list items, I already tried to put my code into SPSecurity.RunWithElevatedPrivileges delegate, still I was getting error. Below was my actual code.


//code line for updating list columns



After doing debugging and removing line by line from actual code I have figured out actual problem – the user who has contributor rights(normal user) was getting access denied error, admin users can able to update list without any problem, it’s because of LIST.UPDATE() statement, because contributor rights users don’t have permission to change anything into List settings, they can add/delete list items but they can’t able to change anything into list, after removing LIST.UPDATE() statement from my actual code, all users were able to successfully update the list items.


How to attach/delete attachments from SharePoint list items


ERROR: Workflow failed on start

I was getting below error while starting of workflow.

Workflow:System.Workflow.ComponentModel.Compiler.WorkflowValidationFailedException: The workflow failed validation.     at System.Workflow.Runtime.WorkflowDefinitionDispenser.ValidateDefinition(Activity root, Boolean isNewType, ITypeProvider typeProvider)     at System.Workflow.Runtime.WorkflowDefinitionDispenser.LoadRootActivity(Type workflowType, Boolean createDefinition, Boolean initForRuntime)     at System.Workflow.Runtime.WorkflowDefinitionDispenser.GetRootActivity(Type workflowType, Boolean createNew, Boolean initForRuntime)     at System.Workflow.Runtime.WorkflowRuntime.InitializeExecutor(Guid instanceId, CreationContext context, WorkflowExecutor executor, WorkflowInstance workflowInstance)     at System.Workflow.Runtime.WorkflowRuntime.Load(Guid key, CreationContext

I spend lots of time to find out exact resolution for workflow failed error, but I couldn’t get solution, below are some of the reference link which talks causing of issue due to migration from visual studio 2005 to 2008 workflow project, but in my case, I have created workflow into visual studio 2008 only, some articles were talking about adding some line into solution project, but that’s also not working for me.

Reference Link:




At the last for resolving my issue, I have created new workflow file, one thing I have observed here – if I am going to change main default file name for workflow file from workflow.cs to something else, then sometime it will mess up workflow. So I would recommend not changing workflow.cs file name.


How to get current status of workflow from SharePoint object model

Reference Link:


Sample Code:
//Obtain a list item
SPWeb thisWeb = SPControl.GetContextWeb(Context);
SPList issuesList = thisWeb.Lists["Customer Issues"];
SPListItem myItem = issuesList.Items[0];
//Get the workflow status value
int WorkflowStatusValue = myItem["MyWorkflow"];
string WorkflowStatusString = System.Enum.GetName
        (typeof(SPWorkflowStatus), WorkflowStatusValue);
StatusLabel.Text = "The current status of the workflow is: " +

SPWorkflow AlterTask didn’t work with hash table which has space into key name

While creating hash table for passing into altertask method, we need to be very careful, because it didn’t like spaces into key name, we need to put our key without spaces.

Sample Code:

taskHash["UserInputField"] = UserInputField.Text;

taskHash["UserInputField_ForHash"] = UserInputField.Text;

taskHash["Status"] = "Completed";

taskHash["Status_ForHash"] = "Completed";

SPWorkflowTask.AlterTask(taskListItem, taskHash, true);

Reference Link:


ERROR: The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again

I was getting above error while doing altertask on SPWorkflowTask.AlterTask method.

Solution:  we need to write altertask code under SPSecurity delegate and need to write code for Allowunsafeaccess.



ERROR: The content type of a workflow task must be derived from the Workflow Task content type

I was getting “The content type of a workflow task must be derived from the Workflow Task content type” error while working with custom ASPX form for visual studio workflow, I have created custom content type which will be used into workflow, I have found out that my content type ID is incorrect by comparing it with some another content type ID. Visual studio activity “Createtaskwithcontenttype” must be inherited from workflow task.

Reference Links: