none
difference between SpSite and SpWeb

    Question

  •  

    hello ,

     

    I had an interview and I  was Asked thsi Question :

     

     

       What is the difference between  SpSite and SpWeb  ?

     

        and which one in the SiteCollection ?

     

     

      thanks

     

     

    Sunday, May 11, 2008 10:40 AM

Answers

  • The problem lies with the difference in terms used between the SharePoint user-interface and the object model. When we are speaking to end-users, they are familiar with the term "creating SharePoint team sites". However, everytime you create a "site" from the UI, an SPWeb object was created.

    It doesn't help that the MSDN documentation uses the following type of naming conventions in their code samples:

    SPSite mySiteCollection = new SPSite("http://servername/"); 
    SPWeb mySite = mySiteCollection.AllWebs["Site_Name"]; 
    SPWeb myRootSite = mySiteCollection.RootWeb;

    For me, it would have been much clearer if they wrote something like this instead:

    SPSite mySite = new SPSite("http://servername/"); 
    SPWeb myWeb = mySite.AllWebs["Site_Name"]; 
    SPWeb myRootWeb = mySite.RootWeb;

    While the description for SPSite may be valid, a typical SharePoint application web site only has onetop-level site collection. Therefore, in this url http://servername/site1/site1a/, there are actually three SPWeb objects, and only one SPSite object.

    SPSite site1 = new SPSite("http://servername/site1/");  
    SPSite site1a = new SPSite("http://servername/site1/site1a/");
    bool sitesAreEqual = site1.Equals(site1a);  // sitesAreEqual should evaluate to true.

    SPWeb web1 = site1.OpenWeb();
    SPWeb web1a = site1a.OpenWeb();
    SPWeb rootWeb = site1.RootWeb; // or site1a.RootWeb; would return same instance.

    See the SPSite.OpenWeb() method for more examples: http://msdn2.microsoft.com/en-us/library/ms474633.aspx.

    SPSite itself is not a "collection" in the sense that comes from CollectionBase, for example. In fact, both SPSite and SPWeb inherit directly from System.Object. Therefore, to use a name such as mySiteCollection is confusing/misleading.

    The key thing to keep in mind is that as developers, we need to use the technically accurate terms when speaking with each other (even if MSDN doesn't), and reserve the marketing/UI terms when speaking with people who would never see the object model. (Note: there are examples of these kinds of terminology differences all over the place in SharePoint 2007.)

    I hope this clears things up a bit...if you find something that is not correct/accurate, please let me know!


    some borns good, others make good
    Sunday, September 14, 2008 9:30 AM

All replies

  • Hello,
    The SPSite is the site collection and the SPWeb is a web site.
    This is a very fundamental question and if you intend on working with moss, I suggest openning msdn, buying a book, and doing some labs.
    Sunday, May 11, 2008 1:47 PM
  • The problem lies with the difference in terms used between the SharePoint user-interface and the object model. When we are speaking to end-users, they are familiar with the term "creating SharePoint team sites". However, everytime you create a "site" from the UI, an SPWeb object was created.

    It doesn't help that the MSDN documentation uses the following type of naming conventions in their code samples:

    SPSite mySiteCollection = new SPSite("http://servername/"); 
    SPWeb mySite = mySiteCollection.AllWebs["Site_Name"]; 
    SPWeb myRootSite = mySiteCollection.RootWeb;

    For me, it would have been much clearer if they wrote something like this instead:

    SPSite mySite = new SPSite("http://servername/"); 
    SPWeb myWeb = mySite.AllWebs["Site_Name"]; 
    SPWeb myRootWeb = mySite.RootWeb;

    While the description for SPSite may be valid, a typical SharePoint application web site only has onetop-level site collection. Therefore, in this url http://servername/site1/site1a/, there are actually three SPWeb objects, and only one SPSite object.

    SPSite site1 = new SPSite("http://servername/site1/");  
    SPSite site1a = new SPSite("http://servername/site1/site1a/");
    bool sitesAreEqual = site1.Equals(site1a);  // sitesAreEqual should evaluate to true.

    SPWeb web1 = site1.OpenWeb();
    SPWeb web1a = site1a.OpenWeb();
    SPWeb rootWeb = site1.RootWeb; // or site1a.RootWeb; would return same instance.

    See the SPSite.OpenWeb() method for more examples: http://msdn2.microsoft.com/en-us/library/ms474633.aspx.

    SPSite itself is not a "collection" in the sense that comes from CollectionBase, for example. In fact, both SPSite and SPWeb inherit directly from System.Object. Therefore, to use a name such as mySiteCollection is confusing/misleading.

    The key thing to keep in mind is that as developers, we need to use the technically accurate terms when speaking with each other (even if MSDN doesn't), and reserve the marketing/UI terms when speaking with people who would never see the object model. (Note: there are examples of these kinds of terminology differences all over the place in SharePoint 2007.)

    I hope this clears things up a bit...if you find something that is not correct/accurate, please let me know!


    some borns good, others make good
    Sunday, September 14, 2008 9:30 AM
  • SPSite represents the site collection having one top level site and sub webs. SPWeb is the web site.

    Jeetlal Prasad

    • Proposed as answer by Jeetlal Prasad Wednesday, January 15, 2014 1:04 AM
    Wednesday, January 15, 2014 1:04 AM