Export (0) Print
Expand All

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

Windows SharePoint Services 3

Creates a file in the collection based on the specified URL, on a Stream object that contains a file, on user objects that represent the users who created and last modified the file, and on DateTime values that specify when they did so.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public SPFile Add(
	string urlOfFile,
	Stream stream,
	SPUser createdBy,
	SPUser modifiedBy,
	DateTime timeCreated,
	DateTime timeLastModified
)

Parameters

urlOfFile
Type: System.String
A string that specifies the URL for the file.
stream
Type: System.IO.Stream
A System.IO.Stream object that contains the file.
createdBy
Type: Microsoft.SharePoint.SPUser
An SPUser object that represents the user who created the file.
modifiedBy
Type: Microsoft.SharePoint.SPUser
An SPUser object that represents the user who last modified the file.
timeCreated
Type: System.DateTime
A System.DateTime value that specifies 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
A System.DateTime value that specifies 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
An SPFile object that represents the 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
{
    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 { }
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 Best Practices: Using Disposable Windows SharePoint Services Objects.

Community Additions

ADD
Show:
© 2014 Microsoft