Sürətli & Asan: Bugün üçün hadisə Alıcı Via SharePoint Object Model istifadə Uploaded File Rename

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

UPDATE 2: Mənim cari layihə, users always upload documents. Nəticəsində, 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() alıcı əslində adını dəyişmək həyata əvvəl və o vaxtdan bəri orada düzgün değer axtarır, 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()" üsul. Əvəzində, Biz istifadə "MoveTo(…)". Here is a minimal bit of code to accomplish this:

 ictimai alıqlamaq ləğv ItemAdded(SPItemEventProperties xassələri)
        {
            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.

Bu, eyni şey ki, daha faydalı versiyası, but assigns the name of the file to "Title":

 ictimai alıqlamaq ləğv ItemAdded(SPItemEventProperties xassələri)
        {
            DisableEventFiring();

            // Fayl özü adı ilə bu bəndin adı Təyin.
 // QEYD: Biz fayl özü dəyişdirmək əvvəl təyin yer almalıdır.
 // Yeniləmə Calling() ki, SPFile üzrə xüsusiyyətlərini etibarsız görünür
 // müəyyən mənada.  Updates to "Title" dəyişiklik qədər uğursuz (və yeniləmə() çağırmaq)
 // fayl adı dəyişikliyin qarşısında köçürülüb.
            properties.ListItem["Title"] = Properties.ListItem.File.Name;

            properties.ListItem.Update();

            SPFile f = properties.ListItem.File;

            // Fayl uzadılması alın.  Biz sonra ehtiyac.
 sim spfileExt = yeni Fayl haqqında məlumat(f.Name).Artırma;

            // Siyahısı maddənin ID fayl adını dəyişmək və saxlamaq üçün fayl uzantısı istifadə
 // vuruşa onun bir hissəsi.
            f.MoveTo(properties.ListItem.ParentList.RootFolder.Url +
                "/" + properties.ListItem["ID"] + spfileExt);

            // Hərəkət törətmək.
            f.Update();

            EnableEventFiring();
        }

4 Haqqında "fikirləriSürətli & Asan: Bugün üçün hadisə Alıcı Via SharePoint Object Model istifadə Uploaded File Rename

  1. No name
    Həmçinin, Word ərzində bunu edin, bu bir səhv qayıtmaq və fayl mövcud deyil deyəcəklər. Bu yeni URL barədə deyil. Mən hələ bu ətrafında bir yol tapdı yoxdur.
    Cavab
  2. Paul Galvin
    Thanks for pointing that out. I don’t know an answer to that one either … onu idarə etmək üçün necə görmək maraqlı olacaq.
    RE: async & yönlendirme
    I wonder if moving it to an ItemAdding will solve it. That’s a big hole.
    Təşəkkür!
    –Paul
    Cavab
  3. Nadeem Mitha
    Fayl asynchronously köçürülüb olur, çünki, EditForm.aspx yönlendiriliyorsunuz imkanı hərəkət törətmiş əvvəl var (və pis). 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.
    Cavab

Bir cavab buraxın Nadeem Mitha Cavab ləğv

E-poçt ünvanından dərc olunmayacaq. Lazım alanlar qeyd olunur *