SharePoint Online Code Snippet Web Part

Recently, a client wanted to share pieces of code on their SharePoint Online site so that employees could easily read and copy them from an internal portal. The SharePoint Online Code Snippet web part is perfect for this scenario because it displays formatted code with syntax highlighting, themes, and line numbers directly on modern SharePoint pages.

In this tutorial, learn how to add the Code Snippet web part, choose a programming language, and use its formatting options like numbered lines, themes, and line wrapping.

What is the Code Snippet web part?

The Code Snippet web part is a modern SharePoint Online web part that lets you display formatted code blocks on a page. It supports multiple programming and markup languages and applies syntax highlighting so the code is easier to read and understand.

This is especially useful for internal documentation, developer portals, training content, and knowledge‑sharing pages where you want to show code examples without users accidentally breaking layout or styling.

Features of the SharePoint Online Code Snippet web part

The Code Snippet web part provides several useful features for displaying code clearly on a SharePoint page:

  • Support for multiple languages such as JavaScript, TypeScript, C#, JSON, HTML, CSS, PowerShell, Python, and more.
  • Syntax highlighting is based on the selected language, so keywords and strings are visually distinct.
  • Optional numbered lines so users can reference specific lines during discussions or reviews.
  • Light and dark theme options to match the look and feel of your site or code style.
  • Line wrapping control to decide whether long lines wrap inside the web part or extend horizontally with a scroll bar.

These settings help you present code in a readable, professional way without needing custom formatting.

Add the SharePoint Online Code Snippet web part

Follow these steps to add the Code Snippet web part to a modern SharePoint Online page:

  1. Open the SharePoint page where you want to display the code and click Edit at the top right.
  2. In the page section where you want to insert the code, click the + icon to add a new web part.
  3. In the web part picker, search for Code Snippet.
  4. Click on the Code Snippet web part to add it to the page.
  5. The Code Snippet web part appears with a default configuration, ready for you to choose a language and paste your code
how to add sharepoint online code snippet web part
  1. The Code Snippet web part is then displayed as shown in the screenshot below.
how to add sharepoint code snippet web part

Check out SharePoint Online Quick Chart Web Part Tutorial

Choose the programming language

The first step in configuring the web part is to select the language so that syntax highlighting works correctly.

  • While the page is in edit mode, click inside the Code Snippet web part.
  • You will see a dropdown where you can choose from various programming and markup languages, including JavaScript, TypeScript, JSON, HTML, CSS, C#, PowerShell, and Python.
  • Select the language that matches the code you want to show.

In the example, Python was selected, and Python code was pasted into the web part. Choosing the correct language ensures that keywords, comments, and strings are highlighted appropriately, making them easier to read.

how to add code snippet web part in sharepoint online

Read SharePoint File and Media Web Part

Add and format the code

Once you have selected the language, you can paste or type your code into the Code Snippet web part.

  • Click inside the code area of the web part.
  • Paste your code or type it directly.
  • As soon as you add the code, the web part applies syntax highlighting based on the selected language.

For example, after choosing Python, code was added with numbered lines and a dark theme, making it easy to read and reference by line number. This is useful when you want to give instructions like “Check line 10 for the function definition.

Here is a screenshot for your reference.

Code snippet web part in sharepoint

Line numbers and theme options

The Code Snippet web part provides options for line numbering and color themes so you can control how the code looks on the page.

Numbered vs unnumbered lines

  • Numbered lines
    • Shows line numbers along the left side of the code block.
    • Ideal for walkthroughs, code reviews, and training content where you want to refer to specific lines.
  • Unnumbered lines
    • Hides line numbers for a cleaner look.
    • Useful when you want a more compact or simple code display.

You can switch between numbered and unnumbered lines using the web part’s formatting options while editing the page.

The Code Snippet web part allows you to switch between dark and light themes so the code block matches your site branding or developer preference.

If we choose Unnumbered lines and Light theme, then the web part will be displayed as shown in the screenshot.

features of sharepoint online code snippet web part

Check out SharePoint Group Calendar Web Part

Control line wrapping

Long lines of code can either wrap automatically or extend horizontally with a scrollbar. The Code Snippet web part includes a Line wrap option that lets you control this behavior.

Line wrap enabled:

  • When Line wrap is enabled (which is the default), long lines of code wrap automatically to the next line within the web part.
  • This avoids horizontal scrolling and works well when you want the code to fit within the width of the page.
  • In the example, when line wrap is enabled, the code is shown neatly within the web part without requiring a horizontal scroll bar.
line wrap in sharepoint online code snippet web part

Line wrap disabled

  • When Line wrap is disabled, long lines remain on a single line.
  • A horizontal scroll bar appears, allowing users to scroll sideways to see the full line.
  • This is useful when preserving the exact line layout is important, such as for aligned columns or long URLs.

Here is a screenshot for your reference.

line wrap in sharepoint code snippet web part

Conclusion

The SharePoint Online Code Snippet web part provides a clean and flexible way to display formatted code on modern SharePoint pages. By selecting the right language, choosing between numbered or unnumbered lines, picking a suitable theme, and controlling line wrap, you can present code examples in a way that is easy for employees to read, understand, and reuse in their own work.

You may like the following tutorials:

  • >

    Build a High-Performance Project Management Site in SharePoint Online

    User registration Power Apps canvas app

    DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

    Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

    Power Platform Tutorial FREE PDF Download

    FREE Power Platform Tutorial PDF

    Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…