1

What I want to do is to create a timer which is enabled on the Event Receiver FeatureActivated. I want to use this timer to execute once a day to check some items in a list and update the status for the items.

I can create and enable the timer - no problem. But when I try to access the SPSite object to get the list items I get the following error:

The sharepoint subset OM has been disabled for the current thread.

The timer is running in it's separate thread which causes the problem.

My question is: how do I access the SPSite object to get access to the list items from a separate thread?

Note: I'm using a timer because I can't use Timer Jobs in a Sandbox solution. Thank you.

Gray
  • 115,027
  • 24
  • 293
  • 354
Høgsdal
  • 395
  • 4
  • 21
  • What do you mean by the *timer*? An instance of a class like [Threading.Timer](http://msdn.microsoft.com/en-us/library/system.threading.timer(v=vs.80).aspx) or [Timers.Timer](http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.80).aspx)? – Ferdinand Prantl Apr 13 '12 at 09:21
  • What code do you use to get the SPSite object and what is you site collection URL? – Ferdinand Prantl Apr 13 '12 at 09:26
  • Yes an instance of the class Timers.Timer. I create the SPSite object like this: `using (SPSite siteCollection = new SPSite("http://localhost:1548")) { web = siteCollection.OpenWeb(); SPListItemCollection listItems = myWeb.Lists["Configuration"].Items; }` – Høgsdal Apr 16 '12 at 05:46

1 Answers1

1

I am afraid that your approach to run a task in the background is not possible. I can confirm that you cannot access the SP OM in an asynchronous delegate or run on a separated thread. I guess it is because the resource usage in sandboxed solutions is monitored and throttled. For example, you are not allowed to leave objects without disposing them too.

Not only you are disallowed to create an SPSite object with either string or Guid; you cannot pass the (SPSite)properties.Feature.Parent object that is available in the event receiver to the delegate too. (All properties of the object will throw the same exception.)

Generally I think that your solution would be too fragile. In a traditional solution, your code would be running in the w3wp.exe process and the first application pool recycle after activating your feature would kill the timer (with the entire application domain). Similarly, in a sandboxed solution you would be at the mercy of SPUCWorkerProcess.exe. You need some service host that will execute your code every day reliably.

Sandboxed solutions and/or SharePoint Online do not offer functionality for background-running services. You would have to run your code somewhere else, for example:

  1. If you are allowed to deploy at least a small farm solution you can deploy the list item updating code as a SP timer job.

  2. If you can afford running a Windows machine all the time you can write the list item refreshing code using the SP Client OM and install either as a Windows Service (timer loop) or as a Windows Scheduled Task (timed start). (The latter is simpler.)

  3. If your solution is supposed to run in SharePoint Online and you cannot rely on a local Windows machine you can deploy it as a hosted service (Worker Role) in Microsoft Azure.

  4. You can implement the regular updating functionality to run on demand in the sandboxed solution - as an ASPX page, for example. Then you would trigger the URL of the resource executing the code by public services like pingler or webcron.

Actually, the options 1 - 3 can be written the same way as the option 4 - the code to execute regularly within the sandboxed solution and the triggering code (very short and SP-independent) separately according to the scheduler's nature - it can be even a perl script using LWP scheduled by cron.

--- Ferda

Community
  • 1
  • 1
Ferdinand Prantl
  • 5,281
  • 34
  • 35
  • Thank for your answer. The solution I'm developing has to be created as a Sandbox Solution and have to run on Sharepoint Online and I won't be able to access any of the data after installed. – Høgsdal Apr 18 '12 at 00:30