Mabilis & Madali: Palitan ang pangalan ng In-upload ng File Paggamit ng SharePoint Bagay Model Via isang Tatanggap ng Kaganapan

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

4 ano sa tingin mo "Mabilis & Madali: Palitan ang pangalan ng In-upload ng File Paggamit ng SharePoint Bagay Model Via isang Tatanggap ng Kaganapan

  1. Walang pangalan
    Din, Kung gagawin mo ito mula sa loob ng Word, ito ay nagbabalik ng isang error at sabihin ang file ay hindi umiiral. Hindi ito sinabi tungkol sa bagong URL. Hindi ko pa nakita ang isang paraan sa paligid na ito.
    Sumagot
  2. Paul Galvin
    Thanks for pointing that out. I don’t know an answer to that one either … ay magiging interesante sa mga makita kung paano upang pamahalaan ito.
    RE: async & pag-redirect
    I wonder if moving it to an ItemAdding will solve it. That’s a big hole.
    Salamat!
    –Paul
    Sumagot
  3. Nadeem Mitha
    Dahil ang file ay makakakuha ng inilipat asynchronously, doon ay isang posibilidad ng nire-redirect sa EditForm.aspx bago ang paglipat ay nakatuon (at iyon ang masamang). 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.
    Sumagot

Mag-iwan ng Sumagot sa Nadeem Mitha Ikansela reply

Ang iyong email address ay hindi nai-publish. Mga kinakailangang patlang ay minarkahan *