Question

What is difference between:

SPSite.Url 

SPSite.RootWeb.Url

If both are same, Which is considered to be good practice?

Was it helpful?

Solution

I don't believe there is a difference in terms of what they return back (the string representation of the root URL of the site).

However, in the second case you are referencing both an SPSite and SPWeb object whereas in the first you are only referencing the SPSite object (so there might be some minor performance considerations if all you are looking for is the URL to the site).

In addition I would think the the context of the code you are writing would favor one over the other (e.g. if you have code that is scoped at the site level it would make more sense to just get the SPSite object Url; if your code was scoped at the web level (for example, doing something to the root SPWeb object and then traversing down to subsequent children webs) then referencing the rootweb.url would make more consistent sense to me).

In both cases you would need to make sure you are following proper disposal practices: http://msdn.microsoft.com/en-us/library/ms468609.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top