Export (0) Print
Expand All

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)

public SPFile Add(
	string urlOfFile,
	byte[] file,
	SPUser createdBy,
	SPUser modifiedBy,
	DateTime timeCreated,
	DateTime timeLastModified
)

Parameters

urlOfFile
Type: System.String

The site-relative URL of the file.

file
Type: []

A byte array that contains the file.

createdBy
Type: Microsoft.SharePoint.SPUser

The user who created the file.

modifiedBy
Type: Microsoft.SharePoint.SPUser

The user who last modified the file.

timeCreated
Type: System.DateTime

The 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.DateTime

The 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.

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.

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.

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();
}
Show:
© 2014 Microsoft