UPDATE: This works but there are significant limitations which are described in the comments. This may still be useful in some cirumstances.
UPDATE 2: Fil-proġett kurrenti tiegħi, users always upload documents. Bħala riżultat, 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() riċevitur jistenna għal valur validu hemm qabel fil-fatt twettaq il semmi mill-ġdid u minn dakinhar, 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()" metodu. Minflok, nużaw "MoveTo(…)". Here is a minimal bit of code to accomplish this:
pubbliku override null ItemAdded(SPItemEventProperties proprjetajiet) { 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.
Din hija verżjoni aktar utli li ma l-istess ħaġa, but assigns the name of the file to "Title":
pubbliku override null ItemAdded(SPItemEventProperties proprjetajiet) { DisableEventFiring(); // Jassenja l-titolu ta 'din il-partita għall-isem tal-fajl innifsu. // NOTA: Din il-ħidma għandha sseħħ qabel we jimmodifika l-file nnifsu. // Sejħat aġġornament() fuq il-SPFile jidher li tinvalida l-proprjetajiet // xi sens. Updates to "Title" naqset sakemm dik il-bidla (u l-aġġornament() sejħa) // tmexxew quddiem il-bidla għall-isem ta 'fajl. properties.ListItem["Title"] = Properties.ListItem.File.Name; properties.ListItem.Update(); SPFile f = properties.ListItem.File; // Niżżel l-estensjoni tal-fajl. Għandna bżonn li aktar tard. string spfileExt = ġdid File Info(f.Name).Estensjoni; // Semmi mill-ġdid l-fajl lill ID-oġġett lista u jużaw l-estensjoni fajl biex iżommu // li intatta parti minnu. f.MoveTo(properties.ListItem.ParentList.RootFolder.Url + "/" + properties.ListItem["ID"] + spfileExt); // Jimpenjaw il-moviment. f.Update(); EnableEventFiring(); } |