1

I have a root site list which I am displaying on several sub-sites using CQWP based on a custom filter and entering the list values from sub-sites itself to the root site list.

So I am trying to store the sub-sites url when saving values using default newform.aspx on the list column. I tried storing url value using workflow,event receiver but not able to store as required. I can achieve this using custom WebPart but don't want this approach. Using WebPart I get the previous visited URL but in event receiver value is null.

Below is the code for getting URL:

HttpContext.Current.Request.UrlReferrer.AbsolutePath

Any solutions would be helpful here. Thanks!

1

To take the parent Web URL you have to use this:

   public override void ItemAdded(SPItemEventProperties properties)
        {
           using (SPSite site = new SPSite(properties.SiteId))
           {
                using (web = site.OpenWeb(properties.RelativeWebUrl))
                {
                   var parentURL= web.ParentWeb.Url.ToString();
                }
           }
        }
11
  • yes this doesn't work in event receiver when i fire it on ItemAdded event Sep 16 '14 at 8:12
  • try this: properties.RelativeWebUrl
    – Nk SP
    Sep 16 '14 at 8:13
  • this gives me the current url. but i want the parent one. Sep 16 '14 at 8:15
  • answer edited .
    – Nk SP
    Sep 16 '14 at 8:20
  • HttpContext.Current.Request.UrlReferrer.AbsoluteUri.ToString() this gives me previous URL right, but in event receiver this returns null. Sep 16 '14 at 8:20
1

The root problem is that you need an information of the HTTP context from the inside of an ER (i.e. the UrlReferrer). HTTP context information is stored at the thread level... but if you use an asynchronous ItemAdded event, you run in a separate thread.

Thus, one option may be to use ItemAdding or a synchronous ItemAdded.
However, in case an item is added through another mean than the UI (PowerShell, timer job, C# server-side OM...), the HTTP context will still be null...
From a functionnality perspective, what do you intend to do if this is the case? From a technical point of view, you must, at least, test whether HttpContext.Current is null or not.

1
  • One important thing I omitted: what is the absolute path of the page newform.aspx when opened from the sub-site? If this form comes from the root site, the UrlReferrer will be that path... Ruining all your efforts to get the original sub-site URL...
    – Evariste
    Jul 29 '15 at 6:30
1

As I known, the HttpContext.Current only available on synchronous event like ItemAdding, ItemUpdating and ItemDeleting.
Can you try HttpContext.Current.Request.UrlReferrer.AbsolutePath on ItemAdding ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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