3

I'm creating a timer Job. And i have to access some lists which are in my solution stored in the site : "http://server:9090/sites/thesite"

For the moment, in my Timer Job i use this :

 SPWebApplication webApplication = this.Parent as SPWebApplication;
 SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];

 SPList ParametresTech = contentDb.Sites["sites/thesite"].RootWeb.Lists[Constantes.Listes.PARAMETRES_TECHNIQUES.Name];

The problem i'm facing here is that i'm in my development environnement, and i don't know what will be the url of the site they will use to deploy the solution in production.

So is there a way to get to the list without knowing the name of the site ?

Thanks

EDIT : That's how the timer job is activated :

public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        string ListJobName = "SAPToSQL";


        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 == ListJobName)
                job.Delete();
        }
        // install the job
        TimerJobSAPToSP listLoggerJob = new TimerJobSAPToSP(ListJobName, site.WebApplication);
        SPHourlySchedule schedule = new SPHourlySchedule();
        schedule.BeginMinute = 0;
        schedule.EndMinute = 59;
        listLoggerJob.Schedule = schedule;
        listLoggerJob.Update();
    }
Thoma Bigueres
  • 141
  • 1
  • 5
  • 14
  • There needs to be more information. If not URL, what distinguishes the production site? Is it based on a certain activated feature? Something else? Right now, your question could be summarized as "I don't know where to go, how can I get there?" – Rich Bennema Jun 18 '12 at 16:30
  • Yes the timer job is activated in a feature event receiver as shown in my edit :) – Thoma Bigueres Jun 18 '12 at 16:33

1 Answers1

2

I would definitely identify the site collection using the feature ID that creates the timer job rather than by URL. Not only does this give you flexibility in naming sites, it also allows you to process multiple site collections that have each subscribed to the job.

I wrote the following utility method to collect the site collections for a timer job:

public static List<Guid> GetSiteIDs(SPWebApplication webApplication, Guid featureId)
{
    List<Guid> ids = new List<Guid>();
    foreach (SPSite site in webApplication.Sites)
    {
        try
        {
            if (SPSite.Exists(new Uri(site.Url)) 
                && null != site.Features[featureId])
            {
                try
                {
                    ids.Add(site.ID);
                }
                catch (Exception ex)
                {
                    // Handle Exception
                }
            }
        }
        finally
        {
            site.Dispose();
        }
    }
    return ids;
}

In the featureId parameter, I pass a constant that I declare in my job definition class.

For more information see: Scope of a feature activated Custom Sharepoint-Timer Job

Community
  • 1
  • 1
Rich Bennema
  • 10,295
  • 4
  • 37
  • 58