A Visual Studio Hack for Sitecore Developers #1

If you use static classes or structs to store Sitecore IDs, sometimes you just copy and paste an existing class/struct and replace the copied IDs with new ones. Having to replace one ID is easy. It turns out sometimes you have to clean 10, 20 or maybe 50 IDs, which is repetitive and time consuming.

In this article I show you how to quickly replace those IDs with empty strings.

Ideally, you should use a code generator such as TDS Classic to automatically generate such classes or perhaphs work with GlassMapper using a Code First approach (or not). However, you might not be familiar with any of these tools or simply want to manually create your “Template” classes.

Usually, a struct class that stores Sitecore IDs looks like the following:

Struct with IDs
Figure 1: Struct with Sitecore IDs

In Visual Studio, you have the Find and Replace functionality which enables you to use Regular Expressions. It is located in Edit > Find and Replace > Quick Replace (or simply Ctrl + H).

Find and Replace
Figure 2: Visual Studio 2017 – Find and Replace

If you want to easily replace all the IDs, simply select the Use Regular Expressions option (Alt + E), use the expression below and replace all the IDs with an empty string.

{.+}

Here’s a demonstration:

And it’s done!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s