1

I am new in SharePoint and I want to know the difference between SPContext.Current.Site.Url, SPContext.Current.Web.Url and SPContext.Current.Site.RootWeb ?

Please help me to know the difference between them.

2 Answers 2

3

SPContext.Current.Site.Url --> It will give the root site URL i.e. site collection URL. SPContext.Current.Web.Url --> It will give the current web site url. SPContext.Current.Site.RootWeb --> An SPWeb object that represents the root Web site.

1

As you may be aware, the typical structure is a site collection with one or more sites (webs) underneath, spread across multiple levels/tiers.

Sometimes the term site is used interchangeably (verbally) to refer to any given website, but in SharePoint, site and web are different objects.

The root site of your site collection will be your site collection's root web.

e.g. if you are located in the root site, then Current Web and Root Web will be equivalent in that context.

You'll want to use Site or Web depending on what you want to do, since each has its own methods and properties, e.g. get the url of the site collection VS get the url of the specific (web)site.

I guess SPContext.Current.Web.Url is the most typical property that you can use to grab the url of the current site, similar to SPWeb.ServerRelativeUrl, which is allows you to get away without specifying the domain, so I'd use that instead if possible.

example:

http://contoso.com/sitea/siteb

tests using javascript console:

_spPageContextInfo.siteServerRelativeUrl --> "/"

_spPageContextInfo.webServerRelativeUrl --> "/sitea/siteb"

I guess this can be confusing at first, so I hope I did a decent job at explaining it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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