Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
SDK Documentation
Add Method
 Add Method (String, Byte[], SPUser,...

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (0)
SPFileCollection.Add Method (String, Byte[], SPUser, SPUser, DateTime, DateTime) (Microsoft.SharePoint)
Creates a file in the collection based on the specified URL, on a byte array 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)
Visual Basic (Declaration)
Public Function Add ( _
    urlOfFile As String, _
    file As Byte(), _
    createdBy As SPUser, _
    modifiedBy As SPUser, _
    timeCreated As DateTime, _
    timeLastModified As DateTime _
) As SPFile
Visual Basic (Usage)
Dim instance As SPFileCollection
Dim urlOfFile As String
Dim file As Byte()
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)
C#
public SPFile Add (
    string urlOfFile,
    byte[] file,
    SPUser createdBy,
    SPUser modifiedBy,
    DateTime timeCreated,
    DateTime timeLastModified
)

Parameters

urlOfFile

A string that specifies the URL for the file.

file

A byte array containing the file.

createdBy

An SPUser object that represents the user who created the file.

modifiedBy

An SPUser object that represents the user who last modified the file.

timeCreated

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

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

An SPFile object that represents the 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.

Visual Basic
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
C#
SPSite oSiteCollection = SPContext.Current.Site;
SPFolder oFolder = oSiteCollection.AllWebs["SourceWebSite"].GetFolder("SourceDocLib");
SPFileCollection collFiles = oSiteCollection.AllWebs["DestWebSite"].GetFolder("DestDocLib").Files;

foreach (SPFile oFile in collFiles.Files)
{
    string strDestUrl = collFiles.Folder.Url + "/" + srcFile.Name;
    byte[] binFile = oFile.OpenBinary();

    SPUser oUserAuthor = oFile.Author;
    SPUser oUserModified = oFile.ModifiedBy;
    System.DateTime dtCreated = oFile.TimeCreated;
    System.DateTime dtModified = oFile.TimeLastModified;

    SPFile oFileNew = collFiles.Add(strDestUrl, binFile, oUserAuthor, oUserModified, dtCreated, dtModified);
    SPListItem oListItem = oFileNew.Item;
    oListItem["Created"] = dtCreated;
    oListItem["Modified"] = dtModified;
    oListItem.Update();
}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
More information      Raphael Jean   |   Edit   |   Show History

Contrary to what the documentation says, this method will overwrite an existing document with the same name. If versioning is enabled for the library, a new version will be created in that case.

The timeCreated and timeLastModified arguments seem to be completely ignored.

When calling this method, then updating the returned SPFile.Item object and calling Update() on it, two versions will be created for the new document. This doesn't happen with the Add(String, byte[]) override.

Tags What's this?: Add a tag
Flag as ContentBug
timeCreated and timeLastModified does not work.      JeffLin   |   Edit   |   Show History

It is confirmed that both timeCreated and timeLastModified are not mean to work. It should always take the current time as the value for those parameters whether you pass in anything or not.

If you have a business reason to set the timeCreated and timeLastModified property of the item uploaded, you can consider using the following code to achive the goal.

<code>

listitem["Created"] = new DateTime(2006, 11,1);

listitem["Modified"] = new DateTime(2006, 12,1);

listitem.UpdateOverwriteVersion();

</code>

by calling UpdateOverwriteVersion, it will also avoid creating a new version of the listitem after the update.

Tags What's this?: Add a tag
Flag as ContentBug
listitem["Created"] simply doesn't work      Mortenm   |   Edit   |   Show History
I just tried the code mentioned above - unfortunately it doesn't work

Well - it works. But you must be an Owner of the site/document library in order to set authors and time of the files added to the system. Contributors cannot add files with proper date and times.
Tags What's this?: Add a tag
Flag as ContentBug
Adding a file with different extension to an existing file      brhoom   |   Edit   |   Show History
I am trying to add a new file with different file extension to an existing file. For example adding a .pdf file to an existing .doc file as a new version. There seems no direct support for such requirements. I wounder if there is any workarround!
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker