Quick & Erraza: Aldatu izena Kargatutako fitxategia SharePoint Object Model erabiliz Gertaera hargailuen bat Via

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

4 buruzko gogoeta "Quick & Erraza: Aldatu izena Kargatutako fitxategia SharePoint Object Model erabiliz Gertaera hargailuen bat Via

  1. Izenik ez
    Era berean,, Hori eginez gero Word etorritako, errore bat itzuliko da esan eta fitxategia ez da existitzen. Ez da berria URL buruz esan. Ez dut oraindik modu bat aurkitu honen inguruan.
    Erantzun
  2. Paul Galvin
    Thanks for pointing that out. I don’t know an answer to that one either … interesgarri nola kudeatu ikusiko dute.
    RE: async & berbideraketak
    I wonder if moving it to an ItemAdding will solve it. That’s a big hole.
    Eskerrik asko!
    –Paul
    Erantzun
  3. Nadeem Mitha
    Fitxategia erabat mugitu delako asinkronoan, Han ari EditForm.aspx birbideratutako aukera bat da mugimendua konpromisoa hartu aurretik (eta hori txarra). 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.
    Erantzun

Utzi iruzkin bat Nadeem Mitha Utzi erantzuna

Zure e-posta helbidea ez da argitaratuko. Beharrezko eremuak markatu dira *