I'm writing a timer job that needs to email document owners when a document column (review Date=today) I want features to be available in sites which I can activate site collection.

How do I ensure that the site where I activate the feature is the site that gets iterated. I've done this in the past but used absolute url's

i.e. SPSite site = webApplication.Sites[http];
However I dont want to give any specific urls as I want one feature available on numerous sites in a single site collection.

Could someone help with method to use and getting a reference for the site where activated and iterating list in that site pls? I found the following pieces of code:

public override void Execute(Guid contentDbId)
 // 1 here }


public override void Execute(Guid targetInstanceId)

 // 2 here }

Code found to iterate a list https://stackoverflow.com/questions/965695/iterate-a-sharepoint-list

public override void FeatureActivated(SPFeatureReceiverProperties properties)
     using(SPSite site = properties.Feature.Parent as SPSite)
         SPList list = site.RootWeb.Lists["ListName"];
         SPListItemCollection items = list.Items;

        foreach (SPListItem listItem in items)
             Response.Write(SPEncode.HtmlEncode(listItem["Url"].ToString()) +"
"); } } }

I also have a feature in the project (can this be used to gather the path of activated site?

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
            SPSite site = properties.Feature.Parent as SPSite;

            // make sure the job isn't already registered

            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                if (job.Name == List_JOB_NAME1)

            // install the job

            ReviewDocument ListTimerJob1 = new ReviewDocument("ReviewDate", site.WebApplication);

1 Answer 1


You could create a second Site scoped feature that sets a SPSite.Properties value which tells the timer job that it can work on that specific site collection.

Then your application scoped timer job can iterate through all site collections recursively until it finds a site collection with that flag on it.

  • I'm happy to activate feature manually on sites that require feature. I dont know how to reference in code the local site. Commented Nov 26, 2013 at 13:19
  • Right, timer jobs are farm or application scoped only, meaning that you can work on all site collections in an application or hardcode in one. OR you could set a flag on the ones you actually want to work on. I missed the site collection part of your answer. Will edit. Commented Nov 26, 2013 at 13:22
  • All I want to do is iterate libraries in a site that has the feature activated but dont know how to code this class and reference the current site. I have updated question and included part of the feature in a hope I can grab hold of the reference to current site. Commented Nov 26, 2013 at 14:12
  • Yes, but timer jobs can only target applications or farms. This means you have to tell the timer job what site to work on. You can either hard code a site url or a site name into the timer job logic, or you can set a flag via a second feature to tell the timer job what site to work on. that would allow you to simply activate the application scoped feature, then activate the site/web scoped feature. Commented Nov 26, 2013 at 14:17
  • I see. In that case is it possible to add feature to root site collection and then iterate all libraries in subsites. This way i dont need to have a seperate feature per web? Commented Nov 26, 2013 at 16:08

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.