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(); } |