Order Sharepoint areas like the portal hierarchy

Posted Monday, May 23, 2005 4:19 PM by Jean-Paul Smit

A few weeks ago I was working on a web part which had to display the area hierarchy, much like the way topics are displayed.

When implementing I found out that the areas in the AreaCollection were returned in alphabetical order and not in the hiearchical order that Sharepoint uses. It turned out that other people also ran into this. Using the 'HonorOrder' property didn't help a lot and in fact it is unclear what that property actually does.

I built my own AreaCollection, very easy but very useful. One thing to be aware of:  when an area is removed from the collection Sharepoint doesn't renumber the other areas. This means that you can have 3 areas with order numbers like 2, 5 and 9. This implementation is taking that into account.

int tmpAreaCnt = 0;

int nbrOfVisibleAreas = 0;

 

// Determine the highest order number

foreach(Area thisArea in SomeArea.Areas)

{

       if(thisArea.Order > tmpAreaCnt)

       {

              tmpAreaCnt = thisArea.Order;

       }

}

 

// Create temp and real array objects to contain the sorted areas

// Increase the counter by one, the array is 0-based

tmpAreaCnt++;

Area[] tmpPortalAreaColl = new Area[tmpAreaCnt];

 

foreach(Area thisArea in SomeArea.Areas)

{

       if(thisArea.Navigation == AreaNavigation.Show)

       {

              tmpPortalAreaColl[thisArea.Order] = thisArea;

              nbrOfVisibleAreas++;

       }

}

 

// Now we only have to remove the empty entries

int areaCnt = 0;

Area[] portalAreaColl = new Area[nbrOfVisibleAreas];

for(int i=0; i<tmpAreaCnt; i++)

{

       if(tmpPortalAreaColl[i] != null)

       {

              portalAreaColl[areaCnt++] = tmpPortalAreaColl[i];

       }

}

 

Filed under:

Comments

# re: Order Sharepoint areas like the portal hierarchy

Monday, May 23, 2005 11:42 PM by Jean-Paul Smit

Judging from your code following could do the trick without 3 loops.
maybe i missed a thing (or two?), didn't test the code. In essense its using an IComparer to do the job of sorting, without the for-loops.


//create an arraylist & sort the areas using a binary sorting algorithm ;)
ArrayList areaList = new ArrayList(SomeArea.Areas);
areaList.Sort(new AreaListComparer());

//should nonvisible areas be removed? if not you could remove the while loop
while(areaList.Count > 0 && !areaList[0].Show)
{
areaList.RemoveAt(0);
}

Area[] portalAreaColl = (Area[]) areaList.ToArray(typeof(Area));


public class AreaListComparer: IComparer
{
public int Compare(object x, object y)
{
Area areaX = x as Area; Area areaY = y as Area;

if (areaX != null && areaY != null)
{
//without testing it, not sure wether this is the right way around
if (areaX.Navigation != AreaNavigation.Show) return -1;
if (areaY.Navigation != AreaNavigation.Show) return 1;

return areaX.Order.CompareTo(areaY.Order);
}
return -1;
}
}