SPAttachmentCollection.AddNow method

Adds the attachment that is represented by the specified file name and byte array to the list item without requiring an update to the parent list item.

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

Syntax

'Declaration
Public Function AddNow ( _
    leafName As String, _
    data As Byte() _
) As String
'Usage
Dim instance As SPAttachmentCollection
Dim leafName As String
Dim data As Byte()
Dim returnValue As String

returnValue = instance.AddNow(leafName, _
    data)
public string AddNow(
    string leafName,
    byte[] data
)

Parameters

  • leafName
    Type: System.String

    A string that specifies the name of the file to attach.

  • data
    Type: []

    A byte array that contains the file to attach.

Return value

Type: System.String
A string that contains the URL of the attachment.

Remarks

If you add an attachment through the AddNow method, the change takes effect immediately in the database and does not require updating the list item in order to refresh version information. However, the attachment is not immediately added to the current attachment collection object, and you must call the Attachments property of the list item to get the updated attachment collection.

Examples

The following code example shows how to add a file attachment to an item in the Announcements list.

Since the AddNow method requires that you pass the file in binary format as a parameter, the example uses the OpenBinary method of the SPFile class to open each file within the folder in binary format.

SPSite oSiteCollection = SPContext.Current.Site;
using (SPWeb oWebsite = oSiteCollection.AllWebs["Site_Name"])
{
    SPFile oFile = oWebsite.Folders["Folder_Name"].Files["File_Name"];

    SPList oList = oWebsite.Lists["Announcements"];
    SPListItemCollection collItem = oList.Items;
    SPListItem oListItem = collItem[5];

    SPAttachmentCollection collAttachments = 
         oListItem.Attachments;

    string strFileName = oFile.Name;

    byte[] binFile = oFile.OpenBinary();

    collAttachments.AddNow(strFileName, binFile);
}
Dim oSiteCollection As SPSite = SPContext.Current.Site
Using oWebsite As SPWeb = oSiteCollection.AllWebs("Site_Name")
    Dim oFile As SPFile = oWebsite.Folders("Folder_Name").Files("File_Name")

    Dim oList As SPList = oWebsite.Lists("Announcements")
    Dim collItem As SPListItemCollection = oList.Items
    Dim oListItem As SPListItem = collItem(5)

    Dim collAttachments As SPAttachmentCollection = oListItem.Attachments

    Dim strFileName As String = oFile.Name

    Dim binFile() As Byte = oFile.OpenBinary()

    collAttachments.AddNow(strFileName, binFile)
End Using

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