none
How to replace a file with existing file in SPDocument libray for an SPItem through Object model...

    Question

  • Hello All,

    Step1: I have created an SPItem in document libray by choosing a file through object model.

    Step2: Now i want to replace the existing file of the same SPItem with new file.

    How can i achieve this using SP Object model.

    Thanks in advance...

    Tuesday, September 07, 2010 8:52 AM

All replies

  • use the following function

     

     

    web.Files.Add(URL, content,true);
    
     
    

    web--> is the spweb object

    URk--> document library url + the filename to be added

    eg. http://siteurl/Documents/DocumentName.doc

    content --> byte array object of the file from the file system

    true--> if u want to override else false

     


    Varun Saxena
    • Marked as answer by KeFang Chen Tuesday, September 14, 2010 9:28 AM
    • Unmarked as answer by srikanth ch Wednesday, September 29, 2010 4:28 AM
    Tuesday, September 07, 2010 9:33 AM
  • Hi Varun, thanks for the reply, I have already used this code for the first step of item adding to document library, now in the second time i want to use a different file to be replaced with the existing 1 through SP Object model. we dont have replace method for the Item.File. How can i implement this functionality?
    Best Regards, Srikanth Ch
    Tuesday, September 07, 2010 9:54 AM
  • Hey Srikanth

    y dont u delete(archive/move to a diff doc library) the first file and upload the second file. This would serve the same purpose i suppose.


    Varun Saxena
    Tuesday, September 07, 2010 9:57 AM
  • Hey Srikanth,

                           Don't change the file name which is uploaded earlier, Just get the old file name and upload the new file with same file name

     

     

    Vinod.


    vinod kumar Bhasyam
    Tuesday, September 07, 2010 10:28 AM
  • Varun Saxena gave you the correct API call. 

    web.Files.Add(URL, contents, true ); 

    The boolean value of true tells SharePoint that you want to overwrite the file.  Please see http://msdn.microsoft.com/en-us/library/ms412937.aspx


    My SharePoint Blog - http://www.davehunter.co.uk/blog
    • Marked as answer by KeFang Chen Tuesday, September 14, 2010 9:28 AM
    • Unmarked as answer by srikanth ch Wednesday, September 29, 2010 4:28 AM
    Tuesday, September 07, 2010 10:36 AM
  • Hi,

    You should be able to overwrite an existing file using Dave's and Srikanth reply. It is as simple as that.

    However, to get your existing file name you might want to write a small function to get that

    Function GetFileName() as String

    Dim currentItem as splistitem = listobj.getitembyID(passthecurrentlistitemid)

    MSDN - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splist.getitembyid.aspx

    once you get this you can easily return the file name as string and add that using same file name

    See this - http://www.sharepointdevwiki.com/display/public/Accessing+list+items+using+the+object+model

    look for 'Accessing List Item Instances'

    End Function

    Note - this is psuedocode and you will have to fit the logic to the programming language you are using

    Hope this helps,

    Thanks

    • Marked as answer by KeFang Chen Tuesday, September 14, 2010 9:28 AM
    • Unmarked as answer by srikanth ch Wednesday, September 29, 2010 4:27 AM
    Tuesday, September 07, 2010 11:50 AM
  • hey all, we can overwrite the old file with new file if the file name is same....but in my case file name is different and file type is also different.

    Suggestions would be appreciated..


    Best Regards, Srikanth Ch
    Wednesday, September 08, 2010 4:36 AM
  • Vinod - Your reply is misleading.  When you use SPFileCollection.Add and give a different filename, it will create a new file and will not update the other file.

    My SharePoint Blog - http://www.davehunter.co.uk/blog
    Wednesday, September 08, 2010 10:10 AM
  • Hi all, for the clear explanation...

    I have a document/file in a document library. i am getting the listitem reference using SPListItem item = list.GetItemById(listitemID);

    now i am using the below code to overwrite existing file with new file

             string fn = Path.GetFileName(FileUpload1.PostedFile.FileName);
             Stream fStream = FileUpload1.PostedFile.InputStream;
             byte[] contents = new byte[(int)fStream.Length + 1];
             fStream.Read(contents, 0, (int)fStream.Length);
             fStream.Close();

             SPFolder docFolder = list.RootFolder;
             SPFile docFile = docFolder.Files.Add(fn, contents, true);

             item = docFile.Item;
             item.Update();

    But its not replacing/overwriting the file, instead its creating a new entry in the document library. But i want the same doc library item to be updated. Any suggestion.....


    Best Regards, Srikanth Ch
    Wednesday, September 29, 2010 7:11 AM