Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

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).

share|improve this answer
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. –  CodeMonkey Aug 12 '12 at 15:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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