Haraka & Rahisi: Rename Uploaded Picha Kutumia SharePoint Object Model Via kupokea Tukio

UPDATE: This works but there are significant limitations which are described in the comments. This may still be useful in some cirumstances.

UPDATE 2: Katika mradi wangu wa sasa, users always upload documents. Kama matokeo, 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 inaonekana kwa thamani halali ya hapo kabla ya kufanya kweli rename na tangu wakati huo, 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()" njia. Badala yake, sisi kutumia "MoveTo(…)". Here is a minimal bit of code to accomplish this:

 umma override utupu ItemAdded(SPItemEventProperties mali)
        {
            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.

Hili ni toleo la muhimu zaidi kwamba haina kitu kimoja, but assigns the name of the file to "Title":

 umma override utupu ItemAdded(SPItemEventProperties mali)
        {
            DisableEventFiring();

            // Hawawajui ilani ya bidhaa hii kwa jina la faili yenyewe.
 // NOTE: Zoezi hili lazima kuchukua nafasi kabla ya sisi kurekebisha faili yenyewe.
 // Wito update() juu ya SPFile inaonekana kubatilisha mali katika
 // baadhi ya hisia.  Updates to "Title" alishindwa mpaka mabadiliko ambayo (na update() wito)
 // walikuwa wakiongozwa mbele ya mabadiliko ya jina la faili.
            properties.ListItem["Title"] = Properties.ListItem.File.Name;

            properties.ListItem.Update();

            SPFile f = properties.ListItem.File;

            // Kupata ugani ya faili.  Tunahitaji kwamba baadaye.
 string spfileExt = mpya Picha Info(f.Name).Ugani;

            // Rename faili ID bidhaa orodha na kutumia faili ugani kuweka
 // kwamba sehemu yake intact.
            f.MoveTo(properties.ListItem.ParentList.RootFolder.Url +
                "/" + properties.ListItem["ID"] + spfileExt);

            // Kufanya hoja.
            f.Update();

            EnableEventFiring();
        }

4 mawazo juu ya "Haraka & Rahisi: Rename Uploaded Picha Kutumia SharePoint Object Model Via kupokea Tukio

  1. Hakuna jina
    Pia, Kama wewe kufanya hili kutoka ndani ya Neno, atarudi makosa na kusema faili haipo. Ni si aliiambia kuhusu URL mpya. Mimi bado kupatikana kwa njia ya kuzunguka hili.
    Kujibu
  2. Paulo Galvin
    Thanks for pointing that out. I don’t know an answer to that one either … itakuwa ya kuvutia kuona jinsi ya kuisimamia.
    RE: async & redirection
    I wonder if moving it to an ItemAdding will solve it. That’s a big hole.
    Shukrani!
    –Paulo
    Kujibu
  3. Nadeem Mitha
    Kwa sababu faili anapata wakiongozwa asynchronously, kuna uwezekano wa kuwa itaelekezwa EditForm.aspx kabla ya hoja ni nia ya (na kwamba ni mbaya). 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.
    Kujibu

Kuondoka Jibu kwa Nadeem Mitha kufuta reply

Anwani yako si kuchapishwa. Mashamba required ni alama *