C# example - UserService1.svc/GetEnabledUsers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace GetAllEnabledUsers_Csharp
{
class Program
{
static void Main(string[] args)
{
string CK = "SAML"; // Enter Company key
//string LN = "PS11"; // Enter Login Name
//string PWD = "Replicon123!"; // Enter Password
//string UN = CK + "\\" + LN; // UserName in Basic Authentication Format
Dictionary<string, string> errorMessages = new Dictionary<string, string>(); // Create a Dictionary to hold the Error Message Details
string endpoint = getendpointurl(CK); // Variable to hold the Endpoint e.g. NA3, NA4, NA5, SP1 etc
if (errorMessages.Any())
{
string errorDetails = String.Format("Exception: {0}\r\nDetails: {1}\r\nCorelationID: {2}\r\n",
errorMessages["Exception"],
errorMessages["Details"],
errorMessages["CorelationID"]);
Console.WriteLine(errorDetails); // Write the error details to the Console
Console.ReadKey();
return;
}
var urlGenAllEnabledUsers = endpoint + CK + "/services/UserService1.svc/GetEnabledUsers"; // Construct the Web Service URL to be used
string inputData = "{}"; // Define the Input JSON Object in String representation
var jObject_inputData = Newtonsoft.Json.Linq.JObject.Parse(inputData); // Parse the string to JSON Object
errorMessages = new Dictionary<string, string>();
var response_inputData = TryCallService(urlGenAllEnabledUsers, jObject_inputData, out errorMessages); // Make the HTTP Service Call to get the response
if (!errorMessages.Any()) // Check for any error during the HTTP Service Call
{
foreach (Newtonsoft.Json.Linq.JToken user in response_inputData["d"])
{
string userDetails = string.Format("displayText: {0}\r\nloginName: {1}\r\nslug: {2}\r\nuri: {3}\r\n",
user["displayText"],
user["loginName"],
user["slug"],
user["uri"]);
Console.WriteLine(userDetails); // Write the User Details to the Console
}
Console.ReadKey();
}
else
{
string errorDetails = String.Format("Exception: {0}\r\nDetails: {1}\r\nCorelationID: {2}\r\n",
errorMessages["Exception"],
errorMessages["Details"],
errorMessages["CorelationID"]);
Console.WriteLine(errorDetails); // Write the error details to the Console
Console.ReadKey();
}
}
// Function to get Endpoint
private static string getendpointurl(string companykey)
{
string url_DiscoveryService = "https://global.replicon.com/DiscoveryService1.svc/GetTenantEndpointDetails"; // URL to Get TenantEndpointDetails
string data_DiscoveyService = "{\"tenant\":{\"uri\":null,\"slug\":null,\"companyKey\":null}}"; // String representation for JSON Object for GetTenantEndpointDetails Service Call
var jObject_DiscoveryService = Newtonsoft.Json.Linq.JObject.Parse(data_DiscoveyService); // Parse the string to JSON Object
jObject_DiscoveryService["tenant"]["companyKey"] = companykey; // Update the Company key
Dictionary<string, string> errorMessages = new Dictionary<string, string>(); // Create a Dictionary to hold the Error Message Details
var response_DiscoveryService = TryCallService(url_DiscoveryService, jObject_DiscoveryService, out errorMessages); // Make the Service Call
if (!errorMessages.Any())
return response_DiscoveryService["d"]["applicationRootUrl"].ToString(); // Return the Endpoint
else
return errorMessages["Details"]; // Return details of the error received
}
// Function to make Service Call
private static Newtonsoft.Json.Linq.JObject TryCallService(string URL, Newtonsoft.Json.Linq.JObject input, out Dictionary<string, string> errorMessages)
{
try
{
Newtonsoft.Json.Linq.JObject jObject;
var jsondata = Newtonsoft.Json.JsonConvert.SerializeObject(input);
byte[] byteArray = Encoding.UTF8.GetBytes(jsondata);
// Make the HTTP request
HttpWebRequest servicecall = (HttpWebRequest)WebRequest.Create(URL);
//servicecall.Credentials = new NetworkCredential(UN, pwd);
servicecall.PreAuthenticate = true;
servicecall.Method = "POST";
servicecall.ContentType = "application/json; charset=UTF-8";
servicecall.Headers.Add("Authorization", "Bearer lXluv1eGYEGzMwjpsRL5mgEANTNkYTAyZWM5OWU2NDg2MWFmZWVjZDM3MzAxZjA1NTA");
servicecall.Headers.Add("X-Replicon-Application", "TestCompanykey_ProjectImport_1.0");
servicecall.ContentLength = byteArray.Length;
servicecall.ServicePoint.Expect100Continue = false;
using (var requestStream = servicecall.GetRequestStream())
{
requestStream.Write(byteArray, 0, byteArray.Length);
requestStream.Close();
}
using (var response = (HttpWebResponse)servicecall.GetResponse())
using (var receivedetream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(receivedetream))
{
jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine()); // Convert the response to JSON Object
}
errorMessages = new Dictionary<string, string>();
return jObject;
}
catch (Exception e)
{
if (e.GetType() == typeof(WebException)) // Check if the exception is a Web Exception(returned by the HTTP request)
{
Dictionary<string, string> error = new Dictionary<string, string>(); // Create a Dictionary to hold the Error Details
WebException eWebException = (WebException)e;
Newtonsoft.Json.Linq.JObject errorJObject = GetResponseErrorObject((HttpWebResponse)eWebException.Response, out error); // Get the formatted Error Details
if (!error.Any())
{
errorMessages = new Dictionary<string, string>()
{
{"Exception", e.Message},
{"Details", errorJObject["error"]["reason"].ToString()},
{"CorelationID", eWebException.Response.Headers["X-Execution-Correlation-Id"]}
};
}
else
{
errorMessages = error;
}
return errorJObject;
}
else
{
errorMessages = new Dictionary<string, string>()
{
{"Exception", e.Message},
{"Details", string.Empty},
{"CorelationID", string.Empty}
};
return null;
}
}
}
// Function to get Error Details
private static Newtonsoft.Json.Linq.JObject GetResponseErrorObject(HttpWebResponse errorResponse, out Dictionary<string, string> error)
{
try
{
Newtonsoft.Json.Linq.JObject jObject;
using (Stream receivedstream = errorResponse.GetResponseStream())
using (StreamReader reader = new System.IO.StreamReader(receivedstream))
{
jObject = Newtonsoft.Json.Linq.JObject.Parse(reader.ReadLine());
}
error = new Dictionary<string, string>();
return jObject;
}
catch (Exception e)
{
error = new Dictionary<string, string>()
{
{"Exception", e.Message},
{"Details", e.Message},
{"CorelationID", string.Empty}
};
return null;
}
}
}
}
Related links
Code examples
Introduction to the Replicon API
Using the API
Getting started with Replicon's API
Viewing the available API operations