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!

Leave a Reply