Microsoft MVP Logo

    <p>In <a href="http://www.andrewconnell.com/blog/Announcing-ACs-VS-CodeRushRefactor-Tools-for-SharePoint-Devs-Part-1-of-5">Part 1</a> of this series on these little tools I created for SharePoint developers I gave a bit of an overview of what they are all about and where you can get additional information. <a href="http://www.andrewconnell.com/blog/Working-with-Templates-in-ACs-VS-CodeRushRefactor-Tools-for-SharePoint-Devs-Part-2-of-5">Part 2</a> discussed CodeRush (CR) templates and showed how they are used. <a href="http://www.andrewconnell.com/blog/MOSS-Publishing-Templates-in-ACs-VS-CodeRushRefactor-Tools-for-SharePoint-Part-3-of-5">Part 3</a> showed some MOSS Publishing-specific templates in the collection. <a href="http://www.andrewconnell.com/blog/Use-of-StringProviders-in-ACs-VS-CodeRushRefactor-Tools-for-SharePoint-Devs-Part-4-of-5">Part 4</a> dig into the guts of how the templates work... specifically around the subject of StringProviders. In this final bit, I want to show how easy it was to build a custom refactoring.</p>
    <p>One of the things that I included in <strong>AC's VS CodeRush/Refactor Tools for SharePoint Devs</strong> is a custom refactoring called <strong>Create Web Part Definition (*.webpart)</strong>. The way it works is if the cursor is on a type that implements <span style="FONT-FAMILY: courier new, courier, monospace">System.Web.UI.WebControls.WebParts.WebPart</span>, this refactoring is available. If activated, it will create a *.webpart file, add it to the project, and fill it up with the contents needed. In addition, it adds the 5-part name of the Web Part type ([namespace].[typename], 4-part name). See for yourself (or view the screencast demo linked on the main page for my tools, linked at the bottom of this post):</p>
    <p align="center"><a title="Photo Sharing" href="http://www.flickr.com/photos/andrewconnell/1204612819/"><img alt="webpartRefactor" width="500" border="0" src="http://farm2.static.flickr.com/1137/1204612819_85f2e34694_o.gif"></a>
    <em>There is a much better, and bigger, screencast available on the main page, linked below.</em></p>
    <p>So, how was this built? Well, it was pretty easy... a lot easier than I thought. First, create a new CR plugin. Then, drag the <span style="FONT-FAMILY: courier new, courier, monospace">RefactoringProvider</span> object from the Toolbox onto the design surface (it's likely not present in your toolbox... you'll need to add it... look for this: <span style="FONT-FAMILY: courier new, courier, monospace">DevExpress.Refactor.Core.RefactoringProvider)</span>. With the provider added, set a few properties as shown here:</p>
    <p align="center"><a title="Photo Sharing" href="http://www.flickr.com/photos/andrewconnell/1204597857/"><img height="427" alt="refactor - 1" width="374" border="0" src="http://farm2.static.flickr.com/1165/1204597857_ee7828ac9b_o.gif"></a></p>
    <p>Now, with the properties set, there are two events that need to be implemented. First, you need to implement the event that checks to see if the refactoring is available (if it shows in the refactoring dropdown) in the current context using the <span style="FONT-FAMILY: courier new, courier, monospace">CheckAvailability</span> event. All this guy does is check to see if the cursor is on a type that implements the desired WebPart type:</p>
    <p align="center"><a title="Photo Sharing" href="http://www.flickr.com/photos/andrewconnell/1205460104/"><img style="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid" height="345" alt="refactor - 2" width="500" src="http://farm2.static.flickr.com/1236/1205460104_e5e612ccdd.jpg"></a></p>
    <p>With the availability set, now we need to add the code that will do the work of the refactoring. The event that triggers the refactoring is <span style="FONT-FAMILY: courier new, courier, monospace">Apply</span>. What this code does is get a reference to the current active project, walk through all types in the project and check each to see if it inherits the <span style="FONT-FAMILY: courier new, courier, monospace">WebPart</span> class. If it finds one (which it should since the refactoring is available), it will create a new file named [webpart_type_name].webpart and write out the contents. Then, it adds the file to the project. Here's the code:</p>
    <p align="center"><a title="Photo Sharing" href="http://www.flickr.com/photos/andrewconnell/1204598235/"><img style="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid" height="369" alt="refactor - 3" width="500" src="http://farm2.static.flickr.com/1199/1204598235_4d291f841a.jpg"></a></p>
    <p>Not too hard huh? The best part: you are no longer restricted to the refactorings provided by Visual Studio... you can create your own using DevExpress' Refactor! Pro.</p>
    <p><em>One thing to note: </em><a href="http://community.devexpress.com/forums/p/56627/190995.aspx#190995"><em>DevExpress has all but stated</em></a><em> that they are likely to change the way the RefactoringProvider object works under the covers. This is something they've never / rarely done, but it would be necessary to make life much easier and provide more power to custom refactoring developers. While this won't break any custom built code, it will break the assemblies built from it. So, all refactorings will need to be rebuilt when the next major release of CR/R comes out. I'll make sure to rebuild my tools when that takes place.</em></p>
    <p>To get more information on the tools including documentation, links to detailed screencasts / demonstrations, and a history, check out the main page:</p>
    <p>&raquo; <a href="http://www.andrewconnell.com/ACs-VS-CodeRush-Refactor-Tools-for-SharePoint-Developers">AC's VS CodeRush/Refactor Tools for SharePoint Developers</a></p>
    <p>Let me know what you think! Oh... and I didn't name this post "part 1 of 5" for some random reason. Over the next four days, I'll add another post related to these productivity enhancements.  :)</p>
    <ul>
    <li><a href="http://www.andrewconnell.com/blog/Announcing-ACs-VS-CodeRushRefactor-Tools-for-SharePoint-Devs-Part-1-of-5">Part 1: Announcing AC's VS CodeRush/Refactor Tools for SharePoint Devs </a></li>
    <li><a href="http://www.andrewconnell.com/blog/Working-with-Templates-in-ACs-VS-CodeRushRefactor-Tools-for-SharePoint-Devs-Part-2-of-5">Part 2: Working with Templates in AC's VS CodeRush/Refactor Tools for SharePoint Devs</a> </li>
    <li><a href="http://www.andrewconnell.com/blog/MOSS-Publishing-Templates-in-ACs-VS-CodeRushRefactor-Tools-for-SharePoint-Part-3-of-5">Part 3: Special Templates in AC's VS CodeRush/Refactor Tools for SharePoint Devs for Publishing Sites </a></li>
    <li><a href="http://www.andrewconnell.com/blog/Use-of-StringProviders-in-ACs-VS-CodeRushRefactor-Tools-for-SharePoint-Devs-Part-4-of-5">Part 4: String Providers in AC's VS CodeRush/Refactor Tools for SharePoint Devs </a></li>
    <li><a href="http://www.andrewconnell.com/blog/Creating-custom-Refactorings-for-use-in-Refactor-Pro-The-quotCreate-Web-Part-Definitionquot-Refactoring-Part-5-of-5">Part 5: Creating custom Refactorings for use in Refactor! Pro: The "Create Web Part Definition" Refactoring</a> </li>
    </ul>
    <div class="wlWriterEditableSmartContent" id="0767317B-992E-4b12-91E0-4F059A8CECA8:367f05fa-6af9-45fc-9b26-1e37cdf908f5" contenteditable="false" style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px">Technorati tags: <a rel="tag" href="http://technorati.com/tags/sharepoint">sharepoint</a>, <a rel="tag" href="http://technorati.com/tags/wss">wss</a>, <a rel="tag" href="http://technorati.com/tags/moss">moss</a>, <a rel="tag" href="http://technorati.com/tags/publishing%20site">publishing site</a>, <a rel="tag" href="http://technorati.com/tags/dxcore">dxcore</a>, <a rel="tag" href="http://technorati.com/tags/coderush">coderush</a>, <a rel="tag" href="http://technorati.com/tags/refactor%20pro">refactor pro</a></div>
Comments powered by Disqus