1

We need to rename MP4 files as they are uploaded to an asset library. We create an ItemAdded event receiver and attempted to rename the file using file.MoveTo()

  • File being uploaded is called Video.mp4
  • Asset Library we're uploading to is called Location

We were expecting ItemAdded to get called once but it's actually getting called 5 times. The 5 ItemAdded events are then fired in this order:

  1. properties.ListItem refers to a Folder: Location
  2. properties.ListItem refers to a Folder: Location/Video/Preview Images
  3. properties.ListItem refers to a Folder: Location/Video/Additional Content
  4. properties.ListItem refers to a File: Location/Video/Preview Images/Video.mp4
  5. properties.ListItem refers to a File: Location/Video/Video.mp4

Here's how we're attempting to renaming files:

SPFile file = properties.ListItem.File;
string folderName = Path.GetDirectoryName(file.Url).Replace("\\", "/");
string newUrl = folderName + "/" + cleanFileName;
newUrl = newUrl.Replace("Location/", "Loction/FILEPREFIX"); //Prefix the file name
file.MoveTo(newUrl, SPMoveOperations.Overwrite);
file.Update();

and folders:

SPFolder folder = properties.ListItem.Folder;
folder.MoveTo(folder.Url.Replace("Location/", "Location/FILEPREFIX"));

However, renaming folders/files in each of these events causes two things happen:

  • The number of events is reduced to 2
  • Once upload is complete we now see 3 files in the asset library rather than 1

Is this the right approach to rename videos on upload? Perhaps there is a better hook to use, maybe before any of the event receivers fire, or after them? Something in client script?

  • Is using an asset library in place of a generic document library a mandatory requirement? – MdMazzotti Jan 21 '14 at 18:42
  • Yep, it's a mandatory requirement – Daniel Jan 21 '14 at 18:47
4

Because you are uploading a file to an Asset Library, right after clicking "upload" you are shown the edit form, in order to fill some metadata that the library requires. Now, the ItemAdded event (which is *asynchronous *) is probably too early to try to change any of the file properties.

I tried your code inside the ItemUpdated event and it worked correctly.

As a side note, remember that once you clicked the upload button, the file is added to the library in a checked out state and the edit form is presented; when you click save, then it is checked in. That means, there is no way to upload a file with the same name of an already existing file. (To prove this, try to upload a file, then upload it again, but uncheck "overwrite existing files" first. You'll see that the upload fails straight away, before the edit form is shown).

  • I've tried in the ItemAdding and ItemUpdating as well. Neither worked although they did result in a single file. That file was not a playable video – Daniel Jan 21 '14 at 21:12
  • The only one that will work is ItemUpdated. Try it! On my dev machine, it works (I tried with an mp4 video just to make sure) – MdMazzotti Jan 21 '14 at 21:17
  • You're right, ItemUpdated is the only one that works. Thanks! – Daniel Jan 21 '14 at 21:35
1

Are you sure you are not re-firing your own event?

You can disable event firing on update like this:

EventFiringEnabled = false;
// file.Update();
EventFiringEnabled = true;

This link is for 2010, but it should still be valid for 2013:
http://adrianhenke.wordpress.com/2010/01/29/disable-item-events-firing-during-item-update/

  • We have done that – Daniel Jan 21 '14 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.