Perhaps a SharePoint SPFile extension for an existing item?

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(); 
+4
source share
2 answers

After saving the file to the library, use the MoveTo method and pass the changed file name in the newUrl parameter.

SPFile.MoveTo Method (string)
Quick and easy: rename the downloaded file using the SharePoint object model through the event receiver

+4
source

Another way that is easier than using MoveTo is to use the BaseName property of the SPListItem object. You would set this by doing

 item["BaseName"] = resolvedFileName; //Whatever you want the new file name to be item.Update(); 

This is easier than MoveTo because you don’t need to worry about the folder hierarchy and you don’t need to worry about the file extension.

For some reason, the property is not listed in the MSDN documentation , but it seems to work without problems.

+3
source

Source: https://habr.com/ru/post/1338385/