0

我想要做的是将 OOTB 共享点工作流 [Approval Sharepoint - 2010] 附加到每个创建的文档库中。为此,我创建了一个列表添加事件接收器并将此代码放入其中 -

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;
                    }
                }
            }
       });
 }

我收到这个错误-

此页面的安全验证无效。在您的 Web 浏览器中单击返回,刷新页面,然后再次尝试您的操作。

在这条线上

docLib.WorkflowAssociations.Add(workFlow);

请问大家有什么建议吗?感谢您的反馈意见。

4

2 回答 2

1

为什么你需要“SPUtility.ValidateFormDigest()”?

您的代码在事件接收器中运行,而不是在表单或 aspx 页面上,没有什么可以验证的。

你能把这条线去掉再试一次吗?

于 2013-04-24T00:44:21.033 回答
0

I believe updating this code block:

web.AllowUnsafeUpdates = true;
web.ValidateFormDigest();
docLib.WorkflowAssociations.Add(workFlow);

docLib.EnableModeration = true;

docLib.Update();
web.Update();
web.AllowUnsafeUpdates = false;

and replacing it with:

web.Site.WebApplication.FormDigestSettings.Enabled = false;
docLib.WorkflowAssociations.Add(workFlow);
docLib.EnableModeration = true;
docLib.Update();
web.Update();
web.Site.WebApplication.FormDigestSettings.Enabled = true;

Let me know if this works for you or if you still encounter the same error.

于 2013-04-23T19:28:22.890 回答