Share via


SPWebCollection.Item property (Int32)

Gets the element at the specified index in the collection. In C#, this property is an indexer for the SPWebCollection class.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public ReadOnly Default Property Item ( _
    i As Integer _
) As SPWeb
    Get
'Usage
Dim instance As SPWebCollection
Dim i As Integer
Dim value As SPWeb

value = instance(i)
public SPWeb this[
    int i
] { get; }

Parameters

  • i
    Type: System.Int32

    A 32-bit integer that specifies the index.

Property value

Type: Microsoft.SharePoint.SPWeb
An SPWeb object that represents the website.

Remarks

The Item property throws an ArgumentOutOfRangeException if the specified index is outside the valid range of indices for the collection.

Examples

The following code example iterates through all the users of each site in a specified site collection and displays the user names of users who are members of the Administrator site group for the site collection.

This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

Dim siteCollection As SPSite = SPContext.Current.Site
Dim webSites As SPWebCollection = siteCollection.AllWebs("Site_Name").Webs
Dim i As Integer

For i = 0 To webSites.Count - 1

    Dim users As SPUserCollection = webSites(i).Users
    Dim j As Integer

    For j = 0 To users.Count - 1

        If users(j).IsSiteAdmin Then

            Response.Write(SPEncode.HtmlEncode(webSites(i).Title) & " :: " 
                & users(j).LoginName & "<BR>")

        End If

    Next j

    collWebsites(intIndexWebsites).Dispose()
Next i
SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsites = oSiteCollection.AllWebs["Website_Name"].Webs;

for (int intIndexWebsites = 0; intIndexWebsites < collWebsites.Count; 
     intIndexWebsites++)
{
    SPUserCollection collUsers = collWebsites[intIndexWebsites].Users;
    for (int intIndexAdmins = 0; intIndexAdmins < collUsers.Count; 
         intIndexAdmins++)
    {
        if (collUsers[intIndexAdmins].IsSiteAdmin)
        {
            Response.Write(SPEncode.HtmlEncode(collWebsites[intIndexWebsites].Title) 
              + "--" + collUsers[intIndexAdmins].LoginName + "<BR>");
        }
    }
    collWebsites[intIndexWebsites].Dispose();
}

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

See also

Reference

SPWebCollection class

SPWebCollection members

Item overload

Microsoft.SharePoint namespace