Брзи & Лесно: Преименувате поставен фајл со употреба на Sharepoint Object Model Преку Настан ресивер

Ажурирање: This works but there are significant limitations which are described in the comments. This may still be useful in some cirumstances.

Ажурирање 2: Во мојот тековниот проект, users always upload documents. Како резултат на, 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() приемник изгледа за е валидна вредност таму пред всушност извршување на преименување и оттогаш, 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()" метод. Наместо, ние ги користиме "MoveTo(…)". Here is a minimal bit of code to accomplish this:

 јавноста избегне поништат ItemAdded(SPItemEventProperties својства)
        {
            Spfile ѓ = 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.

Ова е многу повеќе корисни верзија која го прави истото, but assigns the name of the file to "Title":

 јавноста избегне поништат ItemAdded(SPItemEventProperties својства)
        {
            DisableEventFiring();

            // Му ја додели титулата на оваа точка до името на датотеката се.
 // ЗАБЕЛЕШКА: Оваа задача мора да се случи пред ние менувате самата датотека.
 // Повикувајќи ажурирање() на spfile чини да се поништи имот во
 // извесна смисла.  Updates to "Title" не успеа до таа промена (и ажурирање() јавете се)
 // беа преместени во предниот дел на промена на името на датотеката.
            properties.ListItem["Title"] = Properties.ListItem.File.Name;

            properties.ListItem.Update();

            Spfile ѓ = properties.ListItem.File;

            // Добие продолжување на датотеката.  Ние треба дека подоцна.
 низа spfileExt = нови FileInfo(f.Name).Продолжување;

            // Преименувајте ја датотеката за да проект на елемент во листата и користи ја наставката да се задржи
 // дека дел од неа непроменети.
            f.MoveTo(properties.ListItem.ParentList.RootFolder.Url +
                "/" + properties.ListItem["ID"] + spfileExt);

            // Извршат во движење.
            f.Update();

            EnableEventFiring();
        }

4 размислувања за "Брзи & Лесно: Преименувате поставен фајл со употреба на Sharepoint Object Model Преку Настан ресивер

  1. Пол Галвин
    Thanks for pointing that out. I don’t know an answer to that one either … will be interesting to see how to manage it.
    RE: async & redirection
    I wonder if moving it to an ItemAdding will solve it. That’s a big hole.
    Благодарение!
    –Paul
  2. Nadeem Mitha
    Because the file gets moved asynchronously, there is a possibility of being redirected to EditForm.aspx before the move is committed (and that’s bad). 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.

Остави Одговори на Nadeem Mitha Откажи одговор

Вашата е-маил адреса нема да бидат објавени. Задолжителни полиња се означени *