I thought I could reference SPContext when executing the timerjob function but I get "Instance reference is not set to an instance of an object". It fails when I reference SPContext.

What am I doing wrong? (I'm trying to update items in a list using a TimerJob)

Public Overrides Sub Execute(ByVal targetInstanceId As Guid)

   Dim oWeb As SPWeb = SPContext.Current.Web

   'Update PEEIT Employees List

End Sub

2 Answers 2


SPContext is not accessible in timer job. The SharePoint timer service is a windows service. So you should pass a site and web Ids(Urls) to the timer job and open new web.

using(SPSite site = new SPSite(siteId)){using(SPWeb web = site.OpenWeb(webId)) { //code }}

SPContext represents the context of a HTTPRequest (Similar to HTTPContext).

In the Execute method, SPWebApplication instance is available ; get the target site instance from the this.WebApplication.Sites collection.

URL : http://msdn.microsoft.com/en-us/library/cc406686%28v=office.12%29.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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