1
votes

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

1 Answers

2
votes

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).