SPAttachmentCollection.Item Property (Microsoft.SharePoint)
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)
Syntax

Visual Basic (Declaration)
Public ReadOnly Default Property Item ( _
    iIndex As Integer _
) As String
Visual Basic (Usage)
Dim instance As SPAttachmentCollection
Dim iIndex As Integer
Dim value As String

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

Parameters

iIndex

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

Property Value

A string that contains the file name.
Example

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.

Visual Basic
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 
C#
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];
        }
    }
}
NoteNote:

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 Best Practices: Using Disposable Windows SharePoint Services Objects.

See Also

Tags :


Page view tracker