Problem in starting Sharepoint workflow
I have a custom workflow that I am starting through code.The following code I am using to start a workflow.
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)
{
}
}
This code is attached with ItemUpdated eventhandler code.Whenever I update an item , it cancels the workflow which is already attached and creates a new workflow for the same item.Though it is working fine however sometimes the workflow is not getting started however the previous workflow is getting cancelled. The stack trace which I am getting is as follows.
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&开发者_JAVA百科amp; 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)
Appreciate any help on this.
Complex objects you reference inside an elevated context MUST be created inside that context
So this:
elevatedSite.WorkflowManager.StartWorkflow(itemBeforeUpdate, ...
Needs to be changed to something like this:
elevatedItem = calendarList.GetItemById(itemBeforeUpdate.Id)
elevatedSite.WorkflowManager.StartWorkflow(elevatedItem, ...
精彩评论