Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Iteration of site collection for subsites index out of range

  Asked By: Tyler    Date: Nov 07    Category: Sharepoint    Views: 1487

I am trying to iterate thru the site collection and then add them to a
drop down list and it works but after last cycle thru the catch gets
fired of with a index out of range error should I be adding or
subtracting a number from count?

Source Code

if (Page.IsPostBack != true)
{
try
{
int count = 0;

SPSite siteCollection = new
SPSite("http://gibwss");
SPWebCollection websiteCollection =
siteCollection.AllWebs;

while (websiteCollection.Count >= count)
{

this.ddlSubsitesInSiteCollection.Items.Add(new ListItem(count + " - "
+ websiteCollection.Names[count],count.ToString()));
//Response.Write(count + " - " +
websiteCollection.Names[count] + count.ToString()+"<br/>");
count++;
}
siteCollection.Dispose();
}
catch
{
Response.Write("error");
}
}


Output
error

this data populates the dropdown list but then it throws the catch at
the end

0 - 0
1 - admin
2 - committees
3 - Site1
4 - Site2
5 - Site3
6 - Site4/subsite1
7 - site4/subsite1/webpage HR15
etc.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Jack Carroll     Answered On: Nov 07

Count is the number  of entries in the collection  based on a zero index. So to
get all the items  you should loop until you have processed the item with index
count-1.

 




Tagged: