UPDATE: This works but there are significant limitations which are described in the comments. This may still be useful in some cirumstances.
UPDATE 2: U mom trenutnom projektu, users always upload documents. Kao rezultat toga, 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() prijemnik traži valjane vrijednosti tamo prije nego što zapravo obavljanje preimenovati i od tada, 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()" način. Umjesto toga, koristimo "MoveTo(…)". Here is a minimal bit of code to accomplish this:
javni nadjačati poništiti ItemAdded(SPItemEventProperties Svojstva) { 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.
To je više koristan verzija koja čini istu stvar, but assigns the name of the file to "Title":
javni nadjačati poništiti ItemAdded(SPItemEventProperties Svojstva) { DisableEventFiring(); // Dodjela naslov ove točke na naziv datoteke sama. // NAPOMENA: Ovaj zadatak se mora dogoditi prije izmijeniti datoteku. // Pozivanje ažuriranje() na SPFile čini se izgubiti u svojstvima // neki osjećaj. Updates to "Title" nije do tog promjene (i ažuriranje() pozvati) // su se preselili ispred promjenu naziva datoteke. properties.ListItem["Title"] = Properties.ListItem.File.Name; properties.ListItem.Update(); SPFile f = properties.ListItem.File; // Uzmite produljenje datoteke. Trebamo kasnije. niz spfileExt = novi FileInfo(f.Name).Produžetak; // Promijenite naziv datoteke u popisu stavke ID i koristiti ekstenziju zadržati // da je dio njega netaknuti. f.MoveTo(properties.ListItem.ParentList.RootFolder.Url + "/" + properties.ListItem["ID"] + spfileExt); // Obvezati na potez. f.Update(); EnableEventFiring(); } |