AnsweredAssumed Answered

Add PDF document (item) to organizational portal

Question asked by lbross_pdxedu on Nov 4, 2019

I'm trying to upload a PDF document to our organization's portal from an ArcGIS Pro add-in. I have been unable to find this functionality in the current API so am trying to use the REST API. When I run it, Pro churns away so I think it is uploading but I always receive a 502 Bad Gateway response.

 

Here is my current code:

var url = $"https://www.arcgis.com/sharing/rest/portals/self?f=json&token=" + strToken;
var response = new EsriHttpClient().Get(url);
var json = await response.Content.ReadAsStringAsync();
dynamic portalSelf = JObject.Parse(json);

var uploadUrl = "http://" + Convert.ToString(portalSelf.urlKey) +
".maps.arcgis.com/sharing/rest/content/users/" + owner + "/addItem";

byte[] fileBytes = System.IO.File.ReadAllBytes("C:\\Docs\\animas_AOI_prms\\test\\title_page.pdf");

string strTitle = "Testing 1 2 3";
using (var formData = new MultipartFormDataContent())
{
// Add the HttpContent objects to the form data
// <input type="text" name="f" />
formData.Add(new StringContent("f"), "json");
formData.Add(new StringContent("token"), strToken);
formData.Add(new StringContent("async"), "True");
formData.Add(new StringContent("type"), "PDF");
formData.Add(new StringContent("title"), strTitle);
formData.Add(new StringContent("tags"), "eBagis");
formData.Add(new StringContent("description"), "upload from BAGIS");
formData.Add(new ByteArrayContent(fileBytes, 0, fileBytes.Count()), "PDF Test", "title_page.pdf");

// Invoke the request to the server
// equivalent to pressing the submit button on
// a form with attributes (action="{url}" method="post")
response = await new EsriHttpClient().PostAsync(url, formData);
json = await response.Content.ReadAsStringAsync();
}

I found this Python script and think I am doing the same thing in C#, any ideas?

Outcomes