Home > Uncategorized > The beauty of using SPWeb in conjunction with the SPSite constructor

The beauty of using SPWeb in conjunction with the SPSite constructor

November 25th, 2006 Leave a comment Go to comments

I have been using SPSite and SPWeb classes for a long time to retrieve information from SharePoint. I think for security reason the entry point from outside to a site is ONLY available through the site collection to which that site belongs to. In another word, if you need to retrieve a SPWeb instance for a subsite, you cannot use the following statement:


SPWeb myWeb = new SPWeb (“http://Server_Name/sites/HR/Candidates/default.aspx“);


Instead you should use something like this:


SPSite siteCollection = new SPSite (“http://Server_Name/sites/HR/Candidates/default.aspx“);
SPWeb site = siteCollection.OpenWeb(); <– You will get an SPWeb object representing Candidates subsite


It is nice, isn’t it?


You specify your FULL subsite URL in site collection and you end up getting the SPWeb object representing your subsite in the second line. Yes, SPSite and SPWeb are smart enough to return the lowest-level site specified by the URL that is passed as parameter for the constructor of SPSite. Remember that SPSite still refer to the top-level site which in this case is http://Server_Name/sites/HR.


Categories: Uncategorized Tags:
  1. No comments yet.