启动Sharepoint工作流程时出现问题

时间:2011-01-31 11:39:41

标签: sharepoint-2007 workflow-foundation

我有一个自定义工作流程,我通过代码启动。以下代码用于启动工作流程。

DeleteWorkflowTasks(properties.ListItem.ID);
                        Thread thread = new Thread(delegate() { StartApprovalWorkflow(); });
                        thread.Start();

private void StartApprovalWorkflow()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        try
                        {
                            using (SPSite elevatedSite = new SPSite(siteID))
                            {
                                using (SPWeb elevatedWeb = elevatedSite.OpenWeb(webID))
                                {

                                    SPList calendarList = elevatedWeb.Lists[listID];
SPWorkflowAssociation workflowAssociation = calendarList.WorkflowAssociations.GetAssociationByName(currentWorkflowName, System.Threading.Thread.CurrentThread.CurrentCulture);
                                    elevatedSite.WorkflowManager.StartWorkflow(itemBeforeUpdate, workflowAssociation, workflowAssociation.AssociationData);
                                }
                            }
                        }
                        catch (SPException ex)
                        {
                                                    }
                        catch (Exception ex)
                        {
                                                    }
                    });
            }
            catch (SPException ex)
            {

            }
            catch (Exception ex)
            {
            }
        }

此代码附有ItemUpdated事件处理程序代码。每当我更新项目时,它都会取消已附加的工作流程并为同一项目创建新的工作流程。虽然它工作正常然而有时工作流程尚未开始之前的工作流程正在取消。 我得到的堆栈跟踪如下。

Object reference not set to an instance of an object.   at Microsoft.SharePoint.Library.SPRequestInternalClass.OpenWeb(String bstrUrl, String& pbstrServerRelativeUrl, String& pbstrTitle, String& pbstrDescription, Guid& pguidID, String& pbstrRequestAccessEmail, UInt32& pwebVersion, Guid& pguidScopeId, UInt32& pnAuthorID, UInt32& pnLanguage, UInt32& pnLocale, UInt16& pnTimeZone, Boolean& bTime24, Int16& pnCollation, UInt32& pnCollationLCID, Int16& pnCalendarType, Int16& pnAdjustHijriDays, Int16& pnAltCalendarType, Boolean& pbShowWeeks, Int16& pnFirstWeekOfYear, UInt32& pnFirstDayOfWeek, Int16& pnWorkDays, Int16& pnWorkDayStartHour, Int16& pnWorkDayEndHour, Int16& pnMeetingCount, Int32& plFlags, Boolean& bConnectedToPortal, String& pbstrPortalUrl, String& pbstrPortalName, Int32& plWebTemplateId, Int16& pnProvisionConfig, String& pbstrDefaultTheme, String& pbstrDefaultThemeCSSUrl, String& pbstrAlternateCSSUrl, String& pbstrCustomizedCssFileList, String& pbstrCustomJSUrl, String& pbstrAlternateHeaderUrl, String& pbstrMasterUrl, String& pbstrCustomMasterUrl, String& pbstrSiteLogoUrl, String& pbstrSiteLogoDescription, Object& pvarUser, Boolean& pvarIsAuditor, UInt64& ppermMask, Boolean& bUserIsSiteAdmin, Boolean& bHasUniquePerm, Guid& pguidUserInfoListID, Guid& pguidUniqueNavParent, Int32& plSiteFlags, DateTime& pdtLastContentChange, DateTime& pdtLastSecurityChange, String& pbstrWelcomePage)
   at Microsoft.SharePoint.Library.SPRequest.OpenWeb(String bstrUrl, String& pbstrServerRelativeUrl, String& pbstrTitle, String& pbstrDescription, Guid& pguidID, String& pbstrRequestAccessEmail, UInt32& pwebVersion, Guid& pguidScopeId, UInt32& pnAuthorID, UInt32& pnLanguage, UInt32& pnLocale, UInt16& pnTimeZone, Boolean& bTime24, Int16& pnCollation, UInt32& pnCollationLCID, Int16& pnCalendarType, Int16& pnAdjustHijriDays, Int16& pnAltCalendarType, Boolean& pbShowWeeks, Int16& pnFirstWeekOfYear, UInt32& pnFirstDayOfWeek, Int16& pnWorkDays, Int16& pnWorkDayStartHour, Int16& pnWorkDayEndHour, Int16& pnMeetingCount, Int32& plFlags, Boolean& bConnectedToPortal, String& pbstrPortalUrl, String& pbstrPortalName, Int32& plWebTemplateId, Int16& pnProvisionConfig, String& pbstrDefaultTheme, String& pbstrDefaultThemeCSSUrl, String& pbstrAlternateCSSUrl, String& pbstrCustomizedCssFileList, String& pbstrCustomJSUrl, String& pbstrAlternateHeaderUrl, String& pbstrMasterUrl, String& pbstrCustomMasterUrl, String& pbstrSiteLogoUrl, String& pbstrSiteLogoDescription, Object& pvarUser, Boolean& pvarIsAuditor, UInt64& ppermMask, Boolean& bUserIsSiteAdmin, Boolean& bHasUniquePerm, Guid& pguidUserInfoListID, Guid& pguidUniqueNavParent, Int32& plSiteFlags, DateTime& pdtLastContentChange, DateTime& pdtLastSecurityChange, String& pbstrWelcomePage)
   at Microsoft.SharePoint.SPWeb.InitWeb()
   at Microsoft.SharePoint.SPWeb.get_UserInfoListId()
   at Microsoft.SharePoint.SPListItem.CalculateEffectivePermMask(SPBasePermissions permIn)
   at Microsoft.SharePoint.SPListItem.get_EffectiveBasePermissions()
   at Microsoft.SharePoint.SPListItem.DoesUserHavePermissions(SPBasePermissions permissionMask)
   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)

感谢您的任何帮助。

1 个答案:

答案 0 :(得分:2)

您在高级上下文中引用的复杂对象必须可在该上下文中创建

所以这个:

elevatedSite.WorkflowManager.StartWorkflow(itemBeforeUpdate, ...

需要改为这样的事情:

elevatedItem = calendarList.GetItemById(itemBeforeUpdate.Id)
elevatedSite.WorkflowManager.StartWorkflow(elevatedItem, ...