Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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">
  <CommandUIExtension>
    <CommandUIDefinitions>
      <CommandUIDefinition Location="Ribbon.Documents.New.NewFolder">
        <Button
         Id="Ribbon.Documents.New.NewFolder"
         Sequence="30"
         Command="MyNewFolder"
         LabelText="$Resources:core,cui_ButNewFolder;"
         Image16by16="/_layouts/$Resources:core,Language;/images/formatmap16x16.png" Image16by16Top="-16" Image16by16Left="-248"
         Image32by32="/_layouts/$Resources:core,Language;/images/formatmap32x32.png" Image32by32Top="-448" Image32by32Left="-320"
         ToolTipTitle="$Resources:core,cui_ButNewFolder;"
         ToolTipDescription="$Resources:core,cui_STT_ButNewLibraryFolder;"
         TemplateAlias="o1"
          />
      </CommandUIDefinition>
    </CommandUIDefinitions>
    <CommandUIHandlers>
      <CommandUIHandler
        Command="MyNewFolder"
        CommandAction="javascript: var options = {url:'/_layouts/MyNewFolderForm.aspx', title:'New folder'}; SP.UI.ModalDialog.showModalDialog(options);" />
    </CommandUIHandlers>
  </CommandUIExtension>
</CustomAction>

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.

share|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
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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