I used SharePoint Designer 2007 to make a Workflow that copy item of Calendar list to Contact list when I create a new item. Then I run my code that new item in Calendar list. Unfortunately, I get a message as below
Microsoft.SharePoint.SPException: The security validation for this page is invalid.
Click Back in your Web browser, refresh the page, and try your operation again. --->
System.Runtime.InteropServices.COMException (0x8102006D): The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.
at Microsoft.SharePoint.Library.SPRequestInternalClass.ValidateFormDigest(String bstrUrl, String bstrListName)
at Microsoft.SharePoint.Library.SPRequest.ValidateFormDigest(String bstrUrl, String bstrListName)
--- End of inner exception stack trace ---
at Microsoft.SharePoint.Library.SPRequest.ValidateFormDigest(String bstrUrl, String bstrListName)
at Microsoft.SharePoint.SPWeb.ValidateFormDigest()
at Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflow(SPListItem item, SPWorkflowAssociation association, SPWorkflowEvent startEvent, Boolean bAutoStart, Boolean bCreateOnly)
at Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflow(SPListItem item, SPWorkflowAssociation association, String eventData, Boolean isAutoStart)
at Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflow(SPListItem item, SPWorkflowAssociation association, String eventData)
at StartWorkflowHelper.StartWorkflowFunction.StartWorkflow(SPListItem item)
at AddItem.btnSubmit_Click(Object sender, EventArgs e)
And my code to run workflow :
//Class Handle Workflow and I built this class to dll and set it into GAC.
namespace StartWorkflowHelper
{
public class StartWorkflowFunction
{
[System.Security.Permissions.PermissionSet (System.Security.Permissions.SecurityAction.Assert, Unrestricted = true)]
public static void StartWorkflow(SPListItem item)
{
SPList list = item.ParentList;
SPEventManagerWrapper.DisableEventFiring();
SPWorkflowAssociationCollection wfac = list.WorkflowAssociations;
using (SPWorkflowManager manager = item.Web.Site.WorkflowManager)
{
foreach (SPWorkflowAssociation wfa in wfac)
{
if (wfa.AutoStartChange || wfa.AutoStartCreate)
{
manager.StartWorkflow(item, wfa, "");
}
}
}
SPEventManagerWrapper.EnableEventFiring();
}
}
public static class SPEventManagerWrapper
{
private static readonly string _className = "Microsoft.SharePoint.SPEventManager";
private static readonly string _eventFiringSwitchName = "EventFiringDisabled";
private static Type _eventManagerType;
//Gets the status of event firing on the current thread
public static bool EventFiringDisabled
{
get { return GetEventFiringSwitchValue(); }
}
private static Type EventManagerType
{
get
{
if (_eventManagerType == null)
GetEventManagerType();
return _eventManagerType;
}
}
//Enables event firing on the current thread
public static void EnableEventFiring()
{
SetEventFiringSwitch(false);
}
//Disables sharepoint event firing on the current thread
public static void DisableEventFiring()
{
SetEventFiringSwitch(true);
}
//Sets the event firing switch on Microsoft.SharePoint.SPEventManager class using reflection
private static void SetEventFiringSwitch(bool value)
{
PropertyInfo pi = EventManagerType.GetProperty(_eventFiringSwitchName, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
pi.SetValue(null, value, null);
}
private static bool GetEventFiringSwitchValue()
{
PropertyInfo pi = EventManagerType.GetProperty(_eventFiringSwitchName, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
object val = pi.GetValue(null, null);
return (bool)val;
}
private static Type GetEventManagerType()
{
_eventManagerType = typeof(SPList).Assembly.GetType(_className, true);
return _eventManagerType;
}
}
}
//Method Add New Item of Calendar list
....
Bamboo.StartWorkflowHelper.StartWorkflowFunction.StartWorkflow(item); //item = SPListItem