SPFileCollection.Add method (String, Byte[])

Creates a file in the collection using the specified URL and a byte array that contains the file.

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

'Declaration
Public Function Add ( _
	urlOfFile As String, _
	file As Byte() _
) As SPFile
'Usage
Dim instance As SPFileCollection
Dim urlOfFile As String
Dim file As Byte()
Dim returnValue As SPFile

returnValue = instance.Add(urlOfFile, _
	file)

Parameters

urlOfFile
Type: System.String

The site-relative URL of the file.

file
Type: []

A byte array that contains the file.

Return value

Type: Microsoft.SharePoint.SPFile
The newly added file.

The following code example uses the Add method to copy a file from a document library to the Shared Documents folder of every subsite under a specified Web site. If a file of the same name already exists, the Add method throws an exception.

Dim SiteCollection As New SPSite("http://MySiteCollection")
Try
    Dim webSite As SPWeb = siteCollection.AllWebs("MyWebSite")
    Dim destSites As SPWebCollection = webSite.Webs

    Dim srcFile As SPFile = webSite.GetFile("MyDocLib/MyFile")

    Dim destSite As SPWeb
    For Each destSite In  destSites
        Dim destFolder As SPFolder = destSite.GetFolder("Shared Documents")
        Dim destFiles As SPFileCollection = destFolder.Files

        Dim destURL As String = destFolder.Url + "/" + srcFile.Name
        Dim binFile As Byte() = srcFile.OpenBinary()

        destFiles.Add(destURL, binFile)
    Next destSite
Finally
    SiteCollection.Dispose()
End Try
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 Disposing Objects.

Show: