in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Sharepoint Space

It is all about Sharepoint

SPSite, SPWeb and SPWebCollection

Sharepoint come with many class. Some class name is confusing.

SPSite:
Represents a collection of sites on a virtual server, including a top-level site and all its subsites. Each SPSite object, or site collection, is represented within an SPSiteCollection object that consists of the collection of all site collections on the virtual server.
Note: SPSite is NOT a Collection, in another words, you can not use foreach to loop SPSite. To do this, you have to use SPWebCollection.

SPWebCollection:
Represents a collection of SPWeb objects.

SPWeb:
Represents a SharePoint Web site.

SPFolder:
Represents a folder on a SharePoint Web site. 

The following snipet is copy from a class:

        #region properties
        /// <summary>
        /// site collection url address
        /// </summary>
        public string _RootSiteUrl
        {
            get { return RootSiteUrl; }
            set { RootSiteUrl = value; }
        }
        /// <summary>
        /// Site collection, get SPSite object repsent a "collection"
        /// </summary>
        public SPSite _Sites
        {
            get { return new SPSite(this._RootSiteUrl); }
            set { Sites = value; }
        }
        /// <summary>
        /// Web collection, SPWebCollection, this is the real Collection
        /// </summary>
        public SPWebCollection _Webs
        {
            get { return this._Sites.AllWebs; }
        }

        public string _PPMAspxPageSiteUrl
        {
            get { return this.PPMTargetSiteUrl; }
            set { PPMTargetSiteUrl = value; }
        }

        public SPWeb this[string SiteName] // indexer, represent a SPWeb
        {
            get
            {
                int i = 0;
                foreach (SPWeb web in this._Webs)
                {
                    if (web.Url == SiteName)
                        return this._Webs[ i ];
                    i++;
                }
                return null;
            }
        }

        #endregion

The indexer is quite useful, to get a SPWeb object, simply use mySharePointClass["your site or subsite url"] to access!

Comments

 

BluePhaze said:

Hey there,

For some of you who may not have worked with these objects much there are some very important things to note. Whenever youa re creating instances of any of the above mentioned objects, you need explicitly call dispose on them unless you are using getContext. Otherwise you can get all kinds of craziness happening that will be hard to pindown.

For a bit more info please feel free to look at the updated documentation that we have put up on MSDN covering this: msdn2.microsoft.com/.../bb687949.aspx

Thanks,

James

October 11, 2007 2:42 AM
 

BluePhaze said:

Also a side note, you can iterate through the subsites (spWebs) of a site collection (spSite) by using spSite.Allwebs. This allows you to use it in a forEach statement. :)

October 11, 2007 2:45 AM
 

spsite said:

Pingback from  spsite

May 9, 2008 5:17 PM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts