Paul Galvin's (old) SharePoint space [SharePoint

Just another site

Quick & Easy: Rename Uploaded File Using SharePoint Object Model Via an Event Receiver


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

UPDATE 2: In my current project, users always upload documents.  As a result, 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() receiver looks for a valid value there before actually performing the rename and since then, 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()" method.  Instead, we use "MoveTo(…)".  Here is a minimal bit of code to accomplish this:


        public override void ItemAdded(SPItemEventProperties properties)
            SPFile f = properties.ListItem.File;

            f.MoveTo(properties.ListItem.ParentList.RootFolder.Url + "/xyzzy.doc");


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.

This is a more useful version that does the same thing, but assigns the name of the file to "Title":

        public override void ItemAdded(SPItemEventProperties properties)

            // Assign the title of this item to the name of file itself.
            // NOTE: This assignment must take place before we modify the file itself.
            // Calling update() on the SPFile seems to invalidate the properties in 
            // some sense.  Updates to "Title" failed until that change (and update() call)
            // were moved in front of the change to the file name.
            properties.ListItem["Title"] = properties.ListItem.File.Name;


            SPFile f = properties.ListItem.File;

            // Get the extension of the file.  We need that later.
            string spfileExt = new FileInfo(f.Name).Extension;

            // Rename the file to the list item's ID and use the file extension to keep
            // that part of it intact.
            f.MoveTo(properties.ListItem.ParentList.RootFolder.Url + 
                "/" + properties.ListItem["ID"] + spfileExt);

            // Commit the move.


The tricky bit there is described in the comment.  That was a bit of a surprise to me, but not hard to manage.

This works well for me "as is" because I don’t have a folder structure to worry about.  If I did, then I’d have to do more coding.

This is all new stuff to me, so if someone sees a flaw or better way to do this, please email me or leave a comment.  Thanks!



4 responses to “Quick & Easy: Rename Uploaded File Using SharePoint Object Model Via an Event Receiver

  1. Nadeem January 26, 2008 at 4:22 pm

    Because the file gets moved asynchronously, there is a possibility of being redirected to EditForm.aspx before the move is committed (and that\’s bad).  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.

  2. Paul January 26, 2008 at 4:36 pm

    Thanks for pointing that out.  I don\’t know an answer to that one either … will be interesting to see how to manage it.
    RE: async & redirection
    I wonder if moving it to an ItemAdding will solve it.  That\’s a big hole.

  3. Unknown February 1, 2008 at 9:25 am

    Also, If you do this from within Word, it will return an error and say the file does not exist. It is not told about the new URL. I have not yet found a way around this.

  4. Directory September 9, 2008 at 7:03 pm

    Great posting on share point or share file!
    I dont get it!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: