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. In this post I want to dig into some of the guts of how they work... specifically around the subject of StringProviders.</p>
    <p>StringProviders, a CR thing, are like methods that return strings. That's it... yes, they are that simple. I used them in quite a few places in the templates. The most commonly used one, <strong>SPNewGuid</strong>, is probably the easiest to explain. Before I go on, let me say yes, I am aware there is a NewGuid StringProvider added recently in the stock CR install. Unfortunately it doesn't provide the customization for what we need in SharePoint CAML files, so I created my own.</p>
    <p>To create a StringProvider, create a new plugin project in Visual Studio (VS). Then, drag the StringProvider object from the Toolbox onto the design surface. Looking at it's properties, there are a few things you can set:</p>
    <p align="center"><a title="Photo Sharing" href="http://www.flickr.com/photos/andrewconnell/1205171310/"><img height="294" alt="spnewguid-1" width="403" border="0" src="http://farm2.static.flickr.com/1251/1205171310_ecfbde19c1_o.gif"></a></p>
    <p>Take note of the ProviderName property. Now, look at the parameters by clicking on the builder [...] button. There are two parameters that allow you to specify if the GUID generated should have the curly brackets "{}" and / or the dashes. Why have these options? Well, we don't use curly brackets anywhere in SharePoint CAML files except when referring to site column / field definitions and we don't want curly brackets or dashes when creating new GUIDs for use within content type IDs:</p>
    <p align="center"><a title="Photo Sharing" href="http://www.flickr.com/photos/andrewconnell/1204309793/"><img style="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid" height="357" alt="spnewguid-2" width="500" src="http://farm2.static.flickr.com/1070/1204309793_09093dbc0d.jpg"></a></p>
    <p>If you switch to the Events view of the Property window, you'll see an event named <span style="FONT-FAMILY: courier new, courier, monospace">GetString</span>. This is what is called when the StringProvider is invoked. The code in him is quite simple:</p>
    <p align="center"><a title="Photo Sharing" href="http://www.flickr.com/photos/andrewconnell/1205171132/"><img style="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid" height="110" alt="spnewguid-3" width="500" src="http://farm2.static.flickr.com/1141/1205171132_3aac924575.jpg"></a></p>
    <p>All he does is grab the parameters and call another method: GenerateNewGuidString(bool, bool):</p>
    <p align="center"><a title="Photo Sharing" href="http://www.flickr.com/photos/andrewconnell/1205171436/"><img style="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid" height="189" alt="spnewguid-4" width="500" src="http://farm2.static.flickr.com/1351/1205171436_819fe188ba.jpg"></a></p>
    <p>OK... once this is built and deployed, how do you use it? When creating a new template, right-click in the area where the code is going and select <strong>Insert StringProvider</strong>. This brings up a dialog where you can pick from the different StringProviders installed. For SPNewGuid, you can see it's usage in the <strong>n</strong><span style="FONT-SIZE: 11px"><strong>ewguid</strong> </span>template: <span style="FONT-FAMILY: courier new, courier, monospace">&laquo;?SPNewGuid(false,true)&raquo;</span>. This is calling the provider saying "no curly brackets, but include dashes". Now, you can type <strong>newguid+[SPACE]</strong> within any XML file and get a new GUID right away:</p>
    <p align="center"><a title="Photo Sharing" href="http://www.flickr.com/photos/andrewconnell/1198852843/"><img style="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid" height="232" alt="newguid" width="532" src="http://farm2.static.flickr.com/1280/1198852843_49e7e9e5dd_o.gif"></a></p>
    <p>The SPNewGuid StringProvider is in the plugin CR_SPUtilities.dll that is included in the download. There are two other StringProviders in that plugin:</p>
    <ul>
    <li><strong>WorkflowClassName</strong> - Looks for the first type in the current active project open in Visual Studio that implements either <span style="FONT-FAMILY: courier new, courier, monospace">System.Workflow.Activities.StateMachineWorkflowActivity</span> or <span style="FONT-FAMILY: courier new, courier, monospace">Microsoft.SharePoint.WorkflowActions.SharePointSequentialWorkflowActivity</span> and returns the name of the type (depending on the parameters, it will return the full name [namespace].[typename], or just the type name). </li>
    <li><strong>EventReceiverClassName</strong> - Looks for the first type in the current active project open in Visual Studio that implements either <span style="FONT-FAMILY: courier new, courier, monospace">Microsoft.SharePoint.SPItemEventReceiver</span> or <span style="FONT-FAMILY: courier new, courier, monospace">Microsoft.SharePoint.SPListEventReceiver</span> and returns the name of the type (depending on the parameters, it will return the full name [namespace].[typename], or just the type name). </li>
    </ul>
    <p>There are also a few StringProviders in the CR_ProjectUtilities.dll plugin that may be useful outside of a SharePoint environment:</p>
    <ul>
    <li><strong>ProjectNamespace</strong> - Gets the default namespace configured for the project. </li>
    <li><strong>ProjectAssemblyName</strong> - Gets the default assembly name configured for the project. </li>
    <li><strong>ProjectAssemblyFourPartName</strong> - If the project has been built, retrieves the strong name (4-part name) of the assembly via reflection. If it isn't built, it builds the 4-part name, but adds a token for the PublicKeyToken part to be updated later. </li>
    </ul>
    <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