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:
- properties.ListItem refers to a Folder: Location
- properties.ListItem refers to a Folder: Location/Video/Preview Images
- properties.ListItem refers to a Folder: Location/Video/Additional Content
- properties.ListItem refers to a File: Location/Video/Preview Images/Video.mp4
- 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?