Cuardaigh & Éasca: Athainmnigh Comhad Uasluchtaithe Ag baint úsáide as Samhail Oibiachta SharePoint Via ar Glacadóir Imeacht

Suas chun dáta: This works but there are significant limitations which are described in the comments. This may still be useful in some cirumstances.

Suas chun dáta 2: I mo tionscadal reatha, users always upload documents. Mar thoradh air sin, 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() Breathnaíonn an glacadóir le haghaidh luach bailí ann roimh iarbhír ag comhlíonadh a athainmniú agus ó shin, 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()" modh. Ina áit sin, úsáidimid "MoveTo(…)". Here is a minimal bit of code to accomplish this:

 poiblí shárú neamhní ItemAdded(SPItemEventProperties airíonna)
        {
            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.

Is é seo an leagan níos úsáidí a dhéanann an rud céanna, but assigns the name of the file to "Title":

 poiblí shárú neamhní ItemAdded(SPItemEventProperties airíonna)
        {
            DisableEventFiring();

            // An teideal na míre seo a shannadh do ainm an chomhaid féin.
 // NÓTA: Ní mór an tasc a chur i bhfeidhm roimh linn a mhodhnú an comhad féin.
 // Ag glaoch ar thabhairt cothrom le dáta() ar an SPFile is cosúil a dhéanamh neamhbhailí na maoine i
 // ciall éigin.  Updates to "Title" Theip ar go dtí an t-athrú (agus nuashonrú() glaoch)
 // Bhí ar athraíodh a ionad i os comhair an t-athrú ar ainm an chomhaid.
            properties.ListItem["Title"] = Properties.ListItem.File.Name;

            properties.ListItem.Update();

            SPFile f = properties.ListItem.File;

            // Faigh an síneadh ar an gcomhad.  Ní mór dúinn go déanaí.
 teaghrán spfileExt = nua Eolas Comhad(f.Name).Síneadh;

            // Athainmnigh an comhad leis an mír den liosta ID agus úsáid a bhaint as an síneadh comhad a choinneáil
 // an chuid sin de sé slán.
            f.MoveTo(properties.ListItem.ParentList.RootFolder.Url +
                "/" + properties.ListItem["ID"] + spfileExt);

            // Tiomantas an t-aistriú.
            f.Update();

            EnableEventFiring();
        }

4 smaointe ar "Cuardaigh & Éasca: Athainmnigh Comhad Uasluchtaithe Ag baint úsáide as Samhail Oibiachta SharePoint Via ar Glacadóir Imeacht

  1. Níl ainm
    Chomh maith leis sin, Má dhéanann tú é seo ó laistigh Word, Beidh sé ar ais earráid agus a rá nach bhfuil an comhad ann. Níl sé inis mar gheall ar an URL nua. Nach bhfuil mé bhfuarthas fós ar bhealach timpeall seo.
    Freagra
  2. Paul Galvin
    Thanks for pointing that out. I don’t know an answer to that one either … Beidh suimiúil a fheiceáil conas é a bhainistiú.
    RE: async & atreorú
    I wonder if moving it to an ItemAdding will solve it. That’s a big hole.
    Go raibh maith agat!
    –Paul
    Freagra
  3. Nadeem Mitha
    Mar gheall ar a fhaigheann an comhad ar athraíodh a ionad asynchronously, tá an deis ann de seo á atreorú go EditForm.aspx sula ndéanfar an t-aistriú tiomanta (agus tá go dona). 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.
    Freagra

Leave a Reply a Nadeem Mitha Cealaigh freagra

Ní thabharfar do sheoladh r-phoist a fhoilsiú. Réimsí riachtanacha atá marcáilte *