Share via


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)

Syntax

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

Parameters

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

Examples

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
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;

                try
                {
                    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;
                    oListItemDest.Update();
                }
                catch { }
                oWebsiteDest.Dispose();
            }
        }
        oWebsiteSrc.Dispose();
    }
}
catch { }

Note

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

Reference

SPFileCollection class

SPFileCollection members

Add overload

Microsoft.SharePoint namespace