Արագ & Հեշտ: Վերանվանել Uploaded ֆայլը SharePoint օբյեկտը մոդել միջոցով Event ստացողի

ԹԱՐՄԱՑՆԵԼ: 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 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.

Սա ավելի օգտակար տարբերակ է, որը չի նույն բանը, 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 f = properties.ListItem.File;

            // Ստացեք երկարաձգում է ֆայլը.  Մենք պետք է, որ ավելի ուշ.
 լարային spfileExt = նոր File Info(f.Name).Երկարացնելը;

            // Վերանվանել ֆայլը է ցանկ Նյութի ծանոթյություններ ID եւ օգտագործման ֆայլի ընդլայնում է պահել
 // որ մասը անձեռնմխելի.
            f.MoveTo(properties.ListItem.ParentList.RootFolder.Url +
                "/" + properties.ListItem["ID"] + spfileExt);

            // Կատարել է քայլ.
            f.Update();

            EnableEventFiring();
        }

4 thoughts on "Արագ & Հեշտ: Վերանվանել Uploaded ֆայլը SharePoint օբյեկտը մոդել միջոցով Event ստացողի

  1. No name
    Նույնպես, Եթե ​​անեք դա ներսից Word, այն կվերադառնա սխալ, եւ ասում են, որ ֆայլը գոյություն չունի. Չի պատմեց նոր URL. Ես դեռ չեմ գտել ճանապարհը մոտ սա.
  2. Paul Galvin
    Thanks for pointing that out. I don’t know an answer to that one either … հետաքրքիր կլինի տեսնել, թե ինչպես պետք է կառավարել այն.
    RE: async & Վերաուղղորդում
    I wonder if moving it to an ItemAdding will solve it. That’s a big hole.
    Շնորհակալություն!
    –Paul
  3. Nadeem Mitha
    Քանի որ ֆայլը ստանում տեղափոխվել asynchronously, կա հնարավորություն, որ Վերահղված է EditForm.aspx առաջ քայլը կատարվել է (եւ դա վատ). 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 Ավելացնել կարծիք Չեղարկել պատասխանել

Ձեր էլ. Փոստի հասցեն չի հրապարակվելու. Պահանջվող դաշտերը նշված են աստղանիշով *