Using APIs with C# and stuff

Have a question about Bullhorn's HTTP based APIs? You've found the right place.

API Update [posted on December 13, 2012]:

This version of the Bullhorn APIs should no longer be used.

All new work should be done using Bullhorn's REST or Web Services APIs. For more information please visit: http://developer.bullhorn.com/documentation

Bullhorn REST API provides a simpler and faster way for developers to build applications that interact with the Bullhorn platform and is used by Bullhorn Inc. to develop features of the Bullhorn ATS/CRM application.

Of the BullhornSTAFFING HTTP API interfaces listed below, our analysis shows that the following are most commonly used by developers:

Resume Parsing API
Published Job Data (XML)
The interface to these will continue to work as is because Bullhorn will replace the back end to use REST API. The URLs for these 2 specific APIs may change in the 2nd half of 2013; developers may require to update their applications to use the new URLs.

Moderators: StaffingSupport, s.emmons

cdubois
User
Posts: 12
Joined: Wed Dec 31, 1969 8:00 pm

Using APIs with C# and stuff

Postby cdubois » Sun Dec 09, 2007 4:13 am

I'm finishing up my project and I know I would have loved to see this code 2 months ago, even with all its known and unknown imperfections. I just wanted to post it in the hopes it saves some people some time...

I won't be around to answer too many questions about it. Maybe I'll comment it better in the future to help out the newbies. Use at your own risk. If anybody sees some glaring problems with it, please comment!

Code: Select all

using System;
using System.IO;
using System.Xml;
using System.Net;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SQLXMLBULKLOADLib;
using System.Data.OleDb;
/// <summary>
///
/// </summary>
public class BHWorker
{
    private string privateID = ConfigurationManager.AppSettings.Get("privateLabelID");
    private string key = ConfigurationManager.AppSettings.Get("encryptedAPIKey");

    public BHWorker()
    {
    }
    public String AddEmail(string xml)
    {
        string url = "http://www.bullhornstaffing.com/BullhornStaffing/API/AddEmailMessageAPI.cfm";
        string req = "privateLabelID=" + HttpUtility.UrlEncode(privateID) +
                     "&encryptedAPIKey=" + HttpUtility.UrlEncode(key) +
                     "&xmlData=" + HttpUtility.UrlEncode(xml);
        string xmlResponse = SendToBHAPI(url, req);
        return xmlResponse;
    }
    public string GetPublishedJobs()
    {
        string url = "http://www.bullhornstaffing.com/BullhornStaffing/API/PublicJobPostingsXML.cfm";
        string req = "privateLabelID=" + HttpUtility.UrlEncode(privateID) +
                     "&encryptedAPIKey=" + HttpUtility.UrlEncode(key);

        string xmlData = BHWorker.GetXMLFromAPI(url, req);
        return xmlData;
    }
    public String JobApplication(string userID, string jobIdsList)
    {
        string notificationsList = "3"; // Bullhorn people that should be notified.

        string url = "http://www.bullhornstaffing.com/BullhornStaffing/API/JobApplications.cfm";
        string req = "privateLabelID=" + HttpUtility.UrlEncode(privateID) +
                     "&encryptedAPIKey=" + HttpUtility.UrlEncode(key) +
                     "&userID=" + HttpUtility.UrlEncode(userID) +
                     "&jobPostingID=" + HttpUtility.UrlEncode(jobIdsList)+
                    "&notify=" + HttpUtility.UrlEncode(notificationsList);
        string xmlResponse = SendToBHAPI(url, req);
        return xmlResponse;
    }
    public String AddUpdateCertifications(string bhxml)
    {
        string url = "http://www.bullhornstaffing.com/BullhornStaffing/API/UpdateProfileAPI.cfm";
        string req = "privateLabelID=" + HttpUtility.UrlEncode(privateID) +
                     "&encryptedAPIKey=" + HttpUtility.UrlEncode(key) +
                     "&xmlData=" + HttpUtility.UrlEncode(bhxml);
        string xmlResponse = SendToBHAPI(url, req);
        return xmlResponse;
    }
    public String GetCertifications(string userID)
    {
        return GetProfileWithUserID(userID);
    }
    public String AddUpdateSpecialties(string bhxml)
    {
        string url = "http://www.bullhornstaffing.com/BullhornStaffing/API/UpdateProfileAPI.cfm";
        string req = "privateLabelID=" + HttpUtility.UrlEncode(privateID) +
                     "&encryptedAPIKey=" + HttpUtility.UrlEncode(key) +
                     "&xmlData=" + HttpUtility.UrlEncode(bhxml);
        string xmlResponse = SendToBHAPI(url, req);
        return xmlResponse;
    }
    public String GetSpecialties(string userID)
    {
        return GetProfileWithUserID(userID);
    }
    public String AddUpdateReference(string bhxml)
    {
        string url = "http://www.bullhornstaffing.com/BullhornStaffing/API/AddReferenceAPI.cfm";
        string req = "privateLabelID=" + HttpUtility.UrlEncode(privateID) +
                     "&encryptedAPIKey=" + HttpUtility.UrlEncode(key) +
                     "&xmlData=" + HttpUtility.UrlEncode(bhxml);
        string xmlResponse = SendToBHAPI(url, req);
        return xmlResponse;

    }
    public String GetReference(string userID)
    {
        string api = "http://www.bullhornstaffing.com/BullhornStaffing/API/GetReferenceAPI.cfm";
        string req = "privateLabelID=" + privateID +
                     "&encryptedAPIKey=" + key +
                     "&userID=" + userID;
        string xmlData = BHWorker.GetXMLFromAPI(api, req);
        return xmlData;
    }
    public String AddUpdateEducation(string bhxml)
    {
        string url = "http://www.bullhornstaffing.com/BullhornStaffing/API/AddEducationAPI.cfm";
        string req = "privateLabelID=" + HttpUtility.UrlEncode(privateID) +
                     "&encryptedAPIKey=" + HttpUtility.UrlEncode(key) +
                     "&xmlData=" + HttpUtility.UrlEncode(bhxml);
        string xmlResponse = SendToBHAPI(url, req);
        return xmlResponse;

    }
    public String GetEducation(string userID)
    {
        string api = "http://www.bullhornstaffing.com/BullhornStaffing/API/GetEducationAPI.cfm";
        string req = "privateLabelID=" + privateID +
                     "&encryptedAPIKey=" + key +
                     "&userID=" + userID;
        string xmlData = BHWorker.GetXMLFromAPI(api, req);
        return xmlData;
    }
    public String AddUpdateWorkHistory(string bhxml)
    {
        string url = "http://www.bullhornstaffing.com/BullhornStaffing/API/AddWorkHistoryAPI.cfm";
        string req = "privateLabelID=" + HttpUtility.UrlEncode(privateID) +
                     "&encryptedAPIKey=" + HttpUtility.UrlEncode(key) +
                     "&xmlData=" + HttpUtility.UrlEncode(bhxml);
        string xmlResponse = SendToBHAPI(url, req);
        return xmlResponse;
    }
    public String GetWorkHistory(string userID)
    {
        string api = "http://www.bullhornstaffing.com/BullhornStaffing/API/GetWorkHistoryAPI.cfm";
        string req = "privateLabelID=" + privateID +
                     "&encryptedAPIKey=" + key +
                     "&userID=" + userID;
        string xmlData = BHWorker.GetXMLFromAPI(api, req);
        return xmlData;
    }
    public String GetProfileWithUserID(string userID)
    {
        // Additional parameters
        string profileType = "Candidate";
        string outputType = "BHXML";

        string api = "http://www.bullhornstaffing.com/BullhornStaffing/API/GetProfileAPI.cfm";

        string req = "privateLabelID=" + HttpUtility.UrlEncode(privateID) +
                        "&encryptedAPIKey=" + HttpUtility.UrlEncode(key) +
                        "&userID=" + HttpUtility.UrlEncode(userID) +
                        "&profileType=" + HttpUtility.UrlEncode(profileType) +
                        "&outputType=" + HttpUtility.UrlEncode(outputType);
        string xmlString = GetXMLFromAPI(api, req);
        return xmlString;
    }
    public string UpdateProfile(string xmlData)
    {
        string url = "http://www.bullhornstaffing.com/BullhornStaffing/API/UpdateProfileAPI.cfm";

        string req = "privateLabelID=" + HttpUtility.UrlEncode(privateID) +
                        "&encryptedAPIKey=" + HttpUtility.UrlEncode(key) +
                        "&xmlData=" + HttpUtility.UrlEncode(xmlData);
        return SendToBHAPI(url, req);
    }
    public String GetProfile(string email)
    {
        string url = "http://www.bullhornstaffing.com/BullhornStaffing/API/GetProfileAPI.cfm";

        // Additional parameters
        string profileType = "Candidate";
        string outputType = "BHXML";

        // URL Encode parameters
        string req = "privateLabelID=" + privateID +
                        "&encryptedAPIKey=" + key +
                        "&email=" + email +
                        "&profileType=" + profileType +
                        "&outputType=" + outputType;

        string xmlString = GetXMLFromAPI(url, req);
        return xmlString;

    }
    public XmlDocument GetXMLDocFromAPI(string api, string req)
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(GetXMLFromAPI(api, req));
        return doc;
    }
    public static String GetXMLFromAPI(string api_url, string req)
    {
        HttpWebResponse resp = null;

        // Request data from BullHorn API
        HttpWebRequest request =
            (HttpWebRequest)WebRequest.Create(api_url);

        byte[] postDataBytes = Encoding.UTF8.GetBytes(req);

        // HTTP Request
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postDataBytes.Length;
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(postDataBytes, 0, postDataBytes.Length);
        requestStream.Close();

        // Get HTTP Response
        resp = (HttpWebResponse)request.GetResponse();
        StreamReader responseReader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
        string xmlString = responseReader.ReadToEnd();

        // Close Response stream
        resp.Close();

        // Return info
        return xmlString;
    }

    public string SendToBHAPI(string url, string postdata)
    {
        // Request data from BullHorn API
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        byte[] messageBytes = Encoding.UTF8.GetBytes(postdata);

        // HTTP Request
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = messageBytes.Length;
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(messageBytes, 0, messageBytes.Length);
        requestStream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode != HttpStatusCode.OK)
        {
            string message = String.Format("POST failed. Received HTTP {0}",
            response.StatusCode);
            throw new ApplicationException(message);
        }
        Stream s = response.GetResponseStream();
        StreamReader sr = new StreamReader(s);
        string output = sr.ReadToEnd();
        //return response.StatusCode + " " + url + " " + postdata + " " + output;
        try
        {
            XmlDocument doc = GetXmlDocFromString(output);
        }
        catch (Exception e)
        {
            return "false";
        }

        return "true";
    }
    /**
     *
     *
     */


    public static string GetXmlDocToString(XmlDocument doc)
    {
        MemoryStream memStream = new MemoryStream();
        doc.Save(memStream);
        memStream.Seek(0, 0);
        StreamReader reader = new StreamReader(memStream);
        string ret = reader.ReadToEnd();
        //string ret = Encoding.ASCII.GetString(memStream.GetBuffer(), 0, memStream.GetBuffer().Length);
        return ret;
    }
    /**
     * For when i forget about .loadXml()
     */
    public static XmlDocument GetXmlDocFromString(string s)
    {
        XmlDocument d = new XmlDocument();
        d.LoadXml(s);
        return d;
    }
    public static XmlDocument InsertJobCdataSnippits(XmlDocument doc)
    {
        XmlNodeList nodes = doc.SelectNodes("/root/job/PUBLICDESCRIPTION_STRING");
        foreach (XmlNode n in nodes)
        {
            n.InnerText = "<![CDATA[" + n.InnerText + "]]>";
        }
        return doc;
    }
    public static string GetAlteredPublishedJobs(string outputFile)
    {
        string xml = (new BHWorker()).GetPublishedJobs();
        XmlDocument d = new XmlDocument();
        d.LoadXml(xml);
        d = InsertJobCdataSnippits(d);
        string startPath = HttpContext.Current.Server.MapPath("~/");
        string dataPath = startPath + "App_Data\\" + outputFile;
        XmlTextWriter writer = new XmlTextWriter(dataPath, null);
        writer.Formatting = Formatting.Indented;
        d.Save(writer);
        return outputFile;
    }
}


cseibert
Bullhorn Employee
Posts: 393
Joined: Wed Dec 31, 1969 8:00 pm

Postby cseibert » Mon Dec 10, 2007 11:28 am

Thanks for sharing, this is good stuff.

Tomazo
User
Posts: 1
Joined: Fri Feb 17, 2012 4:03 am

Re: Using APIs with C# and stuff

Postby Tomazo » Fri Feb 17, 2012 4:06 am

I found the solution to my problem! Thanks for providing such a useful information!


Return to “Legacy API Suite Support”

Who is online

Users browsing this forum: No registered users and 1 guest