views:

60

answers:

1

Hey...

So I'm developing a custom timer job for sharepoint using the SPJobDefinition.

This job is activated through a feature receiver.

As I understand it the SPJobDefinition runs on the web-application.

if the feature scope is site or web based, is there any way code-wise to determine which site the feature is activated through in the Customer Job?

To get a clearer picture of what I'm trying to achieve, the job itself is a custom "Alert Me" notification job. What I'd like to do, is be able to determine which sites have activated this feature, so as to determine which sites to subscribe to the alerts notifications.

But I have no idea how to determine which sites have this feature activated.

Any help much appreciated.

+1  A: 

Below is code I've used to do just what you are describing:

    public override void Execute(Guid targetInstanceId)
    {
        foreach (SPSite site in this.WebApplication.Sites)
        {
            try
            {
                if (SPSite.Exists(new Uri(site.Url)) && null != site.Features[FeatureId.AlertMeJob])
                {
                    try
                    {
                        ExecuteJob(site);
                    }
                    catch (Exception ex)
                    {
                        // handle exception
                    }
                }
            }
            finally
            {
                site.Dispose();
            }
        }
    }

FeatureId.AlertMeJob is a GUID representing the Feature with the Feature Receiver that creates the job schedule (if it didn't already exist).

Rich Bennema
This looks excellent - thank you very much! One question though - is there an easy way of fetching the feature ID from within the custom job? Or do I have to save the id somewhere, and then read it in?
Dynde
nvm... I ended up throwing it in the timer job's constructor, and then saving in the web. property bag... not a particularly clean way of doing it I know...
Dynde