Wednesday, October 12, 2011

SPSite Vs SPWeb and SPWebApplication – What they represent in SharePoint Server Object Model

SPSite Vs SPWeb and SPWebApplication – What they represent in SharePoint Server Object Model:

Simply Explained:

A SharePoint Web application (SPWebApplication) is web site inside Ineternet Information server, A web application can have multiple sites , a Site (SpSite) is a collection a single top level site (SPWeb) and group of sites (SPWebCollection) and sub sites.




Lets see the Code Way: In this example we will code from top to bottom in the object model....with some coding samples...

String hierarchy="";//Read web application collection

SPWebApplicationCollection webApplicationCol =

SPWebService.ContentService.WebApplications;

Tip: SPWebService is a container for the SPWebApplications.

foreach (SPWebApplication webApp in webApplicationCol){
//Fetch the Web application name

hierarchy += "Web Application Name (SPWebApplication):" + webApp.Name + "\r\n";

//Looping for all SPSites under each web app.
for (int i = 0; i < webApp.Sites.Count; i++){
SPSite site = webApp.Sites[i];

//Site collection URL

hierarchy += "Site Collection URL(SPSite):" + site.Url + "\r\n";
for (int j = 0; j < site.AllWebs.Count; j++)
{
SPWeb web = site.AllWebs[j];
for (int k = 0; k < web.Webs.Count;k++ )
{
SPWeb web1 = web.Webs[k];

//top level site and subsites

URLhierarchy += "Sub Site URL(SPWeb):" + web1.Url + "\r\n";}

web.Dispose();

}
site.Dispose();
}

hierarchy += "----------------------------------------------" + "\r\n";

To get a reference to the current top-level server farm object -
SPFarm myFarm = SPContext.Current.Site.WebApplication.Farm;

To get a reference to the site collection database -
SPSite oSiteCollection = SPContext.Current.Site.CurrentDatabase

To return the collection of site collections in a SharePoint Web application -
SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
using (SPSiteCollection siteCollections = webApplication.Sites){
foreach (SPSite siteCollection in siteCollections){Label1.Text += siteCollection.Url + “
”;
siteCollection.Close();}
}

TIP : To run the above code reference the Microsoft.SharePoint.Administration.SPWebApplication assembly in your code.

To return the collection of the all the Webs or sites within a site collection, including the top-level site and all sub sites.
SPSite oSiteCollection = SPContext.Current.Site;

TIP: SPContext class is used to return context information about the current Web application, site collection, site, list, or a list item.

using (SPWebCollection collWebsite = oSiteCollection.AllWebs);
{
for (int i = 0; i < collWebsite.Count; i++)

{

using (SPWeb oWebsite = collWebsite[i])

{
SPListCollection collList = oWebsite.Lists;

for (int j = 0; j < collList.Count; j++)
{
Label1.Text += SPEncode.HtmlEncode(collWebsite[i].Title) + ” ”+ SPEncode.HtmlEncode(collList[j].Title) + “
”;}
}
}
}

To return the all the subsites and lists of the current site

using (SPWeb oWebSite = mySiteCollection.OpenWeb())
{
using (SPWebCollection subSites = oWebsite.Webs)
{
foreach (SPWeb subSite in subSites){Label1.Text += SPEncode.HtmlEncode(subSite.Title) + “
”;
SPListCollection collList = subSite.Lists;
foreach (SPList oList in collList){Label1.Text += SPEncode.HtmlEncode(oList.Title) + ” ” +oList.ItemCount.ToString() + “
”;
}
subSite.Close();
}
}
TIP:1. SPEncode: class provides methods for encoding strings. (HtmlDecode, HtmlEncode, HtmlEncodePreserveSpaces, IsLegalCharInUrl , NoEncode,etc.. )
2. HtmlEncode: Encodes the specified string so that special characters used in HTML are encoded as HTML entities.
  • < to <
  • > to >
  • & to &
  • " to "
  • ' to '
}
Cheers!!!
Suneet

No comments: