Tuesday, March 29, 2011

SPWeb Vs SPSite Vs SPWebApplication

If you are a beginner to SharePoint its vital to get an clear knowledge and should have a clear idea to distinguish SPWeb and SPSite and SPWebapplication. If you have a SharePoint environment setup in your machine try writing the following simple console application. This will go beyond the definitions and will be ease to understand and clarify these three buzz words.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace MyFirstTest
{
class Program
{
static void Main(string[] args)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplicationCollection webappcol = SPWebService.ContentService.WebApplications;
foreach (SPWebApplication webapp in webappcol)
{
Console.WriteLine("Web Application URL :" + webapp.Name);

foreach (SPSite site in webapp.Sites)
{
Console.WriteLine("Site Collection URL :" + site.Url);
Console.ReadKey();

foreach (SPWeb web in site.AllWebs)
{
Console.WriteLine("Site URL :" + web.Url);
Console.ReadKey();
}
}
}

});
}
}
}

OutPut:

Web Application URL : Sharepoint-80

Site Collection URL : http://cd-Home

Site URL : http://cd-Home
http://cd-Home/Test
http://cd-Home/Test/Inside

Note:Since I am getting the "site.AllWebs" property it will iterate through all the webs in the Site Collection

Try in your environment you can understand the difference.....

No comments:

Post a Comment