locked
Differences Between SPContext.Current.Site.Url and SpContext.Current.Web.Url RRS feed

  • Question

  • Folks,

    I have a webpart that reads from  some custom lists

    As the webpart and lists are in a subsite with a path like

    is http://Website:8080/Areas/Site

    Do I use is SPContext.Current.Site.Url  or SpContext.Current.Web.Url to access the lists?

    Thanks


    • Edited by [Mick] Thursday, January 12, 2012 1:03 PM misspell
    Thursday, January 12, 2012 1:03 PM

Answers

  • In your case using SPContext.Current.Web.Url will return http://Website:8080/Areas/Site when your webpart is opened from /Areas/Site,

    If its opened from /Areas it will be http://WebSite:8080/Areas/Site


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010
    Thursday, January 12, 2012 2:35 PM
  • Hi,

         SPContext.Current.Web.Url will return http://Website:8080/Areas/Site in your environment.

    Please let me if it helps!!!!


    Ajeet
    • Marked as answer by [Mick] Friday, January 13, 2012 8:29 AM
    Friday, January 13, 2012 5:03 AM

All replies

  • You need to use SPContext.Current.Web.Url to access the list in the current web.

    In the URL you gave, SPSite is at 'http://Website:8080'

     


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010
    Thursday, January 12, 2012 1:10 PM
  • Hi,

         You should use SPContext.Current.Web.Url as it gives you the current context web url to get the url for the web in the current context. SPContext.Current.Site.Url will give the root site url ie. http://Website:8080 event if you are in the subsite http://Website:8080/Areas/Site and when you try to get the list your code will refer to root site to get the list and thus you can get exception.

    You can also use http://Website:8080 but while opening the web object you have to specify the web name which you want to open for eg:

    using(SPSite oSite=new SPSite("http://server:8080"))
    {
       using(SPWeb oWeb=oSite.AllWeb["NameOrIDofTheWeb"])
         {
            //Your Code here
         }
    }
    



    Ajeet
    Thursday, January 12, 2012 1:11 PM
  • Thanks Guys, so If I want to get a list at http://Website:8080/Areas/Site

    I currently do something in the codebehind like

    BaseUrl =

    SPContext

    .Current.Site.Url;

    then in the UI 

    ('

     

    <%=BaseUrl %>/Lists/Country/Item/newifs.aspx');

    What should I use to get the full path to

    http://Website:8080/Areas/Site

    Can I not use SPContext.Current to work out the current subsite I am executing the webpart under?

     

     

     

     

    Thursday, January 12, 2012 1:21 PM
  • Hi,

         You should use SPContext.Current.Web.Url to get the full url of the subsite you are in the current context.

     

    Please let me know if it helps!!!


    Ajeet
    Thursday, January 12, 2012 1:27 PM
  • Thanks Ajeet

    So in the case SPContext.Current.Web.Url when the webpart is running under http://Website:8080/Areas/Site will give me http://Website:8080/Areas/Site?

    In my dev environment my site sites at the root so I cannot really test this out outside of production you see, so i need to be sure before the powershell install.

    Thursday, January 12, 2012 1:34 PM
  • check this,

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.aspx


    Anil

    Anil, that tells me nothing about running under subsite
    Thursday, January 12, 2012 1:38 PM
  • In your case using SPContext.Current.Web.Url will return http://Website:8080/Areas/Site when your webpart is opened from /Areas/Site,

    If its opened from /Areas it will be http://WebSite:8080/Areas/Site


    Ram Prasad Meenavalli | MCTS SharePoint 2010 | MCPD SharePoint 2010
    Thursday, January 12, 2012 2:35 PM
  • Hi,

         SPContext.Current.Web.Url will return http://Website:8080/Areas/Site in your environment.

    Please let me if it helps!!!!


    Ajeet
    • Marked as answer by [Mick] Friday, January 13, 2012 8:29 AM
    Friday, January 13, 2012 5:03 AM
  • Yep this worked . Thanks!
    Friday, January 13, 2012 8:30 AM