0

I have implemented a system for publishing documents from a document library to a Document Centre. This uses a Send To Connection set up with the "Move and leave a link" option. When this is used the original item in the document library remains unchanged (all metadata including the item ID remains the same), but the file attached to it is replaced with an .aspx file containing a link to the document in the Document Centre. I have now been asked to implement a way of copying the document back from the Document Centre to the original document library. Basically, it should be like the reverse of "Move and leave a link" and replace the .aspx with the document.

I have been able to find the moved document in the Document Centre, there appears to be no way to update the file in the list item (the File property of the SPListItem is read-only).

Is there a way of doing this, or is it impossible to replace the file while keeping the rest of the list item intact (including item ID)? It looks like the Send To connection is capable of this but, of course, it's possible that it is using functionality that is unavailable through the Object Model.

1 Answer 1

1

I found a partial solution to this by using this code (DestinationFile is my own extension method used to get the new file):

SPList list = web.Lists[listId];
SPListItem item = list.GetItemById(itemId);

SPFile newFile = item.DestinationFile(); // Extension method to get the published file

string newUrl = item.File.Url.Replace(item.File.Name,newFile.Name);
SPFile file = item.File;
file.CheckOut();
file.MoveTo(newUrl);
file.SaveBinary(newFile.OpenBinaryStream());
file.Update();
file.CheckIn("Retrieved Document",SPCheckinType.MajorCheckIn);
file.Update();

//get item again to update the metadata
item = list.GetItemById(itemId);
item.File.CheckOut();
item.IconOverlay = ""; // Remove the "shortcut" overlay from the item
item.SystemUpdate(false);

This actually worked and replaced the .aspx file with the Word document, which could be opened and edited in Word with no issues. However, after re-publishing the updated document (with the Send To connection with "Move and leave a link" option), the link ends up corrupted. It creates a .aspx page, but this cannot be opened. Downloading the file shows that it contains a mixture of binary data from the document with text from the .aspx page).

1
  • 1
    You should probably re-cap what solutions you have tried in your question section, and not put it as an answer (like with this one). That will confuse people.
    – user3801
    Commented Aug 12, 2012 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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