How to Share Code in Microsoft Teams with Code Blocks

C
Collab365 TeamAuthorPublished Mar 30, 2026
2,358

At a Glance

Target Audience
Developers, administrators and technical teams collaborating in Microsoft Teams
Problem Solved
Sharing readable code in the current Teams composer without relying on the obsolete Code Snippet dialog or exposing secrets.
Use Case
Sending small code examples, commands, logs and collaborative drafts in Teams chats and channels.

How to Share Code in Microsoft Teams with Code Blocks

Microsoft Teams now uses Code Block in the message composer for readable multi-line code.

On desktop or the web, the quickest method is to type /code in the compose box. You can also type three backticks, use the keyboard shortcut, or open the formatting toolbar and select Code Block.

The older version of this article taught a separate Code Snippet window. That is no longer the current workflow.

Insert a code block

Microsoft's current Code Block instructions give four desktop/web routes:

  1. Type /code in the compose box.
  2. Type three backticks: ````` ``.
  3. Press Ctrl + Shift + Alt + B on Windows or the web, or Cmd + Shift + Option + B on Mac.
  4. Select Show formatting options, then select Code Block.

Choose whichever route is easiest to remember. They open the same kind of composer block.

Paste or type the code, choose the correct language from the dropdown, check the preview, then send the message.

Teams remembers the last language you selected. That is convenient until it is wrong: if highlighting looks odd, check the language before deciding the code itself is malformed. Use Plain Text for logs, configuration fragments or a language that is not listed.

Inline code or a code block?

Use inline code for a command, property or short expression inside a sentence. Put one backtick on each side:

Run `Get-ChildItem` and send me the error.

Use a Code Block for multiple lines, indentation or anything someone may copy and run:

```powershell
Get-ChildItem -Path . -File
```

Teams supports a Markdown-style syntax, but Microsoft warns that it does not match every part of standard Markdown. The live Teams Markdown guide is the safe reference when a familiar Markdown sequence behaves differently.

What Code Block gives you

For code blocks created on desktop, Microsoft documents:

  • line numbers;
  • syntax highlighting based on the selected language;
  • automatic or selectable text wrapping;
  • direct copying from a sent message; and
  • mobile rendering.

Code blocks can only be created on desktop or the web. Mobile users can read code blocks created there, but should not be told that the full creation experience is available in the mobile composer.

For long lines, open More options inside the block and select Wrap text. Whether wrapping is helpful depends on the content: it improves reading for a long command, but can make column-aligned logs harder to inspect.

Make a code block collaborative

Before sending, open More options inside the code block and select Make this collaborative. Teams turns it into a Loop component so people with the required permission can edit it together.

Use collaborative code for a small draft that genuinely needs live co-editing. Do not use it as a replacement for source control, review history, testing or deployment approval.

Microsoft notes two important limits:

  • the collaborative option is available before the block is sent; and
  • code over 30,000 characters pasted into a block is converted to a collaborative code block.

If a participant cannot edit, check Loop permissions and tenant policy. Do not assume chat membership automatically grants every Loop permission.

Do not paste secrets into a code block

Readable formatting does not make content safe.

Before sending, remove or replace:

  • API keys, passwords and connection strings;
  • access tokens, cookies and signed URLs;
  • personal data from logs;
  • customer or tenant identifiers that are not needed;
  • internal hostnames and file paths that expand the disclosure; and
  • private certificates or keys.

Use placeholders such as YOUR_API_KEY, contoso.sharepoint.com or <tenant-id>. If a secret is pasted accidentally, deleting the message is not enough: revoke or rotate the credential and follow the incident process.

Give the reader enough context

A perfect code block without context still wastes time. Add:

  • the language and runtime;
  • what the code is meant to do;
  • the exact error or unexpected result;
  • what you already tried;
  • whether the example is complete or reduced; and
  • the safe next action.

For example:

PowerShell 7.4 on Windows. This reduced example should list files but returns access denied on the second path. The credential and real paths have been removed.

Then add the block. This helps someone reproduce the problem without guessing what they are looking at.

When Teams is the wrong place

Use a Teams code block for discussion, a small reproduction, a command or a review comment.

Use a repository or governed file when the content is:

  • a real application or script;
  • long enough that version history matters;
  • subject to pull-request review;
  • reused by automation;
  • covered by deployment controls; or
  • likely to outlive the chat.

Share a link to the exact file, commit or pull request, then use the chat block only for the relevant extract. That preserves context without creating a second unofficial copy of the code.

Code Block is not the Adaptive Card CodeBlock

Microsoft also documents a developer CodeBlock element for Adaptive Cards in Teams. It lets an app or bot render formatted code in a card.

That is a separate developer surface. It is not the button a person selects in the normal chat composer. If you are building a Teams app, use the Adaptive Card CodeBlock documentation and test the documented client limitations.

If you cannot see Code Block

  1. Use Teams desktop or the web rather than trying to create it on mobile.
  2. Type /code or three backticks to bypass a hidden toolbar button.
  3. Expand the compose formatting options.
  4. Update or restart Teams and compare with the web client.
  5. Ask an administrator whether messaging or Loop policy affects the required feature.

Do not return to screenshots of the old Code Snippet dialog and assume the tenant is broken.

For a broader tour of the composer, see How to use the formatting toolbar in Microsoft Teams.

For practical Teams and SharePoint administration help, join the SharePoint & Teams Admins Space.

Frequently asked questions

How do I insert a code block in Microsoft Teams?

On desktop or the web, type /code, type three backticks, use the documented keyboard shortcut, or open formatting options and select Code Block. Then choose a language and send the message.

Can I create a Teams code block on mobile?

No. Microsoft says code blocks can only be created on desktop. Blocks created there render on mobile with formatting for the reader.

Does Microsoft Teams support syntax highlighting?

Yes. Choose a language in the Code Block dropdown. If highlighting looks wrong, select the correct language or use Plain Text for logs and unsupported formats.

Can several people edit the same code block?

Before sending, use More options > Make this collaborative. Teams converts it to a Loop component, and each participant still needs the required Loop permissions.

Should I share a whole script in Teams?

Usually not when it is long-lived or deployed. Keep the source of truth in a repository or governed file, link to the exact version, and paste only the small section needed for the conversation.