UPDATE: This works but there are significant limitations which are described in the comments. This may still be useful in some cirumstances.
UPDATE 2: Í núverandi verkefni mitt, users always upload documents. Þar af leiðandi, 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() móttökutæki útlit fyrir gilt gildi þar áður en þeir framkvæma endurnefna og síðan þá, 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()" aðferð. Staðinn, við notum "MoveTo(…)". Here is a minimal bit of code to accomplish this:
almennings forgang ógilt ItemAdded(SPItemEventProperties eignir) { 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.
Þetta er meira gagni útgáfa þessi hjartarskinn the sami hlutur, but assigns the name of the file to "Title":
almennings forgang ógilt ItemAdded(SPItemEventProperties eignir) { DisableEventFiring(); // Úthluta titilinn á þessu atriði á nafni skrá sig. // ATHUGIÐ: Þessi verkefni þarf að fara fram áður en við breytt skránni sjálfri. // Starf uppfærslu() á SPFile virðist ógilda eiginleika í // sumir skilningarvit. Updates to "Title" ekki fyrr en þessi breyting (og uppfæra() hringja) // voru flutt í framan the breyting til the skrá nafn. properties.ListItem["Title"] = Properties.ListItem.File.Name; properties.ListItem.Update(); SPFile f = properties.ListItem.File; // Fá framlengingu á skrá. Við þurfum að síðar. band spfileExt = ný FileInfo(f.Name).Framlenging; // Endurnefna skrána til ID á lista yfir atriði og nota skrá eftirnafn til að halda // að hluti af því ósnortinn. f.MoveTo(properties.ListItem.ParentList.RootFolder.Url + "/" + properties.ListItem["ID"] + spfileExt); // Skuldbinda færa. f.Update(); EnableEventFiring(); } |