venkatiyer1
Giga Guru

In Service Portal, we don't get the Copy permalink feature in the KB article that we have in a KB article in the normal desktop view. There are a lot of questions around the same in the community. I wanted to share what we have implemented it using two widgets and wanted to share that with other groups members. This could be further modified to make it a directive etc. but I am going with minimal version. 

Steps to add a Copy Permalink Feature:

1. Go to Service Portal -> Widgets -> New

Name: Copy Permalink

Public Widget: True

Then use related link at the bottom of the form Open in Widget Editor

For the HTML Template, add the following code

<div class="panel panel-primary b">
  <div class="panel-heading">
    <h2 class="h4 panel-title" ng-bind="::options.title"></h2>
  </div>
  <div class="panel-body">
     <span ng-click="c.copyPermalink()">{{c.data.linkName}}</span>
  </div>
</div>

For the client side, add the following code

function($scope, $window, $rootScope,spUtil) {
  /* widget controller */
        var c = this;
        var serviceportalURL = c.data.instanceURL + "sp"; 
        // where sp is the service portal URL suffix
	c.data.URL = serviceportalURL + "?id=kb_article&sysparm_article=" + 
	c.data.article.number;
	
	c.copyPermalink = function() {
		var body = angular.element($window.document.body);
		var inp = angular.element('<input/>');
		inp.val(c.data.URL);
		body.append(inp);
		inp.select();
		try {
			var successful = $window.document.execCommand('copy');
			if (!successful) throw successful;
		} catch (err) {
			$window.prompt("Copy to clipboard: Ctrl+C, Enter", c.data.URL);
		}
    inp.remove();
		spUtil.addInfoMessage("Copied to clipboard");
	}
}

For the Server script, add the following code

(function() {
  /* populate the 'data' object */
  /* e.g., data.table = $sp.getValue('table'); */
	options.title = options.title || gs.getMessage("Permalink");
	data.linkName = gs.getMessage("Copy Permalink");
	data.instanceURL = gs.getProperty('glide.servlet.uri');
	data.article = {};
	var page_sysID = $sp.getParameter("sys_id");
	var article_number = $sp.getParameter("sysparm_article");
	var kbNumber;
	if(article_number) {
		  data.article.number = article_number;
		} else if(page_sysID)	{
			var kb = new GlideRecordSecure('kb_knowledge');
			if(kb.get(page_sysID)) {
				data.article.number = kb.getDisplayValue("number");
			}
		}
})();

Save the changes and you can preview it look like

find_real_file.png

2. Go to Service Portal -> Pages -> Id equal to kb_article i.e. Knowledge Base page

Use the related link at the bottom open the page and add the widget that you created i.e. Copy Permalink the right hand side or wherever you want to add it. 

The above widget basically use the URL and extracts the parameter sys_id of the URL to get the number or if we have the URL parameter sysparm_article it would use the number directly from there to create the permalink.

This solves the problem partly but there is another part to it. We need to have a page that covers the following scenarios below:

a. If there is a link like ?id=kb_article&sysparm_article=KB00000XX&sys_id=16f01f3bdb1c17843c4b3511ef9619cb  or  ?id=kb_article&sysparm_article=KB00000XX in the URL, the page in the portal should be the latest version based on the KB number.

b. if there is a link like ?id=kb_article&sys_id=16f01f3bdb1c17843c4b3511ef9619cb in the URL, the page in the portal should show that version of article. 

Currently Kb_article page supports rendering when a sys id is provided. So here our next step begins:

3.  Go to Service Portal -> Widgets -> KB Article Page

Clone this Widget and name it KB Article Page Custom or whatever name works for you. 

Use the related link at the bottom to the open the widget in Widget Editor.

Go to Server script and replace the following line

articleGR.get($sp.getParameter('sys_id'));

with

if($sp.getParameter('sysparm_article')) {
	articleGR.addQuery('number', $sp.getParameter('sysparm_article'));
	articleGR.addQuery('workflow_state', 'published');
	articleGR.orderByDesc("version");
	articleGR.query();
	articleGR.next();
} else if ($sp.getParameter('sys_id')) {
	articleGR.get($sp.getParameter('sys_id'));
}

So the final version will look like this (screenshot of the change alone)

find_real_file.png

The code continues further and i have just taken the above screenshot of the area of the code that got changes in the custom cloned widget. 

Save the widget changes.

4.  Go to Service Portal -> Pages -> Id equal to kb_article i.e. Knowledge Base page

Use the related link at the bottom open the page and replace the main widget in the center column i.e. KB Article Page Custom with the one you created above KB Article Page Custom. 

Save all your changes. 

Now after you have made the above changes whenever you have sysparm_article in the URL the article shown will be the latest published version. The copy permalink on the right would copy the URL created with sysparm_article into the users clipboard. 

 

The above code could be made more modular and also could be included as dependency. 

The final version would look something like below:

find_real_file.png

 

 

On clicking Copy Permalink the URL would get copied to clipboard.

 

Comments
Aleksas Kucins1
Giga Expert

Copy permalink button as well comes with OOTB 'Knowledge Article Content' portal widget if you activate free 'Knowledge Management - Service Portal' plugin.

venkatiyer1
Giga Guru

Thanks Aleksas, was not aware of this plugin as i searched around community and tried googling on Copy Permalink too in portal. This is helpful.  The plugin ensures that it appears as a link at the bottom of the article and it updates the articles within the widget to point to a separate page itself. Well the above code is an alternative for somebody who doesn't want to activate the plugin. We had activated Service Portal - Knowledge base plugin. I didn't know i had to activate Knowledge Base - Service Portal plugin to get this feature. It would have been nice if it was documented or somebody commented the same on the questions asked within the community. 

find_real_file.png

Yang Lu
ServiceNow Employee
ServiceNow Employee

Couple of updates.  In NY, there is a portal page instance option to show the latest:

find_real_file.png

This should solve all future challenges regardless of which portal you use.

However, pre-NY, this will be an issue.  The parameters that need to be passed for the Article ID and not the SysID do not exist at all as mentioned in the above article.  So you'll get an "Article not found" if you pass the article ID to SP and vice versa if you use the 'Knowledge Article Content' of the widget and try to pass sysID.

There are other workarounds, but if you truly want to stay OOB, you'll have to wait till NY.

Rupa2
Tera Contributor

can you please guide on how did you enable this option please ?

Matt Warwick
Tera Contributor

Accessing articles via sysparm_article is great for us, as we've created a URL shortener which redirects users to the KB.

However, we noticed that attachments weren't being displayed if the article was viewed this way, because the sys_id isn't' fetched.

I fixed this by replacing this line in the server script:

t.sys_id = $sp.getParameter('sys_id');

with:

if($sp.getParameter('sysparm_article')) {
   t.sys_id = articleGR.getValue('sys_id');
} else if ($sp.getParameter('sys_id')) {
   t.sys_id = $sp.getParameter('sys_id');
}

 

venkatiyer1
Giga Guru

Thanks Matt. A great value add.

 

Jack9
Giga Contributor

Hi venkatiyer,

When creating the widget, the body where the "Copy Permalink" link should be is empty.

I am on Madrid Patch 9. Do you know what the issue could be?

Any help is appreciated!

 

Jyoti Mehta
Tera Contributor

Hi Venkatiyer,

I have followed all the steps which you have mentioned for copy permalink but when i am trying to copy URL and open it in new window then it will redirect to OOTB service portal not our customized portal. can you please suggest what needs to be done ?

Thanks in advance!!

Regards,

Jyoti 

Jyoti Mehta
Tera Contributor

Hi Matt,

Thanks for above code, we were also facing the same issue and it is resolved after updating above line of code. 

I need your help for one more issue related to same page. In our KB Article page we have additional comments section and Mark as helpful (feedback section) which is not displaying when we open kb article link by using copy permalink.

Regards,

Jyoti 

Matt Warwick
Tera Contributor

Hi Jyoti,

No problem, I'm glad it helped someone!

The feedback section works fine for us using sysparm_article, so not sure what could be wrong.

See https://universityofbirmingham.service-now.com/itportal?id=uob_kb_article&sysparm_article=KB10074 for example, the extra box above it is just a small extra widget we've put in as we were getting lots of unhelpful feedback from users.

Jesse20
Tera Contributor

We did something similar for our production instance a while back, however, if you are using the Knowledge Portal and/or using the portal page kb_article_view for your knowledge articles, then you should see an OOB 'Copy Permalink' at the very bottom right of the article. 

You don't necessarily have to be using the Knowledge Portal, because the kb_article_view page can/will render in whatever service portal you want displayed (depending on security rules).  

Version history
Last update:
‎03-26-2019 02:16 PM
Updated by: