UPDATE: This works but there are significant limitations which are described in the comments. This may still be useful in some cirumstances.
UPDATE 2: In current project, users always upload documents. Ut ex, I don’t run into a problem where MS Word is running and thinks that the file was renamed on it. I did run into a problem, "the file was modified by someone else" and solved this via a simple semaphore type flag. Users need to change a meta data field from its default value to something else. The itemupdated() accipientem spectat ad validam valorem ante TRANSNOMINO in actu perfecto, et tunc cum, I have not had any problems. Your mileage may vary.
I have a client requirement to change the name of files uploaded to a specific document library to conform with a particular naming convention. The API does not provide a "rename()" methodo. Pro, Utimur "MoveTo(…)". Here is a minimal bit of code to accomplish this:
publica dominari Irrita ItemAdded(SPItemEventProperties Proprietates) { SPFile F = properties.ListItem.File; f.MoveTo(properties.ListItem.ParentList.RootFolder.Url + "/xyzzy.doc"); f.Update(); } |
The only tricky bit is the "properties.ListItem.ParentList.RootFolder.Url". The MoveTo() method requires a URL. That mashed up string points me to the root folder of my current document library. This allows me to avoid any hard coding in my event receiver.
Latin utilior est idem facit, but assigns the name of the file to "Title":
publica dominari Irrita ItemAdded(SPItemEventProperties Proprietates) { DisableEventFiring(); // Titulus autem huius assignant item nomen file ipsum. // MONUMENTUM: Hac assignatione fieri oportet ante se temperare file. // Vocans update() in SPFile videtur infirmatione proprietatum // quaedam. Updates to "Title" donec deficerent mutabilibus (et update() voca) // Nomen fasciculi ad commutationem pro motis. properties.ListItem["Title"] = Properties.ListItem.File.Name; properties.ListItem.Update(); SPFile F = properties.ListItem.File; // Impetro file extensionem. Nos postulo ut postea. filum spfileExt = novum File Info(f.Name).Extensio; // TRANSNOMINO lima ut album Item ID scriptor utor lima tractus custodire // intactam illam partem. f.MoveTo(properties.ListItem.ParentList.RootFolder.Url + "/" + properties.ListItem["ID"] + spfileExt); // Committere movéntur. f.Update(); EnableEventFiring(); } |