I need to rename a folder after it is created by a user. I am currently using an event receiver and try to rename the folder in ItemAdding without any success. In ItemAdding event the folder is not persisted yet so accessing it via web.Folders[foldername] gives an error. I tried setting the name in ItemAdded event but ItemAdded is not fired for folders, it only fires for files, I don't know why.

Any help is appreciated.


Usually for changing list items in ItemAdding event receiver you should use AfterProperties. But in this particular situation, I've just tried to change the "Title" property in the ItemAdding event receiver, and it doesn't affect anything, silently swallowing any value without any error.

So I assume, if you don't have time for deep investigations, you might prefer to use something more tricky, for example, replace New Folder button with your own one and capture the execution flow from there.

Assuming you're using SP2010, to replace the New Folder page, you can use custom action approach.

Here is the sample code:

<CustomAction Id="ReplaceNewFolderInDocumentLibraries" Location="CommandUI.Ribbon">
      <CommandUIDefinition Location="Ribbon.Documents.New.NewFolder">
         Image16by16="/_layouts/$Resources:core,Language;/images/formatmap16x16.png" Image16by16Top="-16" Image16by16Left="-248"
         Image32by32="/_layouts/$Resources:core,Language;/images/formatmap32x32.png" Image32by32Top="-448" Image32by32Left="-320"
        CommandAction="javascript: var options = {url:'/_layouts/MyNewFolderForm.aspx', title:'New folder'}; SP.UI.ModalDialog.showModalDialog(options);" />

In this example, the MyNewFolderForm.aspx page with corresponding markup and code for creating a folder must be deployed by using SharePoint Mapped "Layouts" Folder.

To precise the scope of the custom action, you can either use your own document library definition and point the custom action to it by using RegistrationType="List" and RegistrationId="..." attributes, or use code approach, namely SPList.UserCustomActions collection, to add the custom action to the particular document library instance (regardless of it's content type).

P.S. When dealing with Ribbon customizations, don't forget to refresh the browser cache, since some browsers, especially Internet Explorer, tend to cache SharePoint Ribbon. For IE: F12 => Cache => Always refresh cache.

|improve this answer|||||
  • Nice workaround. So how do I get the current folder while creating a sub folder? Should I take it from RootFolder querystring parameter or is there a better way? – Élodie Petit Oct 29 '11 at 23:16
  • After deploying this custom action, New Folder button comes disabled, cannot click on it. Any ideas? – Élodie Petit Oct 30 '11 at 8:24
  • I've tested this piece of code on my local environment, and it really works. The button is available and clickable (even if the aspx page doesn't exist). Try test it on another blank library or something like this. – Andrey Markeev Oct 30 '11 at 9:15
  • 1
    About current folder: RootFolder querystring parameter isn't a bad idea. Just pass it through the script to your page. Btw, to implement this, you can use OOTB SharePoint Javascript function GetUrlKeyValue('RootFolder') in ribbon CommandUIHandler CommandAction. – Andrey Markeev Oct 30 '11 at 9:18
  • Still no luck making that button available. Really strange. – Élodie Petit Oct 30 '11 at 20:24

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.