Building Your Visual Studio Extension: A Step-by-Step Guide

Osman Koç
4 min readNov 22, 2023

--

Hi! If you’ve ever wondered, “How can I create my own Visual Studio extension?” — you’re in for a treat. This article is a straightforward guide, providing step-by-step instructions for those familiar with Visual Studio. We’ll dive into the process using a practical example — the GuidGenerator extension.

Crafted with the power of artificial intelligence using designs.ai — a seamless blend of creativity and technology.

Let’s Dive In

1. Setup Basics: Start by ensuring your development environment is ready. Install the Visual Studio SDK and the necessary tools. Open Visual Studio, and let’s embark on this journey.

2. Define Your Goal: What’s the purpose of your extension? To illustrate, let’s take my extension, GuidGenerator, as an example. It simplifies the generation of unique identifiers. Define your goal clearly to guide your development.

3. Create a New Extension Project:

  • In Visual Studio, Install Visual Studio extension development from “Tools” -> “Get Tools and Features”
Visual Studio 2022 — Menu Bar
Visual Studio 2022 — Modiftying Screen
  • Once installed, restart Visual Studio.
  • Create a new project using the “VSIX Project” template.
Visual Studio 2022 — “Create a new project” screen — Step 1
Visual Studio 2022 — “Create a new project” screen — Step 2

4. Coding Magic: Implement Your Feature (GuidGenerator Example)

  • Open the newly created project.
  • Locate the “GuidGeneratorCommand” class. If your project already includes a “Command1.cs” file, you can simply rename it to “GuidGeneratorCommand.cs”.
  • If not, you can create a new one by right-clicking on your project, selecting “Add” -> “New Item” -> “Command” and naming it “GuidGeneratorCommand”.
Visual Studio 2022 — “Add New Item” screen
  • Inside this class, you’ll find the code responsible for handling the extension functionality. Locate the “Execute” method.
  • Customize this method based on the functionality you want to achieve with your extension. For example, for the GuidGenerator extension, your code might look like this:
// ... (Existing code)

/// <summary>
/// This function is the callback used to execute the command when the menu item is clicked.
/// See the constructor to see how the menu item is associated with this function using
/// OleMenuCommandService service and MenuCommand class.
/// </summary>
/// <param name="sender">Event sender.</param>
/// <param name="e">Event args.</param>
private void Execute(object sender, EventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();

var dte2 = (DTE2)Package.GetGlobalService(typeof(SDTE));
if (dte2 == null) return;

var activeDocument = dte2.ActiveDocument;
if (activeDocument == null) return;

var textDocument = activeDocument.Object() as TextDocument;
if (textDocument == null) return;

var sel = textDocument.Selection as TextSelection;
if (sel == null) return;

WriteGuidString(textDocument, sel);
}

// ... (Existing code)

5. Test Your Extension:

  • Press F5 to build and run your extension.
  • A new instance of Visual Studio will launch for testing.
  • Navigate to the “View” menu -> “Other Windows” -> “GuidGeneratorCommand”.
  • Test your extension in various scenarios to ensure functionality.

6. Explore Further with “Extensibility Essentials 2022”:

  • Now that you’ve completed the steps above, your extension project is ready to roll.
  • As a bonus tip, you might want to explore “Extensibility Essentials 2022” for additional convenience.
  • Optionally, discover how this extension can provide helpful templates by navigating to “Extension” -> “Manage Extensions” within Visual Studio.
Visual Studio 2022 — “Manage Extensions” screen

7. Share Your Creation:

  • Consider making your extension open source on platforms like GitHub.
  • Provide clear documentation, including installation steps, features, and customization options.

8. Publish on Visual Studio Marketplace:

  • Package your extension: Right-click on your project, select “Publish,” and follow the prompts to create a VSIX package.
  • Create a Visual Studio Marketplace account: Visit Visual Studio Marketplace and create an account.
  • Upload your extension: Once logged in, go to “Publish Extensions” and follow the submission process.
Visual Studio Marketplace — “Manage Publishers & Extensions” screen
Visual Studio Marketplace — “New Visual Studio Extension” screen

In Conclusion:

And there you have it — a simple guide to building your Visual Studio extension, using the GuidGenerator example. No grand promises, just practical steps for those curious minds looking to customize their coding environment.

** For more insights on the GuidGenerator extension and how to make the most of it, feel free to check out our previous article, Coding Convenience: Open Source Guid Generation Extensions for VS & VS Code.

Happy coding!

--

--

Osman Koç
Osman Koç

Written by Osman Koç

Software Engineer @Turkcell • ex @KocDigital

No responses yet