Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Xml;
using System.Net;
using System.IO;
using System.Text;
namespace AconexSampleSheet
{
class Program
{
static void Main(string[] args)
{
// Sample request values.
// API Service: List projects for user
//list all the projects for this user
//string uri = "https://uk1.aconex.co.uk/api/projects";
//list all the documents in this project for that user, and return the tracking ID for each
//string uri = "https://uk1.aconex.co.uk/api/projects/268441385/register?return_fields=trackingid";
//list all the versions of the document of tracking ID 271341877549073739 and return the document ID, tracking ID, version number for each document
//string uri = "https://uk1.aconex.co.uk/api/projects/268441385/register?search_query=trackingid:1348828088418370096&return_fields=registered,trackingid,versionnumber&show_document_history=true";
//search all documents in P027C
//string uri = "https://uk1.aconex.co.uk/api/projects/268441385/register?search_query=attribute3:P027c*&return_fields=author,received,registered,filename,docno,title&search_type=Full&show_document_history=true";
//search all mail in inbox of P027C
//string uri = "https://uk1.aconex.co.uk/api/projects/268441385/mail?mail_box=Inbox&search_query=thirdattribute:P027c*&search_type=Full";
//string uri = "https://uk1.aconex.co.uk/api/projects/268441385/mail?mail_box=Inbox&search_query=thirdattribute:P027c*&search_type=Full";
//search all general correspondence items in inbox
//note the space each side of the boolean operator " AND "
//string uri = "https://uk1.aconex.co.uk/api/projects/268441385/mail?mail_box=Inbox&search_query=thirdattribute:P027c* AND corrtypeid:268435877&search_type=Full";
//search all sent transmittals in sent mail
//string uri = "https://uk1.aconex.co.uk/api/projects/268441385/mail?mail_box=Sentbox&search_query=thirdattribute:P027c* AND corrtypename:Transmittal&return_fields=docno,sentdate,subject&&search_type=Full";
//search all sent mail general correspondence
//string uri = "https://uk1.aconex.co.uk/api/projects/268441385/mail?mail_box=Sentbox&search_query=thirdattribute:P027c* AND corrtypeid:268435877&return_fields=docno,subject&search_type=Full";
//string uri = "https://uk1.aconex.co.uk/api/projects/268441385/register?search_query=attribute3:P027c* AND (docno:tbs* OR docno:tsb*)&return_fields=author,received,registered,filename,docno,title&search_type=Full&show_document_history=true";
string uri = args[0];
//Console.WriteLine(uri);
string xml_location = args[1] + args[2];
string userName = "abcd";
string password = "abcd";
string accessToken = "";
string method = "GET";
string response = "";
bool error = false;
bool aconexError = false;
string errorMessage = "";
// XML response from Aconex stored in string parameter “response”
// If aconexError flag is raised, response will contain Aconex error message
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
string credentials = "";
if (accessToken != "")
credentials = "Bearer " + accessToken;
else
credentials = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(userName + ":" + password));
request.Headers.Set(HttpRequestHeader.Authorization, credentials);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Headers.Set("X-Application-Key", "abcd");
request.Method = method;
try
{
using (StreamReader str = new StreamReader(
request.GetResponse().GetResponseStream()))
{
// Contains the Aconex API xml response
response = str.ReadToEnd();
}
}
catch (WebException e)
{
using (WebResponse webResponse = e.Response)
{
try
{
using (Stream data =
webResponse.GetResponseStream())
{
// Contains the Aconex API error xml respons
response = new
StreamReader(data).ReadToEnd();
aconexError = true;
}
}
catch (Exception ex)
{
error = true;
errorMessage =
"Please check your Internet connection.\n"
+ e.Message + "\n" + ex.Message;
}
}
}
// Write result
if (!error)
{
System.IO.File.WriteAllText(xml_location, response);
}
if (error)
{
Console.WriteLine(errorMessage);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.