21

I am fairly new to Sharepoint development (But learning it rapidly due to some initiatives I have in place - not exams).

Anyway, one of my biggest stumbling blocks is the naming convention for objects. That is, spweb, spsite, etc.

Is there an easy way to know which is for web apps, site collections, and subsites (Assuming that is how it works)?

Thanks

4 Answers 4

28

Depending on the kind of solution you are developing, you use different entry points into the object model to obtain the appropriate objects(SPWebApplication, SPSite or SPWeb). Below is the graphical representation of the SharePoint Server Object Model hierarchy.

enter image description here

Physically, SPWebApplication represents an IIS load-balanced website containing a custom web.config and a standard set of SharePoint specific files and virtual directories including the _layouts directory and the _controltemplates directory .

To get reference to target Web Application, you can use :

Uri webAppUrl = new Uri("http://sp2010site");
SPWebApplication webApp = SPWebApplication.Lookup(webAppUrl);

SPSite and SPWeb are fundamental types in the Server Object Model. They represent a site collection and a website, respectively.

By and large, To work with SharePoint from a browser-based application, your code must first establish the site context or site collection context for requests sent to the server. To return the current site collection, you can use :

SPSite site = SPContext.Current.Site;

To return the web site of the current request, you can use :

SPWeb web = SPContext.Current.Web;
16

The terminology difference between the object model and the SharePoint User Interface is one of the strangest stumbling blocks when learning about SharePoint's API.

For a quick reference:

  • SPSite is a site collection
  • SPWeb is an instance of a SharePoint Website (or "Site" from the UI), this includes subsites
  • SPWebApplication is an instance of an IIS Web Application that hosts SharePoint site collections
3
  • One of the weird things for me to understand is that a SPSite is a SPWeb (but a SPWeb is not a SPSite).
    – Kit Menke
    Nov 13, 2011 at 5:50
  • 1
    @KitMenke The names are simply shortened rather than swapped. SPSite is a Site Collection. An SPWeb is a Website. It is unfortunate that the UI calls these "Sites" when it adds so much confusion. Nov 13, 2011 at 8:13
  • See my answer for an attempt at an explanation :-|
    – SPDoctor
    Nov 13, 2011 at 13:18
7

This is a source of great confusion to newcomers to SharePoint and is due to a historical change in the naming convention between the 2003 and 2007 version of the products.

Unfortunately, in earlier versions the Site Collection was called a "Site" and the Site used to be called a "Web". Worse still, the SharePoint Object Model objects were named accordingly (SPSite and SPWeb) and couldn't be changed without breaking existing code. Tragically, there is also a class called SPSiteCollection which is a collection of SPSites and therefore a collection of site objects, and not a site collection object. Oh dear.

So you just have to remember when using the object model:

SPSite = site collection
SPWeb = site
SPSiteCollection = collection of site collections
SPWebCollection = collection of sites
2
  • I don't like using the term "site" even though technically it is the most correct. I try to use "site collection" and "sub site".
    – Kit Menke
    Nov 13, 2011 at 16:36
  • Yeah, it would have been better to avoid re-using the word "site" in the v3 naming. But root site and subsites are all SPWeb objects. It's a mess.
    – SPDoctor
    Nov 13, 2011 at 18:46
2

You can think of an SPSite is a container that contains a single SPWeb, the root web, or top-level web (SPSite.RootWeb). This web contains other SPWeb objects (SPWeb.Webs), but not SPSite objects (though it does have a reference to the parent site collection SPWeb.Site).

This isn't strictly correct, but a helpful mental reminder. Another way of thinking about it is that an SPSite is a collection of SPWeb objects (SPSite.AllWebs), and one of those (and only one) is the root web, or top-level site collection (SPSite.RootWeb).

Fundamentally, an SPSite contains metadata about the collection, and no content, and an SPWeb will contain both metadata and content (in Lists).

Your Answer

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

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