SPAttachmentCollection.Item Property

Gets the file name of the attachment at the specified index in the collection. [C#] In C#, this property is the indexer for the SPAttachmentCollection class.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public ReadOnly Property Item ( _
    iIndex As Integer _
) As String
    Get
'Usage
Dim instance As SPAttachmentCollection
Dim iIndex As Integer
Dim value As String

value = instance.Item(iIndex)
public string this[
    int iIndex
] { get; }

Parameters

  • iIndex
    Type: System.Int32

    A 32-bit integer that specifies the index of the attachment.

Property Value

Type: System.String
A string that contains the file name.

Examples

The following code example iterates through the collection of attachments for each Announcements list of every subsite and uses the indexer to display the file name of each attachment.

The example assumes the existence of an .aspx page that contains a label control named Label1.

Dim siteCollection As SPSite = SPContext.Current.Site
Dim subSites As SPWebCollection = siteCollection.AllWebs
Dim site As SPWeb

For Each site In  subSites

    Dim list As SPList = site.Lists("Announcements")
    Dim listItems As SPListItemCollection = list.Items
    Dim listItem As SPListItem

    For Each listItem In  listItems

        Dim attachments As SPAttachmentCollection = 
            listItem.Attachments
        Dim i As Integer

        For i = 0 To attachments.Count - 1

            Label1.Text += attachments(i)

        Next i

    Next listItem

Next site 
SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsites = oSiteCollection.AllWebs;

foreach (SPWeb oWebsite in collWebsites)
{
    SPList oList = oWebsite.Lists["Announcements"];
    SPListItemCollection collListItems = oList.Items;

    foreach (SPListItem oListItem in collListItems)
    {
        SPAttachmentCollection collAttachments = oListItem.Attachments;

        for (int i=0; i<collAttachments.Count; i++)
        {
            Label1.Text += collAttachments[i];
        }
    }
}

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

SPAttachmentCollection Class

SPAttachmentCollection Members

Microsoft.SharePoint Namespace