తొందరగా & సులభమైన: ఒక ఈవెంట్ స్వీకర్త ద్వారా SharePoint ఆబ్జెక్ట్ మోడల్ ఉపయోగించి అప్లోడ్ ఫైల్ పేరును

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

UPDATE 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 = కొత్త ఫైలు సమాచారం(f.Name).పొడిగింపు;

            // జాబితా అంశం ID ఫైల్ పేరును మార్చి ఉంచాలని ఫైల్ పొడిగింపు ఉపయోగించడానికి
 // అలాగే అది యొక్క భాగాన్ని.
            f.MoveTo(properties.ListItem.ParentList.RootFolder.Url +
                "/" + properties.ListItem["ID"] + spfileExt);

            // ఎత్తుగడ కమిట్.
            f.Update();

            EnableEventFiring();
        }

4 "న ఆలోచనలుతొందరగా & సులభమైన: ఒక ఈవెంట్ స్వీకర్త ద్వారా SharePoint ఆబ్జెక్ట్ మోడల్ ఉపయోగించి అప్లోడ్ ఫైల్ పేరును

  1. సంఖ్య పేరు
    కూడా, మీరు వర్డ్ లోపల నుండి ఇలా చేస్తే, అది ఒక దోషాన్ని మరియు ఫైల్ ఉనికిలో లేదు అని ఉంటుంది. ఇది క్రొత్త URL గురించి చెప్పారు లేదు. నేను ఇంకా ఈ చుట్టూ ఒక మార్గం కనుగొన్నారు లేదు.
  2. పాల్ గాల్విన్
    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.
    ధన్యవాదాలు!
    –పాల్
  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.

ఒక Reply వదిలి నదీమ్ Mitha ప్రత్యుత్తరం రద్దు

మీ ఇమెయిల్ చిరునామా ప్రచురితమైన కాదు. లు గుర్తించబడతాయి *