UPDATE: This works but there are significant limitations which are described in the comments. This may still be useful in some cirumstances.
UPDATE 2: Nire proiektua uneko, users always upload documents. Baten ondorioz, 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() hartzailea da baliozko balioa bilatzen ez benetan rename egin aurretik, eta orduz geroztik, 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()" metodoa. Horren ordez, erabili behar dugun "MoveTo(…)". Here is a minimal bit of code to accomplish this:
publikoak baliogabetzeko gal ItemAdded(SPItemEventProperties propietate) { 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.
Hau gehiago erabilgarria bertsio gauza bera egiten da, but assigns the name of the file to "Title":
publikoak baliogabetzeko gal ItemAdded(SPItemEventProperties propietate) { DisableEventFiring(); // Esleitu Elementu honen izenburua, fitxategi beraren izena. // OHARRA: Esleipen hau egin behar dugu aldatzeko fitxategi beraren aurretik. // Eguneratzea deituz() SPFile buruzko badirudi propietate baliogabetu ahal izateko // Zentzu batzuk. Updates to "Title" aldaketa huts egin arte (eta eguneratzea() deitu) // ziren aldaketaren aurrean mugitu fitxategi-izenaren. properties.ListItem["Title"] = Properties.ListItem.File.Name; properties.ListItem.Update(); SPFile f = properties.ListItem.File; // Talde fitxategi luzapena. Hori behar dugu, geroago. katea spfileExt = berria FileInfo(f.Name).Luzapena; // Aldatu fitxategiaren zerrenda-elementua ID-eta fitxategi-luzapena erabili eta mantentzeko // dela zati oso-osorik. f.MoveTo(properties.ListItem.ParentList.RootFolder.Url + "/" + properties.ListItem["ID"] + spfileExt); // Entregatu mugimendua. f.Update(); EnableEventFiring(); } |