HOWTO: Copy text to clipboard in VSTS/TFS Extensions

If you have used TFS/VSTS, you might have used “Copy to Clipboard” option at least few times - either on the workitem context menu or to copy the repository URL on Clone Repository dialog . In this blog post we will see how to implement that functionality and copy text to clipboard from your VSTS/TFS extensions using Microsoft Visual Studio Team Services Web Extension SDK and Typescript.

Steps

The first step - is to import the required Clipboard module from Utils.

importClipboard=require("VSS/Utils/Clipboard");

Next step - Call copyToClipboard(...) method and pass the text you want to copy to clipboard.

How to copy text as HTML

copyToClipboard(...) method takes additional parameter of type IClipboardOptions. This IClipboardOptions object has additional properties and one of those is copyAsHtml. Setting this to true allows text to be copied as HTML.

Utkarsh Shigihalli

Utkarsh is passionate about software development and has more than 12 years of work experience in the areas of Azure, VSTS, C# and TypeScript. He has developed numerous extensions for Visual Studio and Visual Studio Team Services. He is currently working as a Consultant in the United States.