Mini-Lab: Visual Studio Community 2015 and the C# Web Service Example
If you search for a C#/.NET ServiceNow example you have probably come up with the following:
Web Services C Sharp .NET End to End Tutorial
This is a great first step, and over the years I have implemented several solutions that used this as a starting point. It assumes you are already a C# developer so I thought I would share a couple of insights, and fill in a couple of steps for bringing this example into the new VS IDE, and, of course, rewriting it to do something different!
So we actually have two labs here. First we will be installing the Visual Studio 2015 Integrated Development Environment (VS 2015 IDE or VS IDE). Then we will be building a getRecords example.
Lab 1.1: Installing VS Community 2015
a. Download from here: link it's free!
b. Follow the installation instructions. It goes pretty quick. link
Lab 1.2: Creating a Get Incidents Example
1. Read through the ServiceNow article, but only do step 1.1. Again the link is here.
2. In the VS IDE navigate to File -> New -> Project. The new project form will be displayed.
3. Fill in the form with the following:
a. WPF Application - do this instead of Windows Forms. The reasons are legion, but the best two are: it is significantly less of a resource hog, and it is resolution independent (windows forms are not).
b. Name: CSharpServiceNowInterface. Or your favorite name for this application. Make something up! Show some initiative!
c. Click on the OK button to create your project.
4. Your new project should look like this:
5. Now navigate to Debug -> Start Debugging. This will build and run your application in debugging mode. A blank form should be displayed, and the debugger stuff should be running in the IDE behind it.
6. Close the "MainWindow" form to stop the debugging. We are now ready to do some development.
7. From the Toolbox tab on the left pull out a RichTextbox control and a button control onto your new form. You will find the RichTextbox control under the All WPF Controls tree.
8. Click on the button to get the button properties to show in the lower right window.
9. Change the following:
a. Name: btnSend
b. Content: Send Command. You may need to stretch the button out a bit to get it to display the text.
10. Click on the RichTextbox to get the properties to show.
11. Change the following:
a. Name: rtbResults
12. Click on the outermost edge of the form to get the properties to show.
13. Change the following:
a. Name: ServiceNow
b. Title: ServiceNow Web Service
14. Now right-click on the CSharpServiceNowInterface project to bring up the project context menu.
15. Navigate to Add -> Service Reference. This will bring up the Add Service Reference Form.
16. Fill in the form with the following:
a. Address: https://<>.service-now.com?incident.do?wsdl
b. Namespace: incidentTable
17. Click the Go button. A popup will ask you to fill in the userid and password. BEWARE: It will send these in cleartext to ServiceNow (really???? sigh). Don't give that user too much authority. Read should be sufficient. We will be adding security next.
18. After a proper connection has been made the Add Service Reference form should look like this:
19. Click the OK button. A new Service Reference will be added.
20. Okay, now we go back to the original article. In step 1.2.1 we are to add in the security section to the App.config file.
a. Double-click the App.config entry in the Solution Explorer. This will open the App.config file for editing.
b. Replace the the following XML to the line with the following code:
c. Add in a new section at the bottom after the tag. I used my uid/pw combo that I created in a previous article. BTW, this is a best practice! Never hard-code this stuff in your code!
NOTE: If you REALLY want to crank down on the security of this file I found the following article excellent reading:
Jon Galloway - Encrypting Passwords in a .NET app.config File
d. Save the file (ctl-s). The file should look something like this:
21. Click on the MainWindow.xaml tab to bring the form editor back up.
22. Double-click on the Send Command button. This will bring up the MainWindow code file. We will be doing something similar to step 1.2.3 from the article.
23. Next we will add the Configuration reference to our project (this is annoyingly NOT done for you automatically).
a. Right-click on the References section in Solution Explorer, and choose Add Reference from the context pop-up. This will display the Add Reference form.
b. Scroll down and find the System.Configuration entry. Click on this; a check box will be displayed. Make sure this is checked.
c. Click OK to save your work.
24. Now replace both the MainWindow() and btnSend_Click() methods with the following code:
public MainWindow()
{
InitializeComponent();
// clear out the rich text box
rtbResults.Document.Blocks.Clear();
// make the scrollbar to be visible if results larger than display
rtbResults.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
// kill the pesky extra new lines style
Style noSpaceStyle = new Style(typeof(Paragraph));
noSpaceStyle.Setters.Add(new Setter(Paragraph.MarginProperty, new Thickness(0)));
rtbResults.Resources.Add(typeof(Paragraph), noSpaceStyle);
}
private void btnSend_Click(object sender, RoutedEventArgs e)
{
// declare our soap object for transmission
incidentTable.ServiceNowSoapClient soapClient = new incidentTable.ServiceNowSoapClient();
// pull the cred info from the app.config
soapClient.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["userID"];
soapClient.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["password"];
// Initialize our query
incidentTable.getRecords getRecordsQuery = new incidentTable.getRecords();
// Initialize our response. And since we don't know how many will be returned put it into a List object
List responseList = new List();
// Go after Beth Anglin's active records
getRecordsQuery.assigned_to = "46d44a23a9fe19810012d100cca80666"; // beth anglin or pick your favorite
getRecordsQuery.state = "2"; // active
try
{
// Now fire off our query.
responseList = soapClient.getRecords(getRecordsQuery).OrderBy(o=>o.number).ToList();
// Loop through our response records and print them to the results window
foreach (incidentTable.getRecordsResponseGetRecordsResult response in responseList)
{
string number = "\n" + response.number + "\t" + response.severity + "\t" + response.short_description;
rtbResults.AppendText(number);
}
}
catch(Exception error)
{
// something bad happened!
rtbResults.AppendText(error.Message);
}
}
25. We are ready to test! Navigate to Debug -> Start Debugging. This will run our application and display the form.
26. Click on the Send Command button. The results should appear in the window on the form.
Ta da!
27. Now let's go back and try an encoded query. Comment out the two query lines, and replace them with the encoded query from our List View.
// Go after Beth Anglin's active records
//getRecordsQuery.assigned_to = "46d44a23a9fe19810012d100cca80666"; // beth anglin
//getRecordsQuery.state = "2"; // active
getRecordsQuery.__encoded_query = "active=trueassigned_to=46d44a23a9fe19810012d100cca80666state=2";
28. Now navigate to Debug -> Start Debugging, and run our application again. You should have the same results displayed after clicking the Send Command button.
29. Finally always clean up your unused Usings (another best practice)!
It should then look like this:
Of course you will need to re-test everything to make sure that last step didn't break anything.
30. Finally you may not realize this, but you have created an executable. It is not ready for prime-time, but it is there. If you ever want to release your code you will have to build a release version (this is a debugger version), and then package it all up in an installer. You can find your executable simply by doing the following:
a. Right-click on any tab to bring up the tab context menu. Select "Open Containing Folder". This will bring up an explorer window showing your files.
b. Double-click on the Bin folder and the underlying Debug folder.
c. The debug folder will contain the files necessary to run your application. The .exe file is your executable. The .exe.config file is your App.config file.
Conclusion
That's it! Between the original C# article on the wiki and this one you should have most of the tools you need to do some really serious development! BTW, I will likely write a couple of more articles showing how to implement other features of the WSDL.
Have fun, and have a Happy New Year!
Steven Bell
If you find this article helps you, don't forget to log in and "like" it!
Also, if you are not already, I would like to encourage you to become a member of our blog!
https://www.servicenow.com/community/developer-blog/mini-lab-visual-studio-community-2015-and-the-c-web-service/ba-p/2285038