Andrew Connell [MVP MOSS]
1353 Posts |  39 Articles |  3385 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
PDCBling
SharePoint Quick Links
Article Categories
Archives
Post Categories

View Andrew Connell's profile on LinkedIn

Add to Technorati Favorites

In Part 1 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. Part 2 discussed CodeRush (CR) templates and showed how they are used. Part 3 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.

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, SPNewGuid, 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.

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:

spnewguid-1

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:

spnewguid-2

If you switch to the Events view of the Property window, you'll see an event named GetString. This is what is called when the StringProvider is invoked. The code in him is quite simple:

spnewguid-3

All he does is grab the parameters and call another method: GenerateNewGuidString(bool, bool):

spnewguid-4

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 Insert StringProvider. This brings up a dialog where you can pick from the different StringProviders installed. For SPNewGuid, you can see it's usage in the newguid template: «?SPNewGuid(false,true)». This is calling the provider saying "no curly brackets, but include dashes". Now, you can type newguid+[SPACE] within any XML file and get a new GUID right away:

newguid

The SPNewGuid StringProvider is in the plugin CR_SPUtilities.dll that is included in the download. There are two other StringProviders in that plugin:

  • WorkflowClassName - Looks for the first type in the current active project open in Visual Studio that implements either System.Workflow.Activities.StateMachineWorkflowActivity or Microsoft.SharePoint.WorkflowActions.SharePointSequentialWorkflowActivity and returns the name of the type (depending on the parameters, it will return the full name [namespace].[typename], or just the type name).
  • EventReceiverClassName - Looks for the first type in the current active project open in Visual Studio that implements either Microsoft.SharePoint.SPItemEventReceiver or Microsoft.SharePoint.SPListEventReceiver and returns the name of the type (depending on the parameters, it will return the full name [namespace].[typename], or just the type name).

There are also a few StringProviders in the CR_ProjectUtilities.dll plugin that may be useful outside of a SharePoint environment:

  • ProjectNamespace - Gets the default namespace configured for the project.
  • ProjectAssemblyName - Gets the default assembly name configured for the project.
  • ProjectAssemblyFourPartName - 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.

To get more information on the tools including documentation, links to detailed screencasts / demonstrations, and a history, check out the main page:

» AC's VS CodeRush/Refactor Tools for SharePoint Developers

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.  :)

posted on Wednesday, August 22, 2007 2:53 PM

Feedback

No comments posted yet.

Post Feedback

Title:
Name:
Email:
(email will not be displayed)
Url:
Comments: 
Please add 3 and 5 and type the answer here:    
All Comments Are Filtered & Moderated
Unfortunately comment spammers are just too effecient and are constantly dirtying up blogs with irrelivant and unwanted comments trying to improve their standing on search engines. All comments on this blog are moderated. I do not censor comments, but I don't approve comments with vulger language or those soliciting products. Most of the time comments are approved within a few hours of being submitted with the only exception when I'm traveling.

Why are you asking for my email address?
The only reason I'm asking for your email address, which isn't required to submit a comment, is to provide a gravatar if you've created an account for yourself and associated your email address with a small image. If you have a gravatar created for the email address you submit, it will appear next to your comment. Otherwise nothing will appear.

What is a gravatar?
A gravatar is a "globally recognized avatar." You can get more information about gravatars, as well as create your own for free, at www.gravatar.com. You can also view my gravatar here.


Copyright © 2003 - 2008 Andrew Connell
Creative Commons License 
This work is licensed under a Creative Commons License
Site design by Heather Solomon.

 
 
MOSS WCM Training
Looking for MOSS 2007 WCM developer training? Look no further! I teach my 4-day hands-on and 5-day online WCM classes for developers I offer through the Ted Pattison Group.

Get more information on the WCM courses!


Upcoming Classes
 Hands-on WCM:
 » Atlanta, GA
   Sept 22-25, 2008
 Online WCM:
 » July 21-25, 2008
 » December 8-12, 2008


» Register today!

JAX Office Geeks
Jacksonville Office Geeks (JOG)
JOG is a special interest group in Jacksonville, FL dedicated to bringing the local SharePoint commnity together to share tips, tricks, ideas and best practices for developing solutions on the SharePoint platform.

Next meeting details...
When:
Thur. Sept 18th, 2008
  6-8p EDT
Topic:
Enterprise Content Management - Document Retention

Speaker:
John Holliday, MVP MOSS

RSVP Today!


» Subscribe to the JOG newsletter