It's activated on button click event. Problem is that it's not creating another item in the list, but it is overwriting the current item, so there is always 1 item, instead of creating item on each click on the button, any idea how to fix it ?

string strDocName = "excel.xlsx";

        using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
            using (SPWeb oWeb = oSite.OpenWeb())
                SPList test = oWeb.GetList("/Lists/PermissionImportLog");
                var testing = test.Fields;
                using (FileStream fs = (new FileInfo(@"C:\test\test2.xlsx")).OpenRead())
                    SPList list = oWeb.GetList("/Lists/PermissionImportLog");

                    Hashtable ht = new Hashtable();
                    ht.Add("Created", DateTime.Now);
                    ht.Add("Permissionurl", oWeb.Url);

                    SPFile destfile = list.RootFolder.Files.Add(strDocName, fs, ht, true);
  • change the filename. Aug 15 '17 at 12:39
  • @DerekGusoff, ye I just checked this, but is there a option to allow the upload of a file with same name and create a new item in the list instead of overwriting this with the same file name?
    – Toni
    Aug 15 '17 at 12:53
  • OK, so if you go to /Lists/PermissionImportLog/excel.xlsx, which document will load? Aug 15 '17 at 17:15
  • @DerekGusoff the first one, but the strange thing in modified column it is said it is modified just now..
    – Toni
    Aug 16 '17 at 7:15

A file path can by definition only point to one document, it's like an address. Suppose you mail a letter to a certain address, and there are two homes with that address. Where does the mail carrier deliver the letter?

So your documents need unique file names.

The setting "overwrite existing files", if left unchecked, will simply fail if you try to upload duplicates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.