2

I recently uploaded a timer job that's purpose is to kick off a workflow on items in a custom List. Upon trying to activate it, I am immediately redirected to an error page with a Correlation ID.

Through the error log, I've found that this is because this line in the EventReceiver is returning null.

Dim site As SPSite = TryCast(properties.Feature.Parent, SPSite)

Am I assigning this variable? What would cause a null pointer?

When I add a null check then I can activate the feature, but DeleteJob(site) and CreateJob(site) do not get called.

Here is the code and the error in the log.

Part of the log

09/01/2015 09:10:23.17 w3wp.exe (0x168C4)                                      0x17104                SharePoint Foundation                 Feature Infrastructure                    88jm      High       Feature receiver assembly 'TimerJobToStartWorkflow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=58ee6ce408cfa59d', class 'TimerJobToStartWorkflow.TimerJobToStartWorkflow.Feature1EventReceiver', method 'FeatureActivated' for feature '1b9b6e17-b98f-4908-b61b-25fe6d2119f6' threw an exception: System.NullReferenceException: Object reference not set to an instance of an object.     at TimerJobToStartWorkflow.TimerJobToStartWorkflow.Feature1EventReceiver.DeleteJob(SPSite site)     at TimerJobToStartWorkflow.TimerJobToStartWorkflow.Feature1EventReceiver.FeatureActivated(SPFeatureReceiverProperties properties)     at Microsoft.SharePoint.SPFeature.DoActivationCallout(Boolean fActivate, Boolean fForce)            b303b055-ead8-467d-bf76-2af568b3d06e
09/01/2015 09:10:23.17 w3wp.exe (0x168C4)                                      0x17104                SharePoint Foundation                 General                                       72by      High       Feature Activation: Threw an exception, attempting to roll back.  Feature 'TimerJobToStartWorkflow_Feature1' (ID: '1b9b6e17-b98f-4908-b61b-25fe6d2119f6').  Exception: System.NullReferenceException: Object reference not set to an instance of an object.     at TimerJobToStartWorkflow.TimerJobToStartWorkflow.Feature1EventReceiver.DeleteJob(SPSite site)     at TimerJobToStartWorkflow.TimerJobToStartWorkflow.Feature1EventReceiver.FeatureActivated(SPFeatureReceiverProperties properties)     at Microsoft.SharePoint.SPFeature.DoActivationCallout(Boolean fActivate, Boolean fForce)     at Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, Boolean fForce)                b303b055-ead8-467d-bf76-2af568b3d06e

Feature.xml

<?xml version="1.0" encoding="utf-8"?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Title="TimerJobToStartWorkflow Feature1" Id="1b9b6e17-b98f-4908-b61b-25fe6d2119f6" ReceiverAssembly="TimerJobToStartWorkflow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=58ee6ce408cfa59d" ReceiverClass="TimerJobToStartWorkflow.TimerJobToStartWorkflow.Feature1EventReceiver" Scope="Web" />

Event Reciever

Option Explicit On
Option Strict On
Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Security
Imports Microsoft.SharePoint.Administration
''' <summary>
''' This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
''' </summary>
''' <remarks>
''' The GUID attached to this class may be used during packaging and should not be modified.
''' </remarks>
Namespace TimerJobToStartWorkflow
    <GuidAttribute("78a31cd8-fe3a-4bd1-9ee4-069415c6a0fd")> _
    Public Class Feature1EventReceiver
        Inherits SPFeatureReceiver
        Const List_JOB_NAME As String = "Start Workflow Timer Job"
        ' Uncomment the method below to handle the event raised after a feature has been activated.

        Public Overrides Sub FeatureActivated(properties As SPFeatureReceiverProperties)
            Dim site As SPSite = TryCast(properties.Feature.Parent, SPSite)
            DeleteJob(site)
            CreateJob(site)
        End Sub
        Private Shared Sub DeleteJob(site As SPSite)
            For Each job As SPJobDefinition In site.WebApplication.JobDefinitions
                If job.Name = List_JOB_NAME Then
                    job.Delete()
                End If
            Next
        End Sub
        Private Shared Sub CreateJob(site As SPSite)
            Dim job As New StartWorkflow(List_JOB_NAME, site.WebApplication)
            Dim schedule As New SPMinuteSchedule()
            schedule.BeginSecond = 0
            schedule.EndSecond = 1
            schedule.Interval = 5
            job.Schedule = schedule
            job.Update()
        End Sub

        ' Uncomment the method below to handle the event raised before a feature is deactivated.

        Public Overrides Sub FeatureDeactivating(properties As SPFeatureReceiverProperties)
            Dim site As SPSite = TryCast(properties.Feature.Parent, SPSite)
            For Each job As SPJobDefinition In site.WebApplication.JobDefinitions
                If job.Name = List_JOB_NAME Then
                    job.Delete()
                End If
            Next

        End Sub
        ' Uncomment the method below to handle the event raised after a feature has been installed.

        'Public Overrides Sub FeatureInstalled(ByVal properties As SPFeatureReceiverProperties)
        'End Sub


        ' Uncomment the method below to handle the event raised before a feature is uninstalled.

        'Public Overrides Sub FeatureUninstalling(ByVal properties As SPFeatureReceiverProperties)
        'End Sub

        ' Uncomment the method below to handle the event raised when a feature is upgrading.

        'Public Overrides Sub FeatureUpgrading(ByVal properties As Microsoft.SharePoint.SPFeatureReceiverProperties, ByVal upgradeActionName As String, ByVal parameters As System.Collections.Generic.IDictionary(Of String, String))
        'End Sub

    End Class
End Namespace

StartWorkflow.vb

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration
Imports Microsoft.SharePoint.Workflow

Namespace TimerJobToStartWorkflow
    Class StartWorkflow
        Inherits SPJobDefinition
        Public Sub New()
            MyBase.New()

        End Sub

        Public Sub New(jobName As String, service As SPService, server As SPServer, targetType As SPJobLockType)
            MyBase.New(jobName, service, server, targetType)

        End Sub

        Public Sub New(jobName As String, webApplication As SPWebApplication)
            MyBase.New(jobName, webApplication, Nothing, SPJobLockType.ContentDatabase)
            Me.Title = "Start Workflow Timer Job"

        End Sub


        Public Overrides Sub Execute(contentDbId As Guid)
            Dim site As New SPSite("https://portal.com/it/default.aspx")
            Dim web As SPWeb = site.OpenWeb()
            Dim manager As SPWorkflowManager = site.WorkflowManager
            Dim list As SPList = web.Lists.TryGetList("Certificate Tracking")
            If list IsNot Nothing Then
                Dim association As SPWorkflowAssociation = list.WorkflowAssociations(New Guid("342718f7-aefa-4812-bb6f-738fd8459753"))
                Dim items = list.Items
                For Each item As SPListItem In items
                    Dim workflows = item.Workflows
                    Dim isRunning As Boolean = False
                    For Each workflow As SPWorkflow In workflows
                        If workflow.AssociationId = association.Id AndAlso workflow.InternalState = SPWorkflowState.Running Then
                            isRunning = True
                        End If
                        If workflow.AssociationId = association.Id AndAlso workflow.InternalState <> SPWorkflowState.Running Then
                            isRunning = True
                        End If
                        Exit For
                    Next
                If isRunning = False Then
                        manager.StartWorkflow(item, association, association.AssociationData)
                    End If
                Next
            End If
        End Sub
    End Class
End Namespace
2
+50

Your feature is scoped as web:

Scope="Web"

But your are working with a site in the activate event of the feature:

Dim site As SPSite = TryCast(properties.Feature.Parent, SPSite)

Try changing the scope of the feature or use in SPWeb in the code.

| improve this answer | |
1

You are using scope Web so you would get SPWeb not SPSite.
You can try following to get site:

Dim web As SPWeb = TryCast(properties.Feature.Parent, SPWeb)
Dim site As SPSite = web.Site
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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