Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Single page app on Service Portal Service Now - UI-Router and Widgets

GovarthananV
Tera Contributor

Create a Hello world project in ESC service portal using Angular UI router and service portal widgets

Step 1

First let's download the library (https://github.com/angular-ui/ui-router) and create a UI Script with its content . The minified( https://unpkg.com/@uirouter/angularjs@1.1.1/release/angular-ui-router.min.js ) version is recommended.

System UI > UI Scripts > New 
UI Router AngularJs

 

GovarthananV_1-1748424912513.png


Step 2
Create a widgets 
Service Portal > Widgets > New
SPA Home


SPA Details


Add your HTML, css and scripts in widgets

Step 3
System UI > UI Scripts > New 
SPA UI Script

(function (angular) {
  var app = angular.module("spa_ng_app", ["ui.router"]);
  app.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider
      .state("spa_home", {
        template: '<sp-widget widget="data.spa_home"></sp-widget>',
      })
      .state("spa_details", {
        template: '<sp-widget widget="data.spa_details"></sp-widget>',
      });
  });
})(angular);

GovarthananV_2-1748426137691.png


Step 4
Create new widgets 
Service Portal > Widgets > New
SPA UI View

 HTML

<a ui-sref="spa_home" class="btn btn-primary">Home</a>
<a ui-sref="spa_details" class="btn btn-primary">Details</a>
<ui-view></ui-view>

Server script

(function() {
    data.spa_home = $sp.getWidget('spa_home');
    data.spa_details = $sp.getWidget('spa_details');
})();

 

Client controller
 
api.controller = function($scope, $state, $location) {
    $state.go('spa_home');
};

GovarthananV_3-1748426541097.png


Now add new Widget Dependency

SPA UI View > Widget Dependency > New

SPA UI View Dependency

GovarthananV_4-1748426726245.png

GovarthananV_6-1748427310969.png

 


SPA UI View Dependency > JS Include > New
Once has been added the UI router and SPA UI script in the JS Include 

GovarthananV_7-1748427343678.png


Update the order in SPA UI View Dependency
UI Router to 100
UI Script to 200

GovarthananV_5-1748427124193.png


Step 5

Create new Page 
Service Portal > Pages > New

GovarthananV_8-1748427846578.png


Once create a new page and reopen the page add the widget 


Open in Designer add 12 column then add the SPA UI View widget there and close 
 

GovarthananV_9-1748427959303.png


SPA UI > save or update the page 

GovarthananV_10-1748428144049.png


Step 6
Open the SPA page in 
https://dev******.service-now.com/esc?id=spa_ui
or
https://dev******.service-now.com/$spd.do#/sp/preview/desktop/spa_ui

Click the button and switch the widgets

GovarthananV_11-1748428449211.png

GovarthananV_12-1748428473174.png

 

 

I referred to the post below and decided to use widgets instead of a template or page, as those options take longer to load and don't offer the same flexibility. Using widgets allows for quicker performance and more control over customisation.
 https://www.servicenow.com/community/developer-blog/single-page-app-on-service-portal-part-1-ui-rout...

I believed it would contribute to building a more flexible SPA application in ServiceNow.

Thanks 🙂


0 REPLIES 0