Sample: Script widget

Overview

Use the Script widget sample to create a custom widget that you can use to dynamically insert scripts inside the page.

You can choose from the following options:

  • Inline
    In this mode the Script widget renders the content of the text area in the widget designer on the place where the widget is dropped in the WYSIWYG editor
  • BodyTop
    In this mode the Script widget inserts the content of the text area in the widget designer at the beginning of the <body> tag.
  • BodyBottom
    In this mode the Script widget inserts the content of the text area in the widget designer at the end of the <body> tag.

PREREQUISITES: You must set up a Sitefinity renderer application and connect it to your Sitefinity CMS application. For more information, see Install Sitefinity in ASP.NET Core mode.

NOTE: The instructions in this sample use Visual Studio 2022 and a Sitefinity renderer project named Renderer.

Folder structure

Under your Renderer project, you must create the following folders:

  • TagHelpers
  • Entities/Script
  • ViewComponents
  • Views/Shared/Components/Script

Implement the BodyTop and BodyBottom modes

In these two modes the Script widget registers the content of the text area with the IScriptRepository class and, then this script is read from the ScriptTagHelperTag class and the content is placed at the beginning or the end of the <body> tag.

  • In the context menu of folder TagHelpers, click Add » Class…
  • In Name, enter IScriptRepository.cs and click Add.
  • In the class, paste the following code and save your changes:

  • In the context menu of folder TagHelpers, click Add » Class…
  • In Name, enter ScriptTagHelper.cs and click Add.
  • In the class, paste the following code and save your changes:

Create a custom layout file

You must register the tag helper in the _Layout.cshtml file. You must then use this custom base layout for every page where the Script is placed

Perform the following:

  1. In the context menu of folder Views/Shared, click Add » Class…
  2. Select Code File.
  3. In Name, enter _Layout.cshtml and click Add.
  4. In the file, paste the following code and save your changes:

Create the widget

  • In the context menu of folder Entities/Script, click Add » Class…
  • In Name, enter ScriptEntity.cs and click Add.
  • In the class, paste the following code and save your changes:

  • In the context menu of folder ViewComponents, click Add » Class…
  • In Name, enter ScriptViewComponent.cs and click Add.
  • In the class, paste the following code and save your changes:

  • In the context menu of folder Views/Shared/Components/Script, click Add » Class…
  • Select Code File.
  • In Name, enter Default.cshtml and click Add.
  • In the class, paste the following code and save your changes:

Register the IScriptRepository class

You must register the IScriptRepository class in the Program.cs.

The Program.cs file should look in the following way:

Build your solution.

Result

When you open your Renderer application and open the New editor, you will see the Script widget in the widget selector. When you add the widget on your page and edit it, you can choose where to place the script and you can enter the script in the text area.

Script

Run the sample

This sample is available in Sitefinity’s GitHub repository. You can run and play with it.
To do this, perform the following:

  1. Go to Sitefinity’s GitHub repository Sitefinity ASP.NET Core samples.
  2. Expand Code and click Download ZIP.
  3. Extract the files on your computer.
  4. In the extracted folder, navigate to sitefinity-aspnetcore-mvc-samples-master/src/script-widget folder.
  5. Open the script-widget.sln in Visual Studio.
  6. Open the appsettings.json file.
  7. In section “Sitefinity”, change the “Url” property to the URL of your Sitefinity CMS site.
    If you have deployed Sitefinity CMS on the IIS, point to “https://localhost:<https_port>".
  8. In Visual Studio, in the context menu of script-widget project, click View in Browser.
  9. Log in to your Sitefinity CMS instance and place the widget on a page.

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?