Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

SPFileCollection.Add method (String, Stream, SPUser, SPUser, DateTime, DateTime)

Creates a file in the collection using the specified URL, a stream 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
Public Function Add ( _
	urlOfFile As String, _
	file As Stream, _
	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 Stream
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)

Parameters

urlOfFile
Type: System.String

The site-relative URL of the file.

file
Type: System.IO.Stream

A stream 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.

The following code example copies each of the files in a document library of a Web site to a document library in another Web site. The example preserves the time created and last modified values of the original library.

Try
    Dim srcSiteCollection As New SPSite("http://SourceSiteCollection")

    Try
        Dim srcWebSite As SPWeb = srcSiteCollection.AllWebs("SourceWebSite")
        Dim srcList As SPList = srcWebSite.GetList("SourceWebSite/SourceDocLib")

        Dim srcItems As SPListItemCollection = srcList.Items

        Dim srcItem As SPListItem
        For Each srcItem In  srcItems
            Dim srcFile As SPFile = srcItem.File
            Dim srcStream As Stream = srcFile.OpenBinaryStream()

            Dim destinSiteCollection As New SPSite("http://DestinationSiteCollection")

            Try
                Dim destinWebSite As SPWeb = destinSiteCollection.OpenWeb("DestinationWebSite")
                Dim destinList As SPList = destinWebSite.GetList("DestinationWebSite/DestinationDocLib")

                Dim destinFiles As SPFileCollection = destinList.RootFolder.Files

                Try
                    Dim destinFile As SPFile = destinFiles.Add(destinList + "/" + srcFile.Name, srcStream, srcFile.Author, srcFile.ModifiedBy, srcFile.TimeCreated, srcFile.TimeLastModified)

                    Dim destinItem As SPListItem = destinFile.Item
                    destinItem("Created") = srcFile.TimeCreated
                    destinItem("Modified") = srcFile.TimeLastModified
                    destinItem.Update()
                Catch Else
                End Try
            Finally
                destinSiteCollection.Dispose()
            End Try
        Next srcItem
    Finally
        srcSiteCollection.Dispose()
    End Try
Catch Else
End Try
NoteNote

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.

Show:
© 2015 Microsoft