Change document name in event receiver

I need to rename a document during an ItemUpdating or an ItemUpdated event. Either one would be fine. When my user changes the file name, I want my code to set it to something else. In the examples below, I am trying to set it to a silly constant value.
If I can get this to work, I'll change it to use something meaningful.
Is there any way to change a document's name in an event receiver? I have tried using ItemUpdated (with <Synchronization>Synchronous</Synchronization> in elements.xml). I have tried this:
public override void ItemUpdated(SPItemEventProperties properties)
using (DisabledEventsScope scope = new DisabledEventsScope())
base.ItemUpdated(properties);
try
properties.Web.AllowUnsafeUpdates = true;
properties.ListItem.File.Properties["FileLeafRef"] = "FOOFOO.docx";
properties.ListItem.File.Update();
properties.Web.AllowUnsafeUpdates = false;
catch (Exception ex)
properties.ErrorMessage = "Document cannot be renamed." + ex.Message;
properties.Status = SPEventReceiverStatus.CancelWithError;
//base.ItemUpdated(properties);
I have also tried this:
public override void ItemUpdated(SPItemEventProperties properties)
using (DisabledEventsScope scope = new DisabledEventsScope())
base.ItemUpdated(properties);
try
properties.Web.AllowUnsafeUpdates = true;
properties.AfterProperties["FileLeafRef"] = "FOOFOO.docx";
properties.ListItem.Update();
properties.Web.AllowUnsafeUpdates = false;
catch (Exception ex)
properties.ErrorMessage = "Document cannot be renamed." + ex.Message;
properties.Status = SPEventReceiverStatus.CancelWithError;
//base.ItemUpdated(properties);
And in that last one, I also tried setting properties.ListItem["FileLeafRef"]. I have also tried putting the base.ItemUpdated line after my logic (where you see it commented out above).
Nothing works. No errors are thrown, but the document name stays the way the user set it.
Leigh Webber

There's three things you have to do in order to update the name properties:
Assign properties.ListItem to a variable
Update the BaseName field
Update the Type field (if you wish to change the extension)
public override void ItemUpdated(SPItemEventProperties properties)
EventFiringEnabled = false;
base.ItemUpdated(properties);
var item = properties.ListItem; item["BaseName"] = "BarBar";
item["Type"] = "docx"; //If you need to change the extension
item.Update();
EventFiringEnabled = true;
This should fix your issue

Similar Messages

  • Change document name on HP 6700 using Win 8.1 so that I can save with my documents on hard drive

    change document name on HP 6700 using Win 8.1 so that I can save with my documents on hard drive.
    Am new to this, so help an old man.
    I scan a document, and it comes out as  time and date, and number, but how can i give it a name, so that I can file with other documents, not just the scans.
    Thanks

    Hi,
    You need to install the HP printer software.  The installation process will place a printer icon under the Windows 8.1 Apps menu. Click on the icon and you should be able to see the Advanced setting link.
    HP DV9700, t9300, Nvidia 8600, 4GB, Crucial C300 128GB SSD
    HP Photosmart Premium C309G, HP Photosmart 6520
    HP Touchpad, HP Chromebook 11
    Custom i7-4770k,Z-87, 8GB, Vertex 3 SSD, Samsung EVO SSD, Corsair HX650,GTX 760
    Custom i7-4790k,Z-97, 16GB, Vertex 3 SSD, Plextor M.2 SSD, Samsung EVO SSD, Corsair HX650, GTX 660TI
    Windows 7/8 UEFI/Legacy mode, MBR/GPT

  • I want to change document name.

    Hi, all.
    I want to change document name.
    But the business one can change only AP document.
    AR document can't change their name.
    Is anyone know why only AP documents can change their name.
    Thank you for your help.
    seiichi

    Dear
    In Business one it is only possible to change the purchasing documents. you actually can do it from sdk.  change the system form title and menu name by updating Form.Title and MenuItem.String, as long as you get these UI API
    objects with your code.
    Best Regards,
    xiaodan AN

  • KB fix for print document name in event logs on Server 2012 and Server 2012R2

    It appears as though the requested corrections to the documentation where never honored
    Note After you apply the hotfix or update, you can show the printed document name in the event by enabling a specific Group Policy.
    The policy name:
    Computer Configuration \ Administrative Templates \ Printers
    Allow job name in event logs 
    The Windows 8 / Server 2012 fix is:
    Event ID 307 does not show the printed document name in Windows
    http://support.microsoft.com/kb/2938013/en-us
    2012R2 is in the April Roll up.
    Windows RT 8.1, Windows 8.1, and Windows Server 2012 R2 Update: April 2014
    http://support.microsoft.com/kb/2919355/en-us
    Alan Morris Windows Printing Team

    In my simple test, I see that the Group-Policy does exist on 2012R2 but not for 2012.
    I isolated the registry changes so I can update a 2012 only system... that text is below.
    Windows Registry Editor Version 5.00
    [HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Policies\Microsoft\Windows NT\Printers]
    "ShowJobTitleInEventLogs"=dword:00000001
    [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows NT\Printers]
    "ShowJobTitleInEventLogs"=dword:00000001
    I hope this helps others out since it took sometime to narrow this down. Thank you.

  • Unable to change the name of events in iPoto 08

    Hi.
    I have iPhoto 08, and at first it was fine. But when I transferred my photos from my nikon d40 to my iPod, and then to my iMac from the iPod, i weren´t able too change the name of the events anymore.
    This is very anoying. Hope someone can help me with this.
    Frode

    Frode K
    Welcome to the Apple Discussions.
    Make sure that the HelveticaNeue Font is installed and enabled on your system.
    Regards
    TD

  • Changing Document Name of unsaved File

    Hi,
    is it possible to change the name of an opened but not saved document?
    If I open a photo from ACR as an object, PS will append "as object" to the document name.
    I'd like to delete that part of the document name, BEFORE it will be saved.
    Or is there another way to do that?
    app.activeDocument.name is read only...
    Thanks!

    Try this:
    -X
    var original = app.activeDocument;
    var newDoc = original.duplicate(original.name.replace(' as object', ''));
    original.close(SaveOptions.DONOTSAVECHANGES);

  • Document Library document delete using Event Receiver

    Please suggest your solution for the following scenario:
    A user deletes the document from document library and an e-mail has to be triggered to the admin notifying the information about the document deleted recently along with the option to Approve\Reject. If the Admin approves it, the document stays in Recycle Bin
    and if Rejected, the document gets restored to the document library.
    I came across some articles about 'event receiver' which can trigger an e-mail but couldn't find any coding for the recycle bin and deletion. Is there any other workaround to implement the same?

    Hi,
    In that case, Instead of the deleting the file through code, you can send to recycle using the SPFile.Recycle and SP.CopyTo method.
    SPFile Class
    Murugesa Pandian.,SharePoint 2010| MCPD | MCTS - Configure

  • Change Document Names in Map of Documents

    Hello,
    I am taking the attachments from a completed task and putting them into a map of documents variable.  I was wondering if it is possible to change the names of the documents in the map?  I would like to be able to add an identifier to each document in the map.  Thanks.

    The problem is that you put the id in quotes - which no longer evaluates the statement, but treats it as a litteral.
    To make it easier on my feeble brain (especially when debugging using record/playback) I break it down into multiple steps in the same set value
    /process_data/@myTempID is a string
    /process_data/@myTempDocis a Document
    Location  
    Expression
    /process_data/@myTempID 
    /process_data/MapKeys[1]
    /process_data/@myTempDoc
    /process_data/MapOfDocuments[@id=/process_data/@myTempID]
    /process_data/MapOfDocuments[@id=/process_data/@myTempID]     
    setDocAttribute(/process_data/@myTempDoc, "wsfilename", "test.doc")

  • Changing document name per approval stage

    Hello All,
    I need to use the Purchase Order interface both as a Purchase Requisition and a Purchase Order.
    Basically, I have setup an approval stage for PO. Before the approval takes place, the document is considered a PR and post approval a PO.
    Since, we can create separate print layouts for PR and PO, can the print layouts be tied to the documents approval stage ?
    That is, till such time the PR is approved, only the PR layout is printable, else the PO layout becomes printable automatically.
    Please throw in your ideas too.
    Cheers.

    When you send purchase order(ie)(purchase requisition) for
    approval document is saved as draft,You can print document
    with draft watermark which can be identified as purchase requistion.
    After approval purchase order can be printed automatically
    Wile adding.
    This can be achieved by setting user defaults(Administration>Setup>Users)in which we can set it for a user to print purchase order while adding the document.
    Jeyakanthan

  • How do you change the name of a document in pages on an iPod touch?

    I am on my iPod touch and I want to name my document in Pages. How do I do it?? I know you can, but how??

    nevermind! I figured it out!! You just have to be in where all your documents are shown, hold the NAME (not the document it self, the name of the document) and if you hold it (tap it) for long enough, it will bring up the change document name screen.

  • Cannot Change Name of Events in IPhoto 08

    Recently, IPhotos will no longer allow me to change the names of events. When I click on the "New Event" title I get a dialog box - but I am unable to type anything into the box.
    Any thoughts on this??
    Thanks
    Dennis

    Dennis:
    Welcome to the Apple Discussions. Open Font Book and check to make sure you have Helvetica Neue font installed. If it's there, deactivate and reactivate it. Launch iPhoto and try again. iPhoto uses Helvetica Neue for event titles and other text.
    Do you Twango?
    TIP: For insurance against the iPhoto database corruption that many users have experienced I recommend making a backup copy of the Library6.iPhoto database file and keep it current. If problems crop up where iPhoto suddenly can't see any photos or thinks there are no photos in the library, replacing the working Library6.iPhoto file with the backup will often get the library back. By keeping it current I mean backup after each import and/or any serious editing or work on books, slideshows, calendars, cards, etc. That insures that if a problem pops up and you do need to replace the database file, you'll retain all those efforts. It doesn't take long to make the backup and it's good insurance.
    I've created an Automator workflow application (requires Tiger), iPhoto dB File Backup, that will copy the selected Library6.iPhoto file from your iPhoto Library folder to the Pictures folder, replacing any previous version of it. It's compatible with iPhoto 08 libraries and Leopard. iPhoto does not have to be closed to run the application, just idle. You can download it at Toad's Cellar. Be sure to read the Read Me pdf file.

  • Change document  - Workflow Event Container value not passed

    Hi Iam working on a customer create notification scenario where in we are sending a notification to SAP Inbox whenever a customer is created.We are checking industry key(KNA1-BRSCH) value through a check function module.When the customer is created in the system the system triggers the corresponding even it gets in to check module and an exception is raised saying Industry key does not have any value.
    Problem is Industry key value is not passed on to the container.
    Where might be the problem...?
    Is anything missing in SWEC(Events for change document)
    -but the event is trigerred...container value for industry key not passed...
    Please advice...
    Thanks in advance!

    Refer to link below.
    Sandy

  • Error in changing the name of an event

    When I go to change the name of an event in imovie it gives me this message "Move Failed - This file could not be moved:
    /Users/asrevelli/Movies/iMovie Events.localized/July- - Day 18
    (-5000)
    Anybody know what the deal with this is? I can change the name of projects without a problem but I can't change the name of events. When I try and change it in the finder it tells me that I don't have sufficient access to do that. Please Help!!!

    No, it's not giving me that type of warning. I have had that before. This is an access/privilege type of thing. It seems like it came after I updated to 10.5.7 Leopard. I can't say for sure though.

  • JDBC Adapter-XML Document Name

    I am working on XI 2.0 and need to change the XML document name I am receiving from an inbound JDBC adapter.  The document I am receiving is:
    <resultset>
      <row>
        <col1>ABC</col1>
        <col2>123</col2>
      </row>
    </resultset>
    I want to receive:
    <MyDocumentName>
      <row>
        <col1>ABC</col1>
        <col2>123</col2>
      </row>
    </MyDocumentName>
    Does anyone know how to override the default "resultset" name from the JDBC adapter???  The reason I need to do this is because my message mapping does not work properly unless I have the Message type of the message from my JDBC adapter as "resultset". 
    Thanks for your help!
    Dan Pettingill

    (Note: I work with Dan Pettingill, the starter of this thread)
    I decompiled the class Renato suggested, but I don't
    think that's going to be the solution.  The DB2XMBProcesser is dependent on XI properly setting the properties values for the jdbc adapter.  DB2XMBProcesser will use a property named "db.documentname" to set the name of the root node of the DB2 results. If no such property is defined, the default value of resultSet is used.
    [documentName = getParaWithDefault("db.documentName", "resultset");]
    Our problem seems to be that XI is not passing along the parameters we define in the adapter configuration.  We've tried numerous combinations and names, but they never take. 
    The only way we can get XI to recognize that we are sending in a documentName parm is to call it "docName".  Any other name is essentially ignored (including db.documentname, documentname, XMB.docName, and a lot of other permutations as well).
    But here is the log from that adapter when we do so.  Not that it sees the value we're setting, but it won't set the documentname parm going to the jdbc.
    2004-08-31 17:33:45 : jdbcListener
    Raw configuration data ***
    Transport protocol: JDBC, version
    Message protocol: JDBC, version
    pollInterval|301
    additionalParameters|true
    jdbcDriver|com.ibm.db2.jcc.DB2Driver
    retryInterval|null
    dbpassword|***
    queryStatement|SELECT * FROM RBPRDB.T1256;
    queueName|null
    autoCommit|false
    updateStatement|Delete from RBPRDB.T1256 Where GUID Is Not Null;
    dbdisconnect|false
    dbuser|r50445
    adapterStatus|active
    isolationLevel|2
    addParameterParams|*** TABLE , 2 ROWS DEFINED ***
    addParameterParams|addParameterParams$0$addParameterParamName$docNamespace
    addParameterParams|addParameterParams$0$addParameterParamValue$urn:freescale.com
    /xi/db2-ims/wms
    addParameterParams|addParameterParams$1$addParameterParamName$docName
    addParameterParams|addParameterParams$1$addParameterParamValue$SelectAckQueue
    pollIntervalMsecs|null
    execute|null
    connectionURL|jdbc:db2://WACCTSO.sps.mot.com:9003/WACCDB2T
    qualityOfService|EO
    Used configuration data ***
    'MessageProtocol': 'JDBC'
    'adapterStatus': 'active'
    'addParameterParams.dbEncoding': ''
    'addParameterParams.docName': 'SelectAckQueue'
    'addParameterParams.docNamespace': 'urn:freescale.com/xi/db2-ims/wms'
    'addParameterParams.docNamespaceScope': 'root'
    'additionalParameters': 'true'
    'autoCommit': 'false'
    'connectionURL': 'jdbc:db2://WACCTSO.sps.mot.com:9003/WACCDB2T'
    'dbdisconnect': 'false'
    'dbencoding': ''
    'dbpassword': '***'
    'dbuser': 'r50445'
    'documentname': 'resultset'
    'documentnamespace': ''
    'execute': ''
    'isolationLevel': '2'
    'jdbcDriver': 'com.ibm.db2.jcc.DB2Driver'
    'pollInterval': '301'
    'pollIntervalMsecs': '0'
    'qualityOfService': 'EO'
    'queryStatement': 'SELECT * FROM RBPRDB.T1256;'
    'retryInterval': '301'
    'updateStatement': 'Delete from RBPRDB.T1256 Where GUID Is Not Null;'
    OK - initialized

  • Document name restrictions

    Hi, 
    I was asked to restrict document names. Users mustn't be able to add a file named 'movies.doc' unless it starts with our company's name. It must be 'companyName_movies.doc'. Can this be achieved? I am using SharePoint Online.
    Thank you in advance!

    you can try to do the same by attaching document library with event receiver.
    Check the filename within item adding event and cancel it if its not validate the filename.
    Regards, Dharnendra Shah, MCTS,MCPD - Sharepoint 2010 Application Development Blog: http://ds-sharepoint2010blogs.blogspot.in/ Email: [email protected]

Maybe you are looking for