I-UPDATE: This works but there are significant limitations which are described in the comments. This may still be useful in some cirumstances.
I-UPDATE 2: Sa aking kasalukuyang proyekto, users always upload documents. Bilang isang resulta, 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() receiver maghahanap ng isang wastong halaga doon bago aktwal na gumaganap ang palitan ang pangalan at mula noon, 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()" pamamaraan. Sa halip, ginagamit namin "MoveTo(…)". Here is a minimal bit of code to accomplish this:
publiko magpawalang-bisa walang bisa ItemAdded(SPItemEventProperties katangian) { 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.
Ito ay isang mas kapaki-pakinabang bersyon na gumagana ang parehong bagay, but assigns the name of the file to "Title":
publiko magpawalang-bisa walang bisa ItemAdded(SPItemEventProperties katangian) { DisableEventFiring(); // Magtalaga ng pamagat ng item na ito upang ang pangalan ng mismong file. // Tandaan: Pagtatalaga na ito ay kailangang maganap bago naming baguhin ang mismong file. // Pagtawag update() sa SPFile Mukhang magpawalang-bisa ang mga katangian sa // ilang mga kahulugan. Updates to "Title" Nabigo ang hanggang sa pagbabagong iyon (at mag-update() tumawag) // ay inilipat sa harap ng mga pagbabago sa mga pangalan ng file. properties.ListItem["Title"] = Properties.ListItem.File.Name; properties.ListItem.Update(); SPFile f = properties.ListItem.File; // Kumuha ng mga extension ng file. Kailangan namin na mamaya. pisi spfileExt = bago File Info(f.Name).Karugtong; // Palitan ng pangalan ang file sa ID sa listahan ng item at gamitin ang file extension upang panatilihing // na bahagi nito buo. f.MoveTo(properties.ListItem.ParentList.RootFolder.Url + "/" + properties.ListItem["ID"] + spfileExt); // Commit ang paglipat. f.Update(); EnableEventFiring(); } |