Updating a SharePoint list with external web services data (Part 3)

In part 3 we will be building a simple web service request with C#.NET code.

List<int> myList = new List<int>();

The first thing we must do is initialize the web proxy client object. To begin using the web service proxy client object that was generated by adding a web reference to the GetEmployeePersonalInfo WSDL, we need to add a reference to its namespace using project namespaces. This can be done by adding the following line to the Program.cs source file.

using TestApp.com.employease.GetUserInfo;

Now that we have visibility into this namespace, we have access to the GetEmployeePersonalInfoService object which will act as a web service proxy client. To initialize this object, use the following line.

GetEmployeePersonalInfoService oService = new GetEmployeePersonalInfoService();

Add the above line to the Main method in the Program class (To simplify the code used in this tutorial, we will not be following object oriented design in the example code).

Authentication

In order to authenticate with the Employease Network, we will need to add a UsernameToken element to our SOAP header that conforms to the WS-I basic security profile. Luckily, we can take advantage of the functionality in the WSE 3.0 add-on to generate this for us.

In order to build a UsernameToken you must have created an ECXML user, a company identifier, and a shared secret password provided by ADP to its customers. The demo web service, however, will accept the following  credentials:

User: openapi.ecxml
Company Identifier: openapi
Password: VfmHcgz1QXMG2

Declare the following static member variables within the Program object.

private static string USERNAME = "openapi.ecxml@openapi";

private static string SHARED_SECRET = "VfmHcgz1QXMG2";

private static string COMPANY_IDENTIFIER = "openapi";

Import the namespace containing the UsernameToken with the following line:

using Microsoft.Web.Services3.Security.Tokens;

The WS-I basic security profile Username token will be formed using a combination of the ECXML username and the company identifier (e.g. “openapi.ecxml@openapi”, “soa.ecxml@test.com”, etc). The Password element will be a hashed combination of the shared secret, nonce, and a timestamp. The UsernameToken object constructor will handle all of this for us.

UsernameToken oUsernameToken = new UsernameToken(USERNAME, SHARED_SECRET);

The next steps are to create a SecurityHeaderType object and add the serialized UsernameToken to it’s list of headers.

System.Xml.XmlElement oSecurityHeaderXml = oUsernameToken.GetXml(new

System.Xml.XmlDocument());

SecurityHeaderType oSecurityHeader = new SecurityHeaderType();

oSecurityHeader.Any = new System.Xml.XmlElement[] { oSecurityHeaderXml };

The final step is to associate the security header with the web service proxy client object.

oService.Security = oSecurityHeader;

Building the request payload

Now it is time to populate the request payload. Because we are using a “Get” service, we will be sending a filter in our request payload. If we were using an “Add” or a “Change” web service to modify employee data, we would send an EmployeeKey element and a data element used to modify the existing data (e.g. user account info, corp group).

For this tutorial, we will be using the Companies filter. In .NET, this will be represented as an array of company identifiers (strings). For the demo, the company identifiers string has been defined as “openapi” in our static member variables declaration within Program.

GetEmployeePersonalInfoRequestFilter oFilter =

new GetEmployeePersonalInfoRequestFilter();

oFilter.Companies = new string[] { COMPANY_IDENTIFIER };

Next, we need to create a Request object and apply the filter that we created.

GetEmployeePersonalInfoRequest oRequest =

new GetEmployeePersonalInfoRequest();

oRequest.Filter = oFilter;

We now have enough information to make our web service call. For the GetEmployeePersonalInfo web service, the result will be an array of EmployeePersonalInfoType objects.

EmployeePersonalInfoType[] arPersonalInfo = oService.GetEmployeePersonalInfo(oRequest);

Parsing the response payload

Parsing the response is as simple as iterating through the resulting array (in this case an array of EmployeePersonalInfoTypes) and picking out the necessary data. The code below will expose all values for EmployeePersonalInfoType and display them in a console window.

// Loop through each employee in the response payload.

for (int i = 0; i < arPersonalInfo.Length; i++)

{

String stLastName = arPersonalInfo[i].PersonalInfo.PersonName.LastName;

String stFirstName = arPersonalInfo[i].PersonalInfo.PersonName.FirstName;

GenderType stGender = arPersonalInfo[i].PersonalInfo.Gender;

Array stDeliveryAddress = arPersonalInfo[i].PersonalInfo.PostalAddress.DeliveryAddress;

String stMunicipality = arPersonalInfo[i].PersonalInfo.PostalAddress.Municipality;

String stRegion = arPersonalInfo[i].PersonalInfo.PostalAddress.Region;

String stPostalCode = arPersonalInfo[i].PersonalInfo.PostalAddress.PostalCode;

String stEmail = arPersonalInfo[i].PersonalInfo.EmailAddress;

String stPhone = arPersonalInfo[i].PersonalInfo.Phone;

MaritalStatusType msMaritalStatus = arPersonalInfo[i].PersonalInfo.MaritalStatus;

String stWireless = arPersonalInfo[i].PersonalInfo.Wireless;

YesNoValueType ynTobaccoUser = arPersonalInfo[i].PersonalInfo.TobaccoUser;

YesNoValueType ynVietnamVet = arPersonalInfo[i].PersonalInfo.VietnamVeteran;

String stPager = arPersonalInfo[i].PersonalInfo.Pager;

YesNoValueType ynVeteran = arPersonalInfo[i].PersonalInfo.Veteran;

YesNoValueType ynMedicaid = arPersonalInfo[i].PersonalInfo.Medicaid;

String stFax = arPersonalInfo[i].PersonalInfo.Fax;

String stBirthDate = arPersonalInfo[i].PersonalInfo.BirthDate.ToShortDateString();

String stFullName = stLastName + ", " + stFirstName;

// Write the employees name and birthdate to the console.

System.Console.Out.WriteLine(stFullName + " - " + stBirthDate + " - " + stGender + " - " +  sMaritalStatus + " - " + stDeliveryAddress + " - " + stMunicipality + " - " + stRegion + " - " + stPostalCode + " - " + stEmail + " - " + stPhone + " - " + stFax  + " - " + stWireless + " - " + stPager + " - " + ynMedicaid + " - " + ynTobaccoUser + " - " + ynVeteran + " - " + ynVietnamVet);

}

// Display the console until a newline has been entered.

System.Console.In.ReadLine();

the screen will shows a space for null values between the hyphens.

Because the demo does not assign values to all the employee objects in the web service, the screen will shows a space for null values between the hyphens. When we start building our application to update the SharePoint list with web service data, we will only be using the fields that return an actual value. In our subsequent blog posts, we will be building a SharePoint list that will receive this data, and writing some code that will populate the list with employees in the ADP web service.

Below is the complete code for the TestAPP console application:

using System;

using System.Collections.Generic;

using System.Text;

using TestApp.com.employease.developer;

using Microsoft.Web.Services3.Security.Tokens;

namespace TestApp

{

class Program

{

private static string USERNAME = "openapi.ecxml@openapi";

private static string SHARED_SECRET = "VfmHcgz1QXMG2";

private static string COMPANY_IDENTIFIER = "openapi";

static void Main(string[] args)

{

GetEmployeePersonalInfoService oService = new GetEmployeePersonalInfoService();

UsernameToken oUsernameToken = new UsernameToken(USERNAME, SHARED_SECRET);

System.Xml.XmlElement oSecurityHeaderXml = oUsernameToken.GetXml(new System.Xml.XmlDocument());

SecurityHeaderType oSecurityHeader = new SecurityHeaderType();

oSecurityHeader.Any = new System.Xml.XmlElement[] { oSecurityHeaderXml };

oService.Security = oSecurityHeader;

GetEmployeePersonalInfoRequestFilter oFilter = new GetEmployeePersonalInfoRequestFilter();

oFilter.Companies = new string[] { COMPANY_IDENTIFIER };

GetEmployeePersonalInfoRequest oRequest = new GetEmployeePersonalInfoRequest();

oRequest.Filter = oFilter;

EmployeePersonalInfoType[] arPersonalInfo = Service.GetEmployeePersonalInfo(oRequest);

// Loop through each employee in the response payload.

for (int i = 0; i < arPersonalInfo.Length; i++)

{

String stLastName = arPersonalInfo[i].PersonalInfo.PersonName.LastName;

String stFirstName = arPersonalInfo[i].PersonalInfo.PersonName.FirstName;

GenderType stGender = arPersonalInfo[i].PersonalInfo.Gender;

Array stDeliveryAddress = arPersonalInfo[i].PersonalInfo.PostalAddress.DeliveryAddress;

String stMunicipality = arPersonalInfo[i].PersonalInfo.PostalAddress.Municipality;

String stRegion = arPersonalInfo[i].PersonalInfo.PostalAddress.Region;

String stPostalCode = arPersonalInfo[i].PersonalInfo.PostalAddress.PostalCode;

String stEmail = arPersonalInfo[i].PersonalInfo.EmailAddress;

String stPhone = arPersonalInfo[i].PersonalInfo.Phone;

MaritalStatusType msMaritalStatus = arPersonalInfo[i].PersonalInfo.MaritalStatus;

String stWireless = arPersonalInfo[i].PersonalInfo.Wireless;

YesNoValueType ynTobaccoUser = arPersonalInfo[i].PersonalInfo.TobaccoUser;

YesNoValueType ynVietnamVet = arPersonalInfo[i].PersonalInfo.VietnamVeteran;

String stPager = arPersonalInfo[i].PersonalInfo.Pager;

YesNoValueType ynVeteran = arPersonalInfo[i].PersonalInfo.Veteran;

YesNoValueType ynMedicaid = arPersonalInfo[i].PersonalInfo.Medicaid;

String stFax = arPersonalInfo[i].PersonalInfo.Fax;

String stBirthDate = arPersonalInfo[i].PersonalInfo.BirthDate.ToShortDateString();

String stFullName = stLastName + ", " + stFirstName;

// Write the employees name and birthdate to the console.

System.Console.Out.WriteLine(stFullName + " - " + stBirthDate + " - " + stGender + " - " + msMaritalStatus + " - " + stDeliveryAddress

+ " - " + stMunicipality + " - " + stRegion + " - " + stPostalCode + " - " + stEmail + " - " + stPhone + " - " + stFax

+ " - " + stWireless + " - " + stPager + " - " + ynMedicaid + " - " + ynTobaccoUser + " - " + ynVeteran + " - " + ynVietnamVet);

}

// Display the console until a newline has been entered.

System.Console.In.ReadLine();

}

}

}
January 19, 2013

Questions?

Email support@randgroup.com with any questions you have pertaining to this course.

New CPE Accredited Courses Now Available for Dynamics AX, GP, and NAVEARN CREDITS TODAY