3

I want to replace file to SP library without modify any metadata. Therefore i am trying to get those value before replacing the file and then update those value the original.

However, all error :(

The catch block said that The XXXX file has been modified by AAA on BBB

byte[] fileData= doc.File.OpenBinary(SPOpenBinaryOptions.None); // to download the file from SharePoint in byte[] format

.......

DateTime created = (DateTime)doc["Created"];
DateTime modified = (DateTime)doc["Modified"];
string author = (string)doc["Author"];
string editor = (string)doc["Editor"];
web.Files.Add(Path.Combine(itemsite, itemSubsite, fileURL), fileData, true); // replace my new file to SP library
Console.WriteLine("editor: " + editor);
doc["Created"] = created;
doc["Author"] = author;
doc["Modified"] = modified;
doc["Editor"] = editor;
doc.Update();// error

However, if i don't modify the Created .. modified column:

 byte[] fileData= doc.File.OpenBinary(SPOpenBinaryOptions.None);
 .......
web.Files.Add(Path.Combine(itemsite, itemSubsite, fileURL), fileData, true);
// Done, but the modify name will be changed to mine

Or if i don't edit my file and just modify the Created .. modified column:

DateTime created = (DateTime)doc["Created"];
DateTime modified = (DateTime)doc["Modified"];
string author = (string)doc["Author"];
string editor = (string)doc["Editor"];

doc["Created"] = created;
doc["Author"] = author;
doc["Modified"] = modified;
doc["Editor"] = editor;
doc.Update();
//Done, but the file never been edited.

So, why upload file and update metaData cannot mix together?

Update

SPListItemCollection items = getListItemCollection(oSPWeb, itemlibName);
for (int i = 0; i < items.Count; i++ )
{
var doc = items[i];
byte[] fileData = doc.File.OpenBinary(SPOpenBinaryOptions.None);
........
DateTime created = (DateTime)doc["Created"];
DateTime modified = (DateTime)doc["Modified"];
string author = (string)doc["Author"];
string editor = (string)doc["Editor"]; //StaffA

oSPWeb.Files.Add(Path.Combine(itemsite, itemSubsite, fileURL), fileData, true); // replace my new file to SP library

SPListItemCollection itemForUpdate = getListItemCollection(oSPWeb,  itemlibName);            
itemForUpdate[i]["Created"] = created;
itemForUpdate[i]["Author"] = author;
itemForUpdate[i]["Modified"] = modified;
itemForUpdate[i]["Editor"] = editor;
itemForUpdate[i].Update();

string neweditor = (string)uploadedFiles[i]["Editor"]; // change to my name
}
1
  • have you tried my new code ?
    – Gaurravs
    Sep 30, 2016 at 8:07

2 Answers 2

7

Considering that You are replacing file to SP library using custom code.

If you want to replace file to SP library without modifying Created/Modified by and time stamps, you can use item.SystemUpdate() instead of item.Update()

As per your comment and code, you will need to do following changes:

SPFile file = web.Files.Add(Path.Combine(itemsite, itemSubsite, fileURL), fileData, true);
SPListItem item = file.Item;
item.SystemUpdate();
6
  • sorry, with web.Files.Add(Path.Combine(itemsite, itemSubsite, fileURL), fileData, true); i do not need to use any item.update() / item.SystemUpdate() .. However, it will auto edit the editor columns
    – king yau
    Sep 30, 2016 at 7:48
  • fail.. it changes to my name, not the original
    – king yau
    Sep 30, 2016 at 8:26
  • I hope you are overwriting document, and not uploading new
    – Gaurravs
    Sep 30, 2016 at 8:28
  • i am replacing. In fact, the "Created By" column of the item which i try your way is still keeping the original
    – king yau
    Sep 30, 2016 at 8:30
  • let me re-write full code for you
    – Gaurravs
    Sep 30, 2016 at 8:30
0

you are trying to update an old object and since you have already uploaded new file, it will give conflict error

Try this one

 byte[] fileData= doc.File.OpenBinary(SPOpenBinaryOptions.None); // to download the file from SharePoint in byte[] format

    .......

DateTime created = (DateTime)doc["Created"];
DateTime modified = (DateTime)doc["Modified"];
string author = (string)doc["Author"];
string editor = (string)doc["Editor"];
web.Files.Add(Path.Combine(itemsite, itemSubsite, fileURL), fileData, true); // replace my new file to SP library
Console.WriteLine("editor: " + editor);

doc=list.{Load your Item again}//load doc item again to get updated item.
    doc["Created"] = created;
    doc["Author"] = author;
    doc["Modified"] = modified;
    doc["Editor"] = editor;
    doc.Update();
6
  • The doc.Update(); under web.files.add will bring error to me.
    – king yau
    Sep 30, 2016 at 13:39
  • First run this part of code........ byte[] fileData= doc.File.OpenBinary(SPOpenBinaryOptions.None); ....... web.Files.Add(Path.Combine(itemsite, itemSubsite, fileURL), fileData, true); // Done, but the modify name will be changed to mine
    – Anil Pal
    Sep 30, 2016 at 14:30
  • then load updated item again
    – Anil Pal
    Sep 30, 2016 at 14:31
  • then modify....doc["Created"] = created; doc["Author"] = author; doc["Modified"] = modified; doc["Editor"] = editor; doc.Update();
    – Anil Pal
    Sep 30, 2016 at 14:31
  • it's working for me
    – Anil Pal
    Sep 30, 2016 at 14:31

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.