2

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
   oWeb.Site.OpenWeb("http://dept.litwareinc.com/npp/NPE")

   'Update PEEIT Employees List
   UpdatePEEITEmployees(oWeb)

End Sub
0

2 Answers 2

3

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 }}
0

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.