title: The security validation for this page is invalid error trying to add sharepoint approval workflow to List in ListAdded eventreceiver
body: <p>What I am trying to do is to attach the OOTB sharepoint workflow [Approval Sharepoint - 2010] to each and every document library that ever gets created. To accomplish this  I created a List Added event reciever and put this code in it - </p>
  
  <pre><code>public override void ListAdded(SPListEventProperties properties)
     {
         SPSecurity.RunWithElevatedPrivileges(delegate()
         {  
             SPUtility.ValidateFormDigest();
  
              using (SPSite site = new SPSite(properties.SiteId))
              {
                  using (SPWeb web = site.OpenWeb())
                  {
                      try
                      {
                          base.ListAdded(properties);
                          if (currentList is SPDocumentLibrary)
                          {
                              SPDocumentLibrary docLib = (SPDocumentLibrary)properties.List;
  
  
                              //workflows need a tasks and history list. Here we assume they exist
                              SPList taskList = web.Lists["Tasks"];
                              SPList historyList = web.Lists["Workflow History"];
  
                              //loop through the workfows in the web and grab the one we want by name
                              SPWorkflowTemplate wfTemp = null;
                              foreach (SPWorkflowTemplate wt in web.WorkflowTemplates)
                              {
                                  if (wt.Name == "Approval - SharePoint 2010")
                                  {
                                      wfTemp = wt;
                                      Common.AddToLog(web, "Found " + wt.Name + " in current web " +
                                      web.Url, false);
                                      break;
                                  }
                              }
  
                              //Now add the workflow to the doc library
                              SPWorkflowAssociation workFlow = SPWorkflowAssociation.CreateListAssociation(wfTemp, wfTemp.Name, taskList, historyList);
  
                              workFlow.AllowManual = true;
                              workFlow.AutoStartChange = false;
                              workFlow.AutoStartCreate = true;
                              workFlow.AssociationData = null;
  
                              web.AllowUnsafeUpdates = true;
                              web.ValidateFormDigest();
                              docLib.WorkflowAssociations.Add(workFlow);                                
  
                              docLib.EnableModeration = true;
  
                              docLib.Update();
                              web.Update();
                              web.AllowUnsafeUpdates = false;
                          }
                      }
                      catch (Exception ex)
                      {
                          throw ex;
                      }
                      finally
                      {
                          web.AllowUnsafeUpdates = false;
                      }
                  }
              }
         });
   }
  </code></pre>
  
  <p>I am getting this error- </p>
  
  <p>The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.</p>
  
  <p>at this line </p>
  
  <p><code>docLib.WorkflowAssociations.Add(workFlow);</code></p>
  
  <p>Any any have any suggestions please ? Thanks for your feedback.</p>