I am on my way on this. I have a user interface that creates and edits documents stored in a SharePoint document library. The trick is that I need to allow the user to update the document without problems, just use SPFile.SaveBinary() correctly?
This definitely updates the contents of the file, but somehow the old file name and old extension are saved, this is the problem. Removing and re-adding a list item is not a solution either because the item identifier refers to a URL.
My question is, how can I update the extension metadata and file name of the SPFile element?
So far, all my attempts using the object library have failed, I tried updating the fields below, none of them were successful. There seems to be an easier way to do this.
SPFile file = item.File; file.Item[SPBuiltInFieldId.FileLeafRef] = resolvedFileName; file.Item[SPBuiltInFieldId.FileRef] = "/File/" + resolvedFileName; file.Item[SPBuiltInFieldId.BaseName] = System.IO.Path.GetFileNameWithoutExtension(resolvedFileName); file.Item["Name"] = System.IO.Path.GetFileNameWithoutExtension(resolvedFileName); file.SaveBinary(conduitFile); file.Update();
[EDIT] Here is my working solution.
SPFile file = item.File; string resolvedFileName = item.ID.ToString() + "-" + conduitFileName; item["Title"] = resolvedFileName; file.SaveBinary(conduitFile); file.MoveTo(item.ParentList.RootFolder.Url + "/" + resolvedFileName, true); file.Item["Name"] = resolvedFileName; file.Update();
source share