Friday, July 20, 2012

SPWeb in SharePoint 2010

Often confused with a site collection within a web application, SPWeb represents an actual web site within a site collection (SPSite) which holds actual contents like pages, document libraries, lists .

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 a 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");
SPWeb web = SPContext.Current.Web;

SPSite siteCollection2 = new  SPSite("SITE COLLECTION URL");
SPWeb web2 = siteCollection2.OpenWeb("WEB URL");

SPSite siteCollection3 = new  SPSite("SITE COLLECTION URL");
SPWEB web3 = siteCollection3.RootWeb
******************************************************
Sample Code
using (SPSite ositeCollection = new SPSite(SITE URL))
            {
                using (SPWeb oweb = ositeCollection.RootWeb)
                //using (SPWeb oweb = ositeCollection.OpenWeb("PressReleases"))
                {
                    Console.WriteLine("Is Root Web: " + oweb.IsRootWeb);
                    Console.WriteLine("Master URL: " + oweb.MasterUrl);
                    Console.WriteLine("Name: " + oweb.Name);
                    Console.WriteLine("Site Port: " + oweb.Site.Port);
                    Console.WriteLine("Theme: " + oweb.Theme);
                    Console.WriteLine("Title:" + oweb.Title);
                    oweb.Title = "New Title";
                    oweb.Update();
                    Console.WriteLine("Title:" + oweb.Title);
                    Console.WriteLine("User is Web Admin: " + oweb.UserIsWebAdmin.ToString());
                    Console.WriteLine("Web Template: " + oweb.WebTemplate);
                    foreach (SPUser user in oweb.AllUsers)
                    {
                        Console.WriteLine("User Login Name: " + user.LoginName);
                    }

                    string templateName = "Test Site";
                    string templateTitle = "Test Template";
                    string templateDesc = "This template was saved programmatically.";
                    Console.WriteLine("Save as template");
                    oweb.SaveAsTemplate(templateName, templateTitle, templateDesc, false);
                    Console.WriteLine("Saved");
                }
                //Console.WriteLine("Start Creating site from code. Should run only once");
                //ositeCollection.RootWeb.Webs.Add("FromCode");
                //ositeCollection.RootWeb.Update();
                //Console.WriteLine("Site Created");
                //using (SPWeb oweb = ositeCollection.OpenWeb("FromCode"))
                //{
                //    Console.WriteLine("Is Root Web: " + oweb.IsRootWeb);
                //    Console.WriteLine("Master URL: " + oweb.MasterUrl);
                //    Console.WriteLine("Name: " + oweb.Name);
                //    Console.WriteLine("Site Port: " + oweb.Site.Port);
                //    Console.WriteLine("Theme: " + oweb.Theme);
                //    Console.WriteLine("Title:" + oweb.Title);
                //    Console.WriteLine("User is Web Admin: " + oweb.UserIsWebAdmin.ToString());
                //    Console.WriteLine("Web Template: " + oweb.WebTemplate);
                //    Console.WriteLine("Lists Collection");
                //}
            }

It is also available in SandBoxed solutions.

Related Links
SPContext
SPSite

No comments: