I am trying to get the top lever site URL in my timer job. Right now, I am using SPContext.Current.Site.Url which is giving an error. Please let me know how do I get the top level Site URL being in a ListItem. I have the code as follows

if (item[new Guid(Fields.ThumbnailFieldId)] != null)
            SPFieldUrlValue thumbnail = new SPFieldUrlValue(item[new Guid(Fields.ThumbnailFieldId)].ToString());                
            string tempURL = thumbnail.Url.Replace("http://intranet.swlstg-test.nhs.uk", SPContext.Current.Site.Url.ToString());                    
            this.ThumbnailUrl = tempURL.ToString();              

I have my newsitem is in the following I am trying to change the thumbnail.Url to SPContext.current.site.url alternative.

public NewsItemForTimerJob(SPListItem item)
        // Get item details
        this.Title = item[SPBuiltInFieldId.Title].ToString();
        this.StartDate = (DateTime)item[new Guid(Fields.StartDateFieldId)];
        this.EndDate = (DateTime)item[new Guid(Fields.EndDateFieldId)];
        this.ContentType = item.ContentType.Name;
        this.Headline = item[new Guid(Fields.HeadlineFieldId)].ToString();
        this.NewsBody = item[new Guid(Fields.MainBody)].ToString();
        this.Location = SPHelper.GetTaxonomyFieldValue(item, new Guid(Fields.Location), ", ");
        this.CategoryList = SPHelper.GetTaxonomyFieldValuesList(item, new Guid(Fields.NewsCategory));
        this.TagList = SPHelper.GetTaxonomyFieldValuesList(item, new Guid(Fields.Tags));
        this.Type = item.ContentTypeId.ToString().StartsWith(ContentTypes.NewsBase_ID, StringComparison.CurrentCultureIgnoreCase) ? NewsType.News : NewsType.Events;                         
        // Construct the URLs needed
        this.MoreUrl = string.Format("{0}{1}?lstId={2}&itmId={3}&m={4}&y={5}&ct={6}",
                                ((Type == NewsType.News) ? PageConstants.NewsDetailPage_Url : PageConstants.EventDetailPage_Url),
        this.ArchiveBrowserUrl = string.Format("{0}{1}?m={2}&y={3}&ct={4}",
                                ((Type == NewsType.News) ? PageConstants.NewsTimelinePage_Url : PageConstants.EventsTimelinePage_Url),
        // Get the image - if missing get 'no image'
        if (item[new Guid(Fields.ThumbnailFieldId)] != null)
            SPFieldUrlValue thumbnail = new SPFieldUrlValue(item[new Guid(Fields.ThumbnailFieldId)].ToString());
            this.ThumbnailUrl = thumbnail.Url;
            this.ThumbnailUrl = SPUtility.ConcatUrls(item.ParentList.ParentWeb.ServerRelativeUrl, CommonConstants.Default_Img_Url);
  • webApp.GetResponseUri(SPUrlZone.Default); thats your answer
    – Ali Jafer
    Commented Apr 4, 2013 at 11:31
  • @Ram, show me the code where you are calling NewsItemForTimerJob Commented Apr 4, 2013 at 15:16
  • Have I answered your question adequately? If so, could you please mark it as the correct answer?
    – Kristopher
    Commented Nov 12, 2013 at 20:06

3 Answers 3


Try this in your event receiver:

var web = properties.Web;    

HttpRequest request = new HttpRequest(string.Empty, web.Url, string.Empty);

HttpResponse response = new HttpResponse(new System.IO.StreamWriter(new System.IO.MemoryStream()));

HttpContext impersonatedContext = new HttpContext(request, response);

impersonatedContext.Items["HttpHandlerSPWeb"] = web;

HttpContext.Current = impersonatedContext;

SPContext context = SPContext.GetContext(impersonatedContext);

You should be able to get your SPContext from that.


If your timer job is web application scoped try this,


SPContext will be null in your case scenario, even in above example you will get sites with index "0" in web application timer job is running which might not what you want.

Check this useful links out,

http://msdn.microsoft.com/en-us/library/ms468609(v=office.14).aspx http://msdn.microsoft.com/en-us/library/ms462161(v=office.14).aspx https://stackoverflow.com/questions/4561992/how-to-get-all-site-collections-in-a-web-application

  • I am afraid; I dont get the scope of "this". Intellisense does not give me as I am trying to replace the spcontext.current.site.url.
    – Ram
    Commented Apr 4, 2013 at 11:20
  • Where are you trying to add this code Commented Apr 4, 2013 at 11:48

Try this

public override void Execute(Guid contentDbId)
SPWebApplication webApplication = this.Parent as SPWebApplication;
using (SPSite site = new SPSite(webApplication.Sites[0].Url))

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.