SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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("", 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 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);
share|improve this question
webApp.GetResponseUri(SPUrlZone.Default); thats your answer – ali Sharepoint Apr 4 '13 at 11:31
@Ram, show me the code where you are calling NewsItemForTimerJob – Muhammad Raja Apr 4 '13 at 15:16
Have I answered your question adequately? If so, could you please mark it as the correct answer? – Kristopher Nov 12 '13 at 20:06

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.

share|improve this answer

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,

share|improve this answer
I am afraid; I dont get the scope of "this". Intellisense does not give me as I am trying to replace the – Ram Apr 4 '13 at 11:20
Where are you trying to add this code – Muhammad Raja Apr 4 '13 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))

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.