Friday, January 7, 2011

Using a Config File for Windows SharePoint Services Timer Jobs

One of the biggest problems I've had with timer jobs is that you cannot access the membership provider or role providers with them because they do not have a configuration value.  Then if you want to read from a configuration file, you have to point it to one of the web.configs of the site.
 
Not any more.  I was looking around for a way to add an app.config to the timer service and stumbled upon Jason Huh's blog post about using the Microsoft Enterprise Library with Timer Jobs.  I figured I would be able to use the same configuration for my projects.
 
All you need to do is create a config file for OWSTimer.  You can do this one of two ways, you can deploy this using a solution or you can do it manually. 
 
To do it manually, navigate to the hive (Program Files\Common Files\Microsoft Shared\web server extensions\12) and then open up the BIN folder.  Now create a new file named OwsTimer.exe.config.  In this file, you can place your config settings, this works just like an app.config or a web.config file that you would add in visual studio.
 
I've placed an example config file, but the Membership Provider and Role Provider nodes are not filled all of the way out, you can replace this with your own information or just remove the membership and role manager nodes completely.
 
xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="ConnString" connectionString="ConnectionStringGoesHere" />
connectionStrings>
<system.web>
<membership defaultProvider="MembershipProvider">
<providers>
<add name="MembershipProvider" />
providers>
membership>
<roleManager enabled="true" defaultProvider="RoleProvider">
<providers>
<add name="RoleProvider" />
providers>
roleManager>
system.web>
<appSettings>
<add key="SiteUrl" value="http://sharepointsite"/>
<add key="ProviderPrefix" value="RoleProvider"/>
appSettings>
configuration>
 
Using this, you will be able to finally use a configuration file with your timer services without having to hard code the site that you are going to read the config settings from.

No comments:

Post a Comment