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)


Public Function Add ( _
    urlOfFile As String, _
    file As Stream, _
    createdBy As SPUser, _
    modifiedBy As SPUser, _
    timeCreated As DateTime, _
    timeLastModified As DateTime _
) As SPFile
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, _
public SPFile Add(
    string urlOfFile,
    Stream file,
    SPUser createdBy,
    SPUser modifiedBy,
    DateTime timeCreated,
    DateTime timeLastModified


  • urlOfFile
    Type: System.String

    The site-relative URL of 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.

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

        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")

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

                Dim destinFiles As SPFileCollection = destinList.RootFolder.Files

                    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
                Catch Else
                End Try
            End Try
        Next srcItem
    End Try
Catch Else
End Try
    using (SPSite oSiteCollectionSrc = new SPSite("http://SourceSiteCollection"))
        SPWeb oWebsiteSrc = oSiteCollectionSrc.AllWebs["SourceWebSite"];
        SPList oListSrc = oWebsiteSrc.GetList("SourceWebSite/SourceDocLib");

        SPListItemCollection collListItemsSrc = oListSrc.Items;

        foreach (SPListItem oListItemSrc in collListItemsSrc)
            SPFile oFileSrc = oListItemSrc.File;
            Stream srcStream = oFileSrc.OpenBinaryStream();

            using (SPSite oSiteCollectionDest = new SPSite("http://DestinationSiteCollection"))
                SPWeb oWebsiteDest = oSiteCollectionDest.OpenWeb("DestinationWebSite");
                SPList oListDest = oWebsiteDest.GetList("DestinationWebSite/DestinationDocLib");

                SPFileCollection collFilesDest = oListDest.RootFolder.Files;

                    SPFile oFileDest = collFilesDest.Add(oListDest + @"/" + oFileSrc.Name, srcStream, oFileSrc.Author, oFileSrc.ModifiedBy, oFileSrc.TimeCreated, oFileSrc.TimeLastModified);

                    SPListItem oListItemDest = oFileDest.Item;
                    oListItemDest["Created"] = oFileDest.TimeCreated;
                    oListItemDest["Modified"] = oFileDest.TimeLastModified;
                catch { }
catch { }


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.

See also


SPFileCollection class

SPFileCollection members

Add overload

Microsoft.SharePoint namespace