Ask a questionAsk a question
 

AnswerSorting a site collection

  • Wednesday, October 15, 2008 6:35 PMselene01 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    OK here is my latest issue.  I know how to sort list and I know how to return all the sites under a site.  What I don't know is how to sort the sites I am returning. I am using C#. Can anyone help me. 


    using (SPSite site = new SPSite("http://iseeeps"))

    {

    SPWeb mySite = site.OpenWeb();

    SPWebCollection sites = mySite.Webs;

    }

Answers

All Replies

  • Wednesday, October 15, 2008 9:12 PMWaldek MastykarzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I don't think the SPWebCollection class is sortable, so if you want to sort sites, you would have to put them in a custom collection first.
    -- http://blog.mastykarz.nl
  • Saturday, November 07, 2009 2:10 AMsquebler Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Here is some code from http://blogs.windwardreports.com/tomasr/2009/11/sorting-sharepoint-spwebcollection.html to sort a collection of sites by title.

    Create a custom IComparer for sites:

    public class SPListItemComparer : IComparer
    {

        #region IComparer Members

        public int Compare(object x, object y)
        {
            if (!(x is SPListItem) || !(y is SPListItem))
                throw new ArgumentException("Cannot compare non-SPListItem objects using SPListItemComparer");
            return string.Compare(((SPListItem)x).Name, ((SPListItem)y).Name);
        }

        #endregion
    }

    Then, put the sites into a sortable collection such as ArrayList and call the collection's Sort method using your custom IComparer.

    SPWebCollection collWebsite = oSiteCollection.AllWebs;
    ArrayList sortedWebs = new ArrayList(collWebsite.Count);
    foreach (SPWeb oWebsite in collWebsite)
    {
        sortedWebs.Add(oWebsite);
    }
    sortedWebs.Sort(new ArrowUtils.SPWebSortComparer());