aarebrot.net Frode's blog on Sharepoint and other stuff

Getting your GUID on, macro style

Posted on February 7, 2011
VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)

I'm currently working on creating some custom discussion boards in SharePoint 2007 for a client. With that comes creation of features and content types, which in turn requires GUIDs to be generated. Generating a GUID in Visual Studio is easy. Just click Tools on the menu, followed by Create GUID. So what's the big deal?

When creating a bunch of content types I find that it becomes cumbersome to click the menu, copy a GUID, then paste it into the document before finally removing braces and dashes. So I've created two short macros to speed up the process with simple keyboard shortcuts. This is mostly a reminder for myself so I remember how to do this next time I'm setting up a new Dev environment, but I'm sure it can come in handy for someone else as well.

Note that I went through this process in Visual Studio 2008, but the steps shouldn't be too different in VS2010.

In Visual Studio click "Tools -> Macros -> Macro Explorer" (or just press Alt+F8). The Macro Explorer should open up. Right click "My Macros" and click "New module...".

Adding a module

Adding a module

Give the new module a name and click Add.

Creating a new macro module

Creating a new macro module

Double click the newly created module to open it in edit mode. Then add the code below into your new module.

Sub Create_GUID_BracesDashes()
    DTE.ActiveDocument.Selection.Text = System.Guid.NewGuid.ToString("B").ToUpper()
End Sub

Sub Create_GUID_NoBracesDashes()
    DTE.ActiveDocument.Selection.Text = System.Guid.NewGuid.ToString("N").ToUpper()
End Sub

There's not a lot of magic going on here. We're creating a new GUID and replacing the current selected text with it. I'm using the B and N style formatting, but there are other options available. MSDN has a complete description of all available newGuid.ToString() formats. You can create one for each if you want, but I find these are the two that I use enough to warrant having a shortcut for.

Depending on what you named it, it should look something like this:

Finished macro script

Finished macro script

Now we just need to map the macros to a set of shortcut keys. Click "Tools -> Options" to open the Options dialog, and select Keyboard in the left hand side. In the "Show commands containing:" text box, type in part, or all, of what you named your module to filter and find your macros.

Creating the shortcuts

Creating the shortcuts

Click the macro you want to assign to a new shortcut key and pick the shortcut you want. I have my macros mapped to Ctrl+G, Ctrl + G and Ctrl+G, Ctrl+C, although to do that you'll have to remove the Edit.Goto default shortcut which is Ctrl+G. Pick whatever you're comfortable with, then click Assign to set that shortcut combo to your macro. Repeat with the second macro.

That's all there is to it. Now you can spit out Content Types faster than you can say GUID rhymes with squid. Have fun.

Getting your GUID on, macro style, 5.0 out of 5 based on 1 rating