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)

'Declaration
PublicFunctionAdd ( _
	urlOfFileAsString, _
	fileAsByte(), _
	createdByAsSPUser, _
	modifiedByAsSPUser, _
	timeCreatedAsDateTime, _
	timeLastModifiedAsDateTime _
) AsSPFile
'Usage
DiminstanceAsSPFileCollectionDimurlOfFileAsStringDimfileAsByte()
DimcreatedByAsSPUserDimmodifiedByAsSPUserDimtimeCreatedAsDateTimeDimtimeLastModifiedAsDateTimeDimreturnValueAsSPFilereturnValue = instance.Add(urlOfFile, _
	file, createdBy, modifiedBy, timeCreated, _
	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.

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
Show:
© 2014 Microsoft