SPFileCollection.Add Method (String, Byte , SPUser, SPUser, DateTime, DateTime)
Creates a file in the collection using the specified URL, a byte array that contains the contents of a file, user objects that represent the users who created and last modified the file, and DateTime values that specify when they did so.
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(), _
createdBy As SPUser, _
modifiedBy As SPUser, _
timeCreated As DateTime, _
timeLastModified As DateTime _
) As SPFile
'Usage
Dim instance As SPFileCollection
Dim urlOfFile As String
Dim file As Byte()
Dim createdBy As SPUser
Dim modifiedBy As SPUser
Dim timeCreated As DateTime
Dim timeLastModified As DateTime
Dim returnValue As SPFile
returnValue = instance.Add(urlOfFile, _
file, createdBy, modifiedBy, timeCreated, _
timeLastModified)
public SPFile Add(
string urlOfFile,
byte[] file,
SPUser createdBy,
SPUser modifiedBy,
DateTime timeCreated,
DateTime timeLastModified
)
Parameters
urlOfFile
Type: System.StringThe site-relative URL of the file.
file
Type: []A byte array that contains the file.
createdBy
Type: Microsoft.SharePoint.SPUserThe user who created the file.
modifiedBy
Type: Microsoft.SharePoint.SPUserThe user who last modified the file.
timeCreated
Type: System.DateTimeThe date and time when the file was created. To set the time created, you must also set this value through the indexer of the list item that is associated with the file and call the Update method.
timeLastModified
Type: System.DateTimeThe date and time when the file was last modified. To set the time last modified, you must also set this value through the indexer of the list item that is associated with the file and call the Update method.
Return Value
Type: Microsoft.SharePoint.SPFile
The newly added file.
Remarks
This method fails to overwrite a file if the file already exists. If the user running the command is not a member of the Administrator site group, the SPUser object and DateTime value does not apply.
This Add method works on a site document library; however, if you use it with an area document library, the call fails with an "Access Denied" error. If you do not include the last four parameters in the Add method, the file is added to the area document library, but you cannot change the author and document times.
Examples
The following code example copies each of the files in the top folder of a document library in one site to the document library of another site. The example preserves the time created and last modified values of the original library.
Dim site As SPSite = SPContext.Current.Site
Try
Dim srcFolder As SPFolder = site.AllWebs("MySourceWebSite").GetFolder("MySourceDocLib")
Dim destFiles As SPFileCollection = site.AllWebs("MyDestinationWebSite").GetFolder("MyDestinationDocLib").Files
Dim srcFile As SPFile
For Each srcFile In srcFolder.Files
Dim destURL As String = destFiles.Folder.Url + "/" + srcFile.Name
Dim binFile As Byte() = srcFile.OpenBinary()
Dim userAuthor As SPUser = srcFile.Author
Dim userModified As SPUser = srcFile.ModifiedBy
Dim created As System.DateTime = srcFile.TimeCreated
Dim modified As System.DateTime = srcFile.TimeLastModified
Dim newFile As SPFile = destFiles.Add(destURL, binFile, userAuthor, userModified, created, modified)
Dim fileItem As SPListItem = newFile.Item
fileItem("Created") = created
fileItem("Modified") = modified
fileItem.Update()
Next srcFile
Finally
site.Dispose()
End Try
SPSite oSiteCollection = SPContext.Current.Site;
SPFolder oFolder = oSiteCollection.AllWebs["SourceWebSite"].GetFolder("SourceDocLib");
SPFileCollection collFiles = oSiteCollection.AllWebs["DestWebSite"].GetFolder("DestDocLib").Files;
foreach (SPFile oFile in collFiles.Files)
{
string strDestUrl = collFiles.Folder.Url + "/" + srcFile.Name;
byte[] binFile = oFile.OpenBinary();
SPUser oUserAuthor = oFile.Author;
SPUser oUserModified = oFile.ModifiedBy;
System.DateTime dtCreated = oFile.TimeCreated;
System.DateTime dtModified = oFile.TimeLastModified;
SPFile oFileNew = collFiles.Add(strDestUrl, binFile, oUserAuthor, oUserModified, dtCreated, dtModified);
SPListItem oListItem = oFileNew.Item;
oListItem["Created"] = dtCreated;
oListItem["Modified"] = dtModified;
oListItem.Update();
}