Code Snippet Web Part
Benefits
- Readable code on any page: syntax highlighting makes scripts and samples far easier to scan than plain text
- Built for IT knowledge bases: PowerShell snippets, configuration samples, and how-to examples live right on the wiki page
- Copy-friendly: readers grab working code straight from the page instead of retyping it from screenshots
- Light or dark theme: match the code block to your page design
- No custom code required: displaying code is pure configuration, not development
Settings
- Paste and pick: drop your code into the web part and choose the language for highlighting
- Line numbers: toggle numbered lines for easy reference in instructions and code reviews
- Theme choice: render the block in a light or dark theme
- Display only: the web part shows code as text; nothing on the page executes
Limits and Nuances
- Wide language support: popular languages such as PowerShell, JavaScript, Python, JSON, and SQL render with proper highlighting, and plain text covers everything else
- Keep snippets focused: long files belong in a repository or document library; the web part shines for short, instructional samples
- Pair it with prose: a snippet plus a Text or Markdown web part explaining it beats a wall of unexplained code
Code Snippet Web Part vs. the Alternatives
- Code Snippet vs. Text web part: the Text web part can style short inline code, but without highlighting or line numbers; Code Snippet treats code as a first-class block.
- Code Snippet vs. Markdown web part: Markdown handles prose that contains some code; Code Snippet is the dedicated display for the code itself, with language and theme controls.
- Code Snippet vs. attaching a script file: a file in a library must be downloaded and opened; a snippet on the page is read in context, right where the instructions are.
- Code Snippet vs. the old Script Editor: the Script Editor used to run code on the page; modern SharePoint pages intentionally do not, and Code Snippet exists to present code, not execute it.
Common Questions About the Code Snippet Web Part
What is the Code Snippet web part used for?
Displaying code on a SharePoint page so people can read, learn from, and copy it. IT teams use it for PowerShell snippets in knowledge bases, developers for configuration samples in wikis, and trainers for step-by-step examples. The web part adds syntax highlighting, optional line numbers, and a light or dark theme, so code looks intentional on the page rather than pasted in as plain text.
Can the Code Snippet web part run scripts on the page?
No – it displays code, it never executes it. Modern SharePoint pages do not run embedded scripts at all; that is a deliberate security design of the modern page model. If you remember the Script Editor web part, this is not its successor – the Code Snippet web part exists for sharing code as readable content, which is exactly what an intranet knowledge base needs.
What languages does the Code Snippet web part support?
A long list of popular languages – PowerShell, JavaScript, Python, JSON, SQL, and many others – each rendered with proper syntax highlighting. You pick the language on the web part’s toolbar, along with line numbers and a light or dark theme. If a language is not on the list, the plain text option still presents the code in a clean, monospaced block.
Can readers copy the code from the page?
Yes – the snippet is real text, so readers select and copy it straight into their editor or console. That is the web part’s quiet superpower compared with the common alternative of posting screenshots of code, which cannot be copied, cannot be searched, and go blurry on mobile screens. Working snippets on the page mean fewer retyping errors and faster help-desk resolutions.
When should I use the Code Snippet web part instead of the Text web part?
Whenever code is the content. The Text web part is for prose and can style a short inline command, but it offers no syntax highlighting, no line numbers, and no code theme. The moment you are sharing more than a one-liner – a script, a configuration block, a query – the Code Snippet web part presents it properly. Many pages use both: Text for the explanation, Code Snippet for the code.
Can Greg build this for our organization?
Yes – this is exactly the kind of work Greg Zelfond does. As an independent SharePoint and Microsoft 365 consultant and Microsoft MVP, he designs IT knowledge bases and technical wikis where documentation and code samples live side by side, like the knowledge management examples on LookBook 365. Reach out through the contact page to get started.