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)


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

returnValue = instance.Add(urlOfFile, _
public SPFile Add(
    string urlOfFile,
    byte[] file


  • 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")
    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
End Try
using (SPSite oSiteCollection = new SPSite("http://localhost"))
    using(SPWeb oWebsite = oSiteCollection.AllWebs["MyWebSite"])
        SPWebCollection collWebsites = oWebsite.Webs;
        SPFile oFile = oWebsite.GetFile("MyDocLib/MyFile");
        foreach (SPWeb oWebsiteNext in collWebsites)
            SPFolder oFolder = oWebsiteNext.GetFolder("Shared Documents");
            SPFileCollection collFiles = oFolder.Files;

            string strDestUrl = oFolder.Url + "/" + oFile.Name;
            byte[] binFile = oFile.OpenBinary();
            collFiles.Add(strDestUrl, binFile);



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


SPFileCollection class

SPFileCollection members

Add overload

Microsoft.SharePoint namespace