Search code examples
c#appian

Attempting to access Appian documents using C# and web-api's


I am trying to write a C# program to integrate my Appian cases with an external electronic document records management system (EDRMS).

The program needs to download the documents that have been uploaded for each case in Appian and then ensure that they're uploaded to the EDRMS using web-services.

I have written an Appian Web-api that I can use to log in and retrieve a list of the documents for each case, so I know where in the EDRMS I need to put the documents.

The documents are accessible [using a web-browser] with a URL like this: https://myappiansite.appiancloud.com/suite/doc/123456

If I browse to this URL using my web-browser, it immediately downloads the file.

However, if I attempt to connect to this url programmatically from my C# program, what I get when I [attempt to] open the file-stream, is some HTML of the default landing page rather than the contents of the document.

The code I'm writing looks like this:

        HttpWebRequest MyRequest = (HttpWebRequest)WebRequest.Create("https://myappiansite.appiancloud.com/suite/doc/123456?signin=native");
        MyRequest.Timeout = 10000; // 10 seconds

        CredentialCache credentialCache = new CredentialCache();
        credentialCache.Add(new System.Uri("https://myappiansite.appiancloud.com/suite/doc/123456?signin=native"),
                            "Basic",
                            new NetworkCredential("myAppianUsername",
                                                  "myAppainPassword"));

        MyRequest.Credentials = credentialCache; 
        MyRequest.PreAuthenticate = true;

        // Get the web response
        HttpWebResponse MyResponse = (HttpWebResponse)MyRequest.GetResponse();

        if (HttpStatusCode.OK == MyResponse.StatusCode)
        {
            int chunkSize = 524288;
            using (FileStream fs = new FileStream("C:\temp\myNewFile.doc", FileMode.Create))
            {
                using (Stream MyResponseStream = MyResponse.GetResponseStream())
                {
                    BinaryReader br = new BinaryReader(MyResponseStream);
                    byte[] data;
                    Boolean lastChunk = false;

                    do
                    {
                        data = br.ReadBytes(chunkSize);
                        if (data == null || data.Length == 0 || data.Length < chunkSize)
                        {
                            lastChunk = true;
                        }
                        if (data != null)
                        {
                            fs.Write(data, 0, data.Length);
                        }
                   }
                   while (!lastChunk);
                   br.Close();
                }//using MyResponseStream
            } // Using fs
        }

But when this code has run, the document "C:\temp\myNewFile.doc" just contains HTML of our main landing page.

Any thoughts on why browsing to https://myappiansite.appiancloud.com/suite/doc/123456 would download the document, but the code above just gets the HTML of the landing page?

[Note that the user "myAppianUsername" is a non-saml user, whereas normally, one would login using an account connected to AD via SAML. If I remove the "?signin=native" from the URL, then the file that is downloaded contains the HTML for the SAML login]

thanks heaps,

David.


Solution

  • I figured out what the problem was - it turns out that there are security restrictions built into Appian that won't allow applications to use the url "https://myappiansite.appiancloud.com/suite/doc/123456" to download documents. I had to create a seperate web-api to do this. Fortunately, Appian provides a document download web-api template/wizard that generates the api for you.

    So, by using the document download web-api wizard to generate a web api called "DownloadMyFile", I was able to use the code unchanged, with a web-api of:

    https://myappiansite.appiancloud.com/suite/webapi/DownloadMyFile/12345

    The only minor tweaks I had to make to to it was to add extra document types to its whitelist.