Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
share|improve this question
1  
from where are you calling this code ? from a page ? Is it called from a postback ? –  Steve B Jun 16 '11 at 10:47
    
wow - so L A R G E error message ;) –  Ryan Jun 16 '11 at 11:50
    
Why did my comments get deleted? hmmm –  Ryan Jun 21 '11 at 9:37
add comment

1 Answer 1

EDIT: moved from comments

I am working at Bamboo. I had fixed this issue and I wanna share all. I checked sharepoint logs and found out a message "System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Security.SecurityException: That assembly does not allow partially trusted callers." To fix this message, you insert "Unrestricted=true" property.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.