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)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
Syntax
'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)
public SPFile Add(
string urlOfFile,
byte[] file
)
Parameters
urlOfFile
Type: System.StringThe 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.
Examples
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
using (SPSite oSiteCollection = new SPSite("https://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);
oWebsiteNext.Dispose();
}
}
}
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.