locked
Upload programatically multiple file with same name in doc lib then it shoud not replace the old one. It should create the new entry? RRS feed

  • Question

  • Hi All

    Upload programatically multiple file with same name in doc lib then it shoud not replace the old one. It should create the new entry?

    Below is my code, If file is existing with same name then its replacing the old one.

    But I want to create the new entry with increment ID.

    protected

    void btnUpload_Click(object sender, EventArgs e)

    {

    if (fileUpload.HasFile)

    {

    if (fileUpload.PostedFile.ContentLength > 0)

    {

    string fileName = fileUpload.FileName;

    using (SPWeb oWeb = siteUrl.OpenWeb())

    {

    oWeb.AllowUnsafeUpdates =

    true;

    SPDocumentLibrary docLib = oWeb.Lists["ETS-DocFile"] as SPDocumentLibrary;

    string fileExtension = fileUpload.FileName.Substring(fileUpload.FileName.IndexOf("."));

    byte[] fileByte = fileUpload.FileBytes;

    string destUrl = fileName + fileExtension;

    SPFile destFile = docLib.RootFolder.Files.Add(destUrl, fileByte, true);

    destFile.Update();

    oWeb.AllowUnsafeUpdates =

    false;

    }

    }

    }

    Friday, September 14, 2012 9:44 AM

Answers

All replies

  • Hi Vishwa,

    Why did you post your question two times? it is enough to post only once and we are able to see. Duplicate post just waste memory so delete your second post first..(i have seen your previous threads and you have done same thing in all your threads)..DON'T do it next time.

    Now come to your question, you can not have a file with same name in library so you need to change the name before uploading it. So use below code to check existing file in library and if exist then change the file name and upload it:

    SPFile file = web.GetFile("/site/doclib/filename.ext");
      if (file.Exists)
      {
        ...
      }
    

    Hope it could help



    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Friday, September 14, 2012 10:20 AM
    Moderator
  • Hi Hemendra,

    Thank you so much for your reply. But suppose there are lots of file in library and I am uploading a new file and the name the new file match with any one of the file in doc lib. But I do not know with which file it match.

    SPFile file = web.GetFile("/site/doclib/filename.ext");
    Here the
    filename.ext is static But I can upload any file with different name like: RenameFile.doc and this RenameFile.doc is also existing in library.

    Can you please explore more with the code?

    Is there is any other option, It should not replace the old file, it should create the new entry.

    Thanks a lot.

    Vishwa

    Monday, September 17, 2012 5:17 AM
  • >Here the filename.ext is static But I can upload any file with different name like: RenameFile.doc and this RenameFile.doc is also existing in library.

    It is not static, you need to filename instead of filename.ext. As per your code you can try this:

    string fileName = fileUpload.FileName;
    using (SPWeb oWeb = siteUrl.OpenWeb())
    {
    oWeb.AllowUnsafeUpdates = true;
    
    SPFile file = web.GetFile("/site/doclib/fileName");
      if (file.Exists)
      {
        ...
      }
    
    

    Hope it could help


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Monday, September 17, 2012 5:35 AM
    Moderator
  • Hi Hemendra,

    string

    fileName = fileUpload.FileName;

    using (SPWeb oWeb = siteUrl.OpenWeb())

    {

    oWeb.AllowUnsafeUpdates =

    true;

    SPDocumentLibrary docLib = oWeb.Lists["ETSDocLib"] as SPDocumentLibrary;

    SPFile file = oWeb.GetFile(siteUrl + "/" + docLib + "/" + "Forms" + "/" + "AllItems.aspx" + "/" + fileName);

    if (file.Exists)

    {

    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('The file you are trying to upload is already existing. Please change the file name');", true);

    return;

    } This is url:-http://sharepoint:9494/ETSDocLib/Forms/AllItems.aspx/aaa.txt When I dubugg the code I am getting same url but it is not going inside if loop. Even the file aaa.txt is existing.

    One more thing:- I have created a column named "Name" in doc lib and I want to add items in that column using code -input will take from TextBox on form. As we do with list. Thanks, Vishwa

    Monday, September 17, 2012 8:10 AM
  • Hi,

    >SPFilefile = oWeb.GetFile(siteUrl + "/"+ docLib + "/"+ "Forms"+ "/"+ "AllItems.aspx"+ "/"+ fileName);

    Because your doc url is wrong that's why it is not able to find doc in library. I have given you code in previous post use that. (no need to use forms and AllItems.aspx in url)..Use below one

    SPFile file = oWeb.GetFile(siteUrl + "/" + docLib + "/" + fileName);

    > I have created a column named "Name" in doc lib

    //Add the metadata to File
    Hashtable MetaData
    MetaData.Add([ColumnName], Value);
    
    //Put on your destination file
    SPFileCollection destFiles = web.GetFolder([Folder]).Files;
    destFiles.Add(Path.GetFileName(inputFile.PostedFile.FileName), inputFile.PostedFile.InputStream, MetaData, true);
    
    Hope it could help



    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Tuesday, September 18, 2012 5:15 AM
    Moderator
  • Hi Hemendra,

    Sorry to say but actually I did not understand the concept of metadata.

    Actually I have created a column named "DataTime" Type-Date and Time in my doc file.Now I want, whenever file uploads in doc lib then it should show with current date and time.So that I can find which date and what time file had been uploaded. For that purpose I have used System.DateTime.Now; ex:-

    SPListItem

    items[

    items = list.Items.Add(); "DateTime"] = Systems.DateTime.Now; This is for list. How can I do with doc lib?             Thanks, Vishwa

    Tuesday, September 18, 2012 7:10 AM
  • Hi,

    It is very simple, if you want to add column value with file then use metadata class. In you case you can update datetime like this:

    //Add the metadata to File
    Hashtable MetaData
    MetaData.Add(["Datetime"], DateTime.Now);
    

    See this blog for screen to clear your confusion:

    http://zimmergren.net/technical/how-to-upload-a-filedocument-using-the-sharepoint-object-model

    Hope it could help


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Tuesday, September 18, 2012 7:16 AM
    Moderator
  • Hi Hemendra,

    Thanks a ton for your help and support.

    If I was trying with metadata, getting always file did not fine with given url.

    So I tried to update using SPListItem class and It worked as my requirments.

    SPFile

    destFile = docLib.RootFolder.Files.Add(destUrl, fileByte, true);

    SPListItem item = destFile.Item;

    item[

    "DateTime"] = docDateTime;

    item.Update();

    destFile.Update(); Thanks, Vishwa

    Tuesday, September 18, 2012 9:19 AM