Integrate ServiceNow with Microsoft Orchestrator

ServiceNow(SN) can be integrated with Microsoft Orchestrator using out of the box REST Integration Pack however, it is a very complex process when it comes to read and write data from/to SN. Some paid management packs are also available in the market however if you are planning to build your own to save cost or better control, following information can give you a kick start.

There are two components to make this connectivity, first being authentication and then the queries you want to run. But before that there are some prerequisites that you may need to perform before you can successfully connect to SN:

  1. Make sure the URL for your SN instance (better to use demo instances in the beginning) should be accessible without any proxy
  2. You should have valid admin credentials to the instance
  3. The SN should be set to respond to REST queries and if you are using any ID other than the admin, then that ID should be allowed to fetch data via REST
  4. You should have Visual Studio as well as API for SN and REST
  5. You should have the basic understanding on how to package a management pack and publish it to the Orchestrator

Leave your queries in the comments if you need any further information on these prerequisites. The code given below is just to give you an idea to kick start and by no mean is the best way to code.

Connectivity:

In order to connect to SN and authenticate, use the following code. Once you create the MP using the code below, in Orchestrator, you should see a SN connectivity menu. Type the required details and your settings are now saved and ready to be used in the MP.

using System;
using System.Net;
using System.Net.Mail;
using System.Xml;
using System.Xml.XPath;
namespace Microsoft.SystemCenter.Orchestrator.Integration.Examples.ServiceNow
{
 [ActivityData("ServiceNow Configuration")]
 public class ServiceNowSettings
 {
 private String url = string.Empty;
 private String userName = string.Empty;
 private String password = string.Empty;
 //private string[] test = new string[2000];
 [ActivityInput, ActivityOutput]
 public String URL
 {
 get { return url; }
 set { url = value; }
 }
[ActivityInput, ActivityOutput]
 public String UserName
 {
 get { return userName; }
 set { userName = value; }
 }
[ActivityInput(PasswordProtected = true), ActivityOutput]
 public String Password
 {
 get { return password; }
 set { password = value; }
 }
 [ActivityMethod]
 public string[] Main()
 {
 string UrlRequest = URL+"/api/now/table/sys_db_object?sysparm_limit=10000&sysparm_order_direction=name";
HttpWebRequest request = WebRequest.Create(UrlRequest) as HttpWebRequest;
 request.PreAuthenticate = true;
 request.Credentials = new NetworkCredential(UserName, Password);
 request.Method = "GET";
 request.ContentType = "application/xml";
 request.Accept = "application/xml";
 HttpWebResponse response = request.GetResponse() as HttpWebResponse;
XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load(response.GetResponseStream());
 XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
 nsmgr.AddNamespace("rest", "http://schemas.microsoft.com/search/local/ws/rest/");
for each location
 XmlNodeList locationElements = xmlDoc.SelectNodes("//result", nsmgr);
int i = 0;
 foreach (XmlNode location in locationElements)
 {
i++;
 }
 string[] arr1 = new string[i];
 int j = 0;
 foreach (XmlNode location in locationElements)
 {
 arr1[j] = location.SelectSingleNode(".//name", nsmgr).InnerText;
 j++;
 }
 Array.Sort(arr1);
 return arr1;} 
 [ActivityOutput]
 public string[] Test
 {
 get { return Main(); }
}}}

Fetching the Data:

For fetching the data, in the same VS project, create another class and use the following code. Once you recreate the MP, you should now see the Fetch Data option under your MP in Orchestrator, drag it to the runbook and use the setting saved, build your query, save the query result to any log file and run the runbook. The results should be visible in the log file.

using System;
using System.Net;
using System.Net.Mail;
using Microsoft.SystemCenter.Orchestrator.Integration;
using System.Xml.XPath;
using System.Xml;
using System.Collections.Generic;
namespace Microsoft.SystemCenter.Orchestrator.Integration.Examples.ServiceNow
{
[Activity("Select Table")]
 public class SelectTable : IActivity
 {
 private ServiceNowSettings settings;
 public string t1;
[ActivityConfiguration]//(Usage = ConfigurationUsage.ExecuteOnly)]
 public ServiceNowSettings Settings
 {
 set { settings = value; }
 get { return settings; }
 }
 public void Design(IActivityDesigner designer)
 {
 designer.AddInput("Select Table").WithListBrowser(settings.Test);
 designer.AddFilter("active");
 designer.AddFilter("assigned_to");
 designer.AddOutput("Selected Table");
}
public void Execute(IActivityRequest request1, IActivityResponse response1)
 {
 string seltable = request1.Inputs["Select Table"].AsString();
 response1.Publish("Selected Table", seltable);
}
}
}

Fetching Incidents:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(settings.URL + "/incident.do?WSDL");
 webRequest.PreAuthenticate = true;
 webRequest.Credentials = new NetworkCredential(settings.UserName, settings.Password);
 //webRequest.Timeout = // details elided
 HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
 XPathDocument xpathDocument = new XPathDocument(webResponse.GetResponseStream());
 XPathNavigator xpathNavigator = xpathDocument.CreateNavigator();
XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(new NameTable());
 xmlNamespaceManager.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
 int i = 0;
 //foreach (XPathNavigator node in xpathNavigator.Select("//xsd:element/@getKeys", xmlNamespaceManager))
 foreach (XPathNavigator node in xpathNavigator.Select("//xsd:schema/xsd:element[5]/xsd:complexType/xsd:sequence/xsd:element/@name", xmlNamespaceManager))
 {
 i++;
 }
 //Console.ReadLine();//Console.WriteLine("Show all formatted addresses: Option 1");
string[] arr1 = new string[i];
 int j = 0;
 foreach (XPathNavigator node in xpathNavigator.Select("//xsd:schema/xsd:element[5]/xsd:complexType/xsd:sequence/xsd:element/@name", xmlNamespaceManager))
{
 arr1[j] = node.Value;
 j++;
 }
 //Array.Sort(arr1);
 return arr1;}

You can create multiple options in the MP to read, delete, update data in SN. Use a SQL DB to store and manipulate data before uploading to SN again. Your other applications or automation systems can read from this SQL DB and update it once the tasks are completed and upload the results in SN.

Advertisements

Posted on December 9, 2015, in Orchestrator, ServiceNow and tagged , , , . Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: