ઝડપી & સરળ: એક ઇવેન્ટ રીસીવર વાયા શેરપોઈન્ટ ઓબ્જેક્ટ મોડેલ મદદથી અપલોડ કરેલી ફાઇલ નામ બદલો

અપડેટ: 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 = નવું ફાઈલ માહિતી(f.Name).વિસ્તરણ;

            // યાદીમાં આઇટમની ને માટે ફાઈલ નામ બદલો અને રાખવા માટે ફાઇલ એક્સ્ટેંશન ઉપયોગ
 // અખંડ તે છે કે તે ભાગ.
            f.MoveTo(properties.ListItem.ParentList.RootFolder.Url +
                "/" + properties.ListItem["ID"] + spfileExt);

            // ચાલ મોકલવું.
            f.Update();

            EnableEventFiring();
        }

4 પર "વિચારોઝડપી & સરળ: એક ઇવેન્ટ રીસીવર વાયા શેરપોઈન્ટ ઓબ્જેક્ટ મોડેલ મદદથી અપલોડ કરેલી ફાઇલ નામ બદલો

  1. કોઈ નામ
    પણ, તમે વર્ડ અંદર આવું તો, તે એક ભૂલ પાછા આવો અને ફાઈલ અસ્તિત્વમાં નથી કહી કરશે. તે નવા URL વિશે જણાવ્યું નથી. હું હજુ સુધી આ આસપાસ રીતે મળ્યા નથી.
  2. પીડી Galvin
    Thanks for pointing that out. I don’t know an answer to that one either … તે મેનેજ કરવા માટે જુઓ કે કેવી રીતે રસપ્રદ રહેશે.
    નવેસર: async & પુનર્નિર્દેશન
    I wonder if moving it to an ItemAdding will solve it. That’s a big hole.
    આભારદર્શક શબ્દો!
    –પોલ
  3. નાદીમ Mitha
    ફાઈલ અસુમેળ ખસેડવામાં નહીં કારણ કે, 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.

એક જવાબ છોડો નાદીમ Mitha જવાબ રદ કરો

તમારું ઇમેઇલ સરનામું પ્રકાશિત કરવામાં આવશે નહીં. જરૂરી ક્ષેત્રો ચિહ્નિત થયેલ છે *