Source included A collection of PageData objects.

Namespace:  EPiServer.Core
Assembly:  EPiServer (in EPiServer.dll) Version: 6.0.530.0

Syntax

Remarks

Control authors might need to fill this kind of collection from code. Mostly you'll work on collections retrieved by a GetChildren(...) call or from a Web control.

The GetChildren(...) method defined on the IPageSource interface returns a PageDataCollection. The IPageSource interface is implemented by many classes like PageBase (and descendants), DataFactory, PageControlBase (which most Web controls inherit from) and more.

PageDataCollection is often used together with other EPiServer classes, see the following HowTo articles which demonstrate some ways in which it can be used: Search for pages created recently and Search for pages based on page type property.

CopyC#
#region Copyright � 1997-2007 EPiServer AB. All Rights Reserved.
/*
This code may only be used according to the EPiServer License Agreement.
The use of this code outside the EPiServer environment, in whole or in
parts, is forbidden without prior written permission from EPiServer AB.

EPiServer is a registered trademark of EPiServer AB. For more information 
see http://www.episerver.com/license or request a copy of the EPiServer 
License Agreement by sending an email to info@episerver.com*/

#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

namespace EPiServer.Core
{
    /// <summary>
    /// <img src="Icons/CSharp.gif" alt="Source included" border="0" /> 
    /// A collection of <b>PageData</b> objects.
    /// </summary>
    /// <remarks>
    /// <para>
    /// Control authors might need to fill this kind of collection from code. Mostly you'll 
    /// work on collections retrieved by a <b>GetChildren</b>(...) call or from a Web control. 
    /// </para>
    /// <para>
    /// The <b>GetChildren</b>(...) method defined on the <b>IPageSource</b> interface returns a 
    /// <b>PageDataCollection</b>. The <b>IPageSource</b> interface is 
    /// implemented by many classes like <b>PageBase</b> (and descendants), <b>DataFactory</b>, 
    /// <b>PageControlBase</b> (which most Web controls inherit from) and more.
    /// </para>
    /// <para>
    /// PageDataCollection is often used together with other EPiServer classes, see the following HowTo 
    /// articles which demonstrate some ways in which it can be used: 
    /// <a href="http://sdk.episerver.com/library/cms5/Developers%20Guide/How%20To/Search%20for%20pages%20created%20recently.htm">Search for pages created recently</a>
    /// and 
    /// <a href="http://sdk.episerver.com/library/cms5/Developers%20Guide/How%20To/Search%20for%20pages%20based%20on%20page%20type%20property.htm">Search for pages based on page type property.</a>
    /// 
    /// </para>
    /// <code source="../EPiServerNET/Core/PageDataCollection.cs" lang="cs"/>
    /// </remarks>
    /// <example>
    /// The following code example demonstrates the usage of <b>PageDataCollection</b>.
    /// <code source="../CodeSamples/EPiServer/Core/PageDataCollectionSamples.cs" region="PageDataCollection1" />
    /// </example>
    [Serializable]
    public class PageDataCollection : IList<PageData>, IList
    {
        private List<PageData> _pageList;
        private object _pageArrayLockObject = new object();

        /// <summary>
        /// Default constructor
        /// </summary>
        public PageDataCollection()
        {
            _pageList = new List<PageData>();
        }

        /// <summary>
        /// Constructor with capacity indication
        /// </summary>
        public PageDataCollection(int capacity)
        {
            _pageList = new List<PageData>(capacity);
        }

        /// <summary>
        /// Create and copy PageData elements from other PageData collection
        /// </summary>
        public PageDataCollection(ICollection<PageData> c)
        {
            _pageList = new List<PageData>(c);
        }

        /// <summary>
        /// Create a list with PageData objects from any enumareble collection.
        /// </summary>
        /// <remarks>
        /// Supports any enumerable which contains <see cref="EPiServer.Core.PageData" /> or <see cref="EPiServer.Core.RawPage" />
        /// <para>
        /// Note that in te case of an enumerable with PageData instances, this constructor will reference the same instance
        /// of the PageData objects as the original enumerable, but for RawPage instances it will create new PageData objects.
        /// </para>
        /// </remarks>
        public PageDataCollection(IEnumerable e)
        {
            _pageList = new List<PageData>();
            foreach (object page in e)
            {
                if (page is PageData)
                {
                    _pageList.Add((PageData)page);
                }
                else if (page is RawPage)
                {
                    _pageList.Add(new PageData((RawPage)page));
                }
                else
                {
                    throw new EPiServerException("Unknown type in IEnumerable:" + page.GetType().ToString());
                }
            }
        }

        /// <summary>
        /// Create and copy PageData elements array of RawPage objects.
        /// </summary>
        public PageDataCollection(RawPage[] rawPages)
        {
            _pageList = new List<PageData>(rawPages.Length);
            foreach (RawPage page in rawPages)
            {
                _pageList.Add(new PageData(page));
            }
        }

        /// <summary>
        /// Access PageData objects using index
        /// </summary>
        public PageData this[int index]
        {
            get { return _pageList[index]; }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value", "Cannot add null object to PageDataCollection");
                }
                _pageList[index] = value;
            }
        }

        /// <summary>
        /// Retrive enumerator
        /// </summary>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return _pageList.GetEnumerator();
        }

        /// <summary>
        /// Remove all items in list
        /// </summary>
        public void Clear()
        {
            _pageList.Clear();
        }

        /// <summary>
        /// Insert new page at given position
        /// </summary>
        /// <param name="index">Index to insert page</param>
        /// <param name="page">Page to insert</param>
        public void Insert(int index, PageData page)
        {
            if (page == null)
            {
                throw new ArgumentNullException("value", "Cannot add null object to PageDataCollection");
            }
            _pageList.Insert(index, page);
        }

        /// <summary>
        /// Remove page from collection
        /// </summary>
        /// <param name="index">Remove at index</param>
        public void RemoveAt(int index)
        {
            _pageList.RemoveAt(index);
        }

        /// <summary>
        /// Remove multiple pages from collection
        /// </summary>
        /// <param name="index">Start removing at index</param>
        /// <param name="count">Number of pages to remove</param>
        public void RemoveRange(int index, int count)
        {
            _pageList.RemoveRange(index, count);
        }

        /// <summary>
        /// Number of pages in collection
        /// </summary>
        /// <returns>Returns the number of pages in the collection.</returns>
        public int Count
        {
            get { return _pageList.Count; }
        }

        /// <summary>
        /// Sorts all the elements in the collection
        /// </summary>
        /// <param name="comparer"></param>
        public void Sort(IComparer<PageData> comparer)
        {
            _pageList.Sort(comparer);
        }

        /// <summary>
        /// Make a shallow copy of collection
        /// </summary>
        /// <returns>Copied collection</returns>
        public PageDataCollection ShallowCopy()
        {
            return new PageDataCollection((ICollection<PageData>)_pageList);
        }

        /// <summary>
        /// Make a deep copy of collection
        /// </summary>
        /// <returns>Copied collection</returns>
        public PageDataCollection Copy()
        {
            PageDataCollection coll = new PageDataCollection(Count);
            foreach (PageData pageData in _pageList)
            {
                coll.Add(pageData.Copy());
            }
            return coll;
        }

        /// <summary>
        /// Add pages from other collection
        /// </summary>
        /// <param name="pages">Collection to add</param>
        public void Add(PageDataCollection pages)
        {
            _pageList.AddRange(pages);
        }

        /// <summary>
        /// Find page reference in collection
        /// </summary>
        /// <param name="page">The reference to find</param>
        /// <returns>Returns -1 when reference not found</returns>
        public int Find(PageReference page)
        {
            for (int i = 0; i < _pageList.Count; i++)
            {
                if (_pageList[i].PageLink.CompareToIgnoreWorkID(page))
                {
                    return i;
                }
            }

            return -1;
        }

        /// <summary>
        /// Find page reference in collection with a specific language branch
        /// </summary>
        /// <param name="page">The reference to find</param>
        /// <param name="languageBranch">The language branch to search for</param>
        /// <returns>Returns -1 when reference not found</returns>
        public int Find(PageReference page, string languageBranch)
        {
            for (int i = 0; i < _pageList.Count; i++)
            {
                if (_pageList[i].PageLink.CompareToIgnoreWorkID(page))
                {
                    if (String.Equals(_pageList[i].LanguageBranch, languageBranch, StringComparison.OrdinalIgnoreCase))
                    {
                        return i;
                    }
                }
            }

            return -1;
        }

        /// <summary>
        /// Check if page with reference exists in collection
        /// </summary>
        /// <param name="page">The reference to find</param>
        /// <returns>Returns <b>false</b> when reference not found</returns>
        public bool Exists(PageReference page)
        {
            return Find(page) >= 0;
        }

        /// <summary>
        /// Check if page with reference exists in collection
        /// </summary>
        /// <param name="page">The reference to find</param>
        /// <param name="languageBranch">The language branch to search for</param>
        /// <returns>Returns <b>false</b> when reference not found</returns>
        public bool Exists(PageReference page, string languageBranch)
        {
            return Find(page, languageBranch) >= 0;
        }

        /// <summary>
        /// Gets an object that can be used to synchronize access to the internal collection.
        /// </summary>
        public object SyncRoot
        {
            get { return _pageArrayLockObject; }
        }

        /// <summary>
        /// Gets a value indicating whether access is synchronized (thread-safe).
        /// </summary>
        public bool IsSynchronized
        {
            get { return false; }
        }

        /// <summary>
        /// Copies the elements to an <b>Array</b>, starting at a particular <b>Array</b> index.
        /// </summary>
        /// <param name="array">The one-dimensional Array that is the destination of the elements copied.</param>
        /// <param name="index">The zero-based index in array at which copying begins.</param>
        void ICollection.CopyTo(Array array, int index)
        {
            _pageList.ToArray().CopyTo(array, index);
        }

        /// <summary>
        /// Copies the elements to an PageData Array, starting at a particular Array index.
        /// </summary>
        /// <param name="array">The one-dimensional PageData Array that is the destination of the elements copied.</param>
        /// <param name="index">The zero-based index in array at which copying begins.</param>
        public void CopyTo(PageData[] array, int index)
        {
            _pageList.CopyTo(array, index);
        }

        /// <summary>
        /// Returns array of <see cref="EPiServer.Core.RawPage"/> elements
        /// </summary>
        /// <returns></returns>
        public RawPage[] ToRawPageArray()
        {
            List<RawPage> list = new List<RawPage>(Count);
            foreach (PageData pageData in _pageList)
            {
                list.Add(pageData.ToRawPage());
            }
            return list.ToArray();
        }

        #region ICollection<PageData> Members

        /// <summary>
        /// Add new page to collection
        /// </summary>
        /// <param name="page">Page to add</param>
        /// <returns></returns>
        public void Add(PageData page)
        {
            if (page == null)
            {
                throw new ArgumentNullException("value", "Cannot add null object to PageDataCollection");
            }
            _pageList.Add(page);
        }

        /// <summary>
        /// Determines whether an PageData is in the Collection. 
        /// </summary>
        /// <param name="item">The PageData to locate in the List. The value can be a null reference.</param>
        /// <returns></returns>
        public bool Contains(PageData item)
        {
            return _pageList.Contains(item);
        }

        /// <summary>
        /// Gets a value indicating whether the ICollection is read-only (defalut value is false).
        /// </summary>
        public bool IsReadOnly
        {
            get { return false; }
        }

        /// <summary>
        /// Removes the first occurrence of a specific page from the PageDataCollection. 
        /// </summary>
        /// <param name="page">The page to remove from the PageDataCollection.</param>
        /// <returns>true if page was successfully removed from the PageDataCollection; otherwise, false. This method also returns false if page is not found in the collection. </returns>
        public bool Remove(PageData page)
        {
            return _pageList.Remove(page);
        }

        #endregion

        #region IEnumerable<PageData> Members

        /// <summary>
        /// Returns enumerator for this collection
        /// </summary>
        /// <returns></returns>
        public IEnumerator<PageData> GetEnumerator()
        {
            return _pageList.GetEnumerator();
        }

        #endregion

        #region IList<PageData> Members

        /// <summary>
        /// Determines the index of a specific item in the <see cref="PageDataCollection"/>.
        /// </summary>
        /// <param name="item">The <see cref="EPiServer.Core.PageData"/> item to locate in the <see cref="PageDataCollection"/>.</param>
        /// <returns>
        /// The index of <paramref name="item"/> if found in the list; otherwise, -1.
        /// </returns>
        public int IndexOf(PageData item)
        {
            return _pageList.IndexOf(item);
        }

        #endregion

        #region IList Members

        /// <summary>
        /// Adds an item, which must be of type <see cref="EPiServer.Core.PageData"/>, to the <see cref="PageDataCollection"/>.
        /// </summary>
        /// <param name="value">The <see cref="T:System.Object"/> to add to the <see cref="PageDataCollection"/>.</param>
        /// <returns>
        /// The position into which the new element was inserted.
        /// </returns>
        public int Add(object value)
        {
            PageData pageData = value as PageData;
            if (pageData == null)
            {
                throw new ArgumentException("Must be of type PageData", "value");
            }
            Add(pageData);
            return _pageList.Count - 1;
        }

        /// <summary>
        /// Determines whether the <see cref="PageDataCollection"/> contains a specific value.
        /// </summary>
        /// <param name="value">The <see cref="T:System.Object"/> to locate in the <see cref="PageDataCollection"/>.</param>
        /// <returns>
        /// true if the <see cref="T:System.Object"/> is found in the <see cref="PageDataCollection"/>; otherwise, false.
        /// </returns>
        public bool Contains(object value)
        {
            PageData pageData = value as PageData;
            if (pageData == null)
            {
                return false;       // The collection can only contain PageData objects
            }
            return Contains(pageData);
        }

        /// <summary>
        /// Determines the index of a specific item in the <see cref="PageDataCollection"/>.
        /// </summary>
        /// <param name="value">The <see cref="T:System.Object"/> to locate in the <see cref="PageDataCollection"/>.</param>
        /// <returns>
        /// The index of <paramref name="value"/> if found in the list; otherwise, -1.
        /// </returns>
        public int IndexOf(object value)
        {
            PageData pageData = value as PageData;
            if (pageData == null)
            {
                return -1;       // The collection can only contain PageData objects
            }
            return IndexOf(pageData);
        }

        /// <summary>
        /// Inserts an item, which must be of type <see cref="EPiServer.Core.PageData"/>, in the <see cref="PageDataCollection"/> at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which <paramref name="value"/> should be inserted.</param>
        /// <param name="value">The <see cref="EPiServer.Core.PageData"/> instance to insert into the <see cref="PageDataCollection"/>.</param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        /// <paramref name="index"/> is not a valid index in the <see cref="PageDataCollection"/>. </exception>
        public void Insert(int index, object value)
        {
            PageData pageData = value as PageData;
            if (pageData == null)
            {
                throw new ArgumentException("Must be of type PageData", "value");
            }
            Insert(index, pageData);
        }

        /// <summary>
        /// Gets a value indicating whether the <see cref="PageDataCollection"/> has a fixed size.
        /// </summary>
        /// <value>Always false for <see cref="PageDataCollection"/>.</value>
        public bool IsFixedSize
        {
            get { return false; }
        }

        /// <summary>
        /// Removes the first occurrence of a specific object from the <see cref="PageDataCollection"/>.
        /// </summary>
        /// <param name="value">The <see cref="T:System.Object"/> to remove from the <see cref="PageDataCollection"/>.</param>
        public void Remove(object value)
        {
            PageData pageData = value as PageData;
            if (pageData == null)
            {
                return;       // The collection can only contain PageData objects
            }
            Remove(pageData);
        }

        /// <summary>
        /// Gets or sets the <see cref="EPiServer.Core.PageData"/> instance at the specified index.
        /// </summary>
        object IList.this[int index]
        {
            get { return this[index]; }
            set
            {
                PageData pageData = value as PageData;
                if (pageData == null)
                {
                    throw new ArgumentException("Must be of type PageData");
                }
                this[index] = pageData;
            }
        }

        #endregion
    }
}

Examples

The following code example demonstrates the usage of PageDataCollection.
CopyC#
// must be called from inside a class that implements IPageSource, 
// like SimplePage or TemplatePage.
PageDataCollection oPages;
oPages = this.GetChildren(EPiServer.Core.PageReference.StartPage);
Response.Write("Number of pages below startpage: " + oPages.Count);

Inheritance Hierarchy

System..::.Object
  EPiServer.Core..::.PageDataCollection

See Also