Friday, July 20, 2012

SPSite in SharePoint 2010

Often confused with a site within a site collection, SPSite represents a collection (including top level web site annd all its sub sites. not a site.

At the top we have a web application, or a SPWebApplication
After SPWebApplication, we have site collection or site or SPSite
In each site collection we have web sites or SPWeb
In each of these sites we can have more sub sites also called SPWeb.


Lets see what our object model representation is in terms of Central Administration
1. When you click New under Central Administration>Application Management> Manage web applications, you are dealing with SPWebApplication. While creating a web application, you select Authentication type, port, host header and other settings.
2. When you click Create site collections under Central Administration> Application Management, you are dealing with SPSite (you select a web application to create site collection in it). While creating a site collection, you select an existing web application and choose site templates.
3. When you click New Site under Site Actions from your site, you are dealing with SPWeb. While creating s web site, you select a from available templates, title and url


SPSite object can be obtained in many ways. For example

SPSite siteCollection = new SPSite("SITE COLLECTION URL");
SPSite siteCollection = SPContext.Current.Site;
******************************************************
Sample Code
using (SPSite oSiteCollection = new SPSite(SITE URL))
            {
                Console.WriteLine("All Webs Count: " + oSiteCollection.AllWebs.Count.ToString());
                Console.WriteLine("Content Database Name: " + oSiteCollection.ContentDatabase.Name);
                Console.WriteLine("Features Counts: " + oSiteCollection.Features.Count.ToString());
                Console.WriteLine("Host Name: " + oSiteCollection.HostName);
                Console.WriteLine("Owner Login Name: " + oSiteCollection.Owner.LoginName);
                Console.WriteLine("Root Web Exists: " + oSiteCollection.RootWeb.Exists.ToString());
                Console.WriteLine("URL: " + oSiteCollection.Url);
                Console.WriteLine("Web Application Application Pool Name: " + oSiteCollection.WebApplication.ApplicationPool.Name);
            }

It is also available in SandBoxed solutions.

Related Links
SPContext
SPWeb

No comments: