Hi all,
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: myappiansite.appiancloud.com/.../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("">myappiansite.appiancloud.com/.../123456 MyRequest.Timeout = 10000; // 10 seconds
CredentialCache credentialCache = new CredentialCache(); credentialCache.Add(new System.Uri(theWebUrl), "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 myappiansite.appiancloud.com/.../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.
Discussion posts and replies are publicly visible
I believe there are security restrictions that prevent downloading documents in this way. Instead of trying to use the URL directly, I'd suggest creating a Web API that can send the document. When you first create a Web API, it gives a template for "Document Download". If you select that, it returns an API that can accept a document ID and return the corresponding document.
Then, you should be able to call this API to return any document you need.
Thanks! That's resolved the problem. Generated a Document download Web-Api using the wizard, added the document types I require to the code and used that in place of the "/suite/doc/123456" url, and it works perfectly. Cheers! :-)