Quick & Easy: Endurnefna skrá innsend nota SharePoint Object Model gegnum Event Receiver

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 =  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();
        }

4 hugsanir á "Quick & Easy: Endurnefna skrá innsend nota SharePoint Object Model gegnum Event Receiver

  1. Ekkert nafn
    Einnig, Ef þú gerir þetta innan frá Word, það mun skila villu og segja skráin er ekki til. Það er ekki sagt um nýja vefslóð. Ég hef ekki enn fundið leið í kringum þetta.
    Svara
  2. Paul Galvin
    Thanks for pointing that out. I don’t know an answer to that one either … verður áhugavert að sjá hvernig á að stjórna henni.
    RE: async & utanáskrift
    I wonder if moving it to an ItemAdding will solve it. That’s a big hole.
    Takk!
    –Paul
    Svara
  3. Nadeem Mitha
    Því að skrá fær flutt asynchronously, það er möguleiki á að vera vísað til EditForm.aspx áður en ferðinni er skuldbundinn (og það er slæmt). You’ll see that this happens if you make the code sleep before the MoveTo line. I still haven’t figured out what can be done about that problem aside from using a customized edit form.
    Svara

Leyfi svar við Nadeem Mitha Hætta við svar

Netfangið þitt verður ekki birt. Nauðsynlegir reitir eru merktir *