Uploading a File to the Library

In my previous post, we looked at how to attach a file to an equipment programmatically. In this post, we will look at how to upload a file to the library with tags. The code is actually simpler with uploading to the library as we don’t require a secondary level parameter collections about the detail of the uploading file.

Publish – Request

This is a call to upload a document to the project’s library.

URL: https://bim360field.autodesk.com/api/library/publish

Supported Request Methods: Post

Documentation: https://bim360field.autodesk.com/apidoc/index.html#library_api_method_1

Required Parameters:

  • ticket : string – the ticket obtained from the login call
  • project_id : string – the ID of the project to upload a file to
  • filename: string – the name of the file to be uploaded
  • Filedata: binary – the content of the document to upload

Optional Parameters:

  • directory: string – the folder name where a document to be uploaded.
  • document_id: string – if specified, a new revision of document_id will be created.
  • tags: string – a set of comma separated tags
  • caption: string – a caption for the document

Publish – Response

Upon a successful request call, the status code will be “Created”. Below shows an example of how a successful response looks like:

{
"caption":null,
"composite_image_names":"Test5_ee04303e_a6f0_4372_977f_0205fce9dba3_composite.jpg",
"content_type":"application/pdf",
"created_at":"2015-08-12 01:08:31 -0500",
"document_id":"8f0a63ba-9809-46d8-b85e-4bf28b0c5b0e",
"fcreate_date":"2015-08-12 01:08:31 -0500",
"name":"Test5.pdf",
"fmod_date":"2015-08-12 01:08:31 -0500",
"height":null,
"id":"8f0a63ba-9809-46d8-b85e-4bf28b0c5b0e",
"locations":[],
"num_pages":null,
"object_type":"Document",
"parent_id":null,
"pending":true,
"project_id":"d2338132-d598-429f-ae14-1859e7f77b00",
"revision_count":1,
"revision_position":0,
"path":"",
"filename":"Test5.pdf",
"size":1,
"tags":"MyTag,YourTag",
"thumb_image_name":"Test5_ee04303e_a6f0_4372_977f_0205fce9dba3_thumb.png",
"updated_at":"2015-08-12 01:08:31 -0500",
"vela_viewer_image_names":"Test5_ee04303e_a6f0_4372_977f_0205fce9dba3_vela_viewer.jpg",
"vela_viewer_image_sizes":null,
"width":null,"photo_taken_at":null,
"base_revision_id":null,
"physical_filename":"Test5_ee04303e_a6f0_4372_977f_0205fce9dba3.pdf",
"physical_path":"",
"folder_id":null
}

The line breaks are added for readability.

Publish – Sample Code

Here is the code sample to upload a file to the library:

LibraryPublish

public static string LibraryPublish(string ticket, string project_id,
  string directory, string filename, string FiledataPath,
  string document_id, string tags, string caption)
{
// (1) Build request
var client = new RestClient();
client.BaseUrl = new System.Uri(_baseUrl);

// Set resource or end point
var request = new RestRequest();
request.Resource = “/api/library/publish”;
request.Method = Method.POST;

// Add parameters
request.AddParameter(“ticket”, ticket);
request.AddParameter(“project_id”, project_id);

request.AddParameter(“directory”, directory);
request.AddParameter(“filename”, filename);

// Optional parameters
//request.AddParameter(“document_id”, document_id);
request.AddParameter(“tags”, tags);
//request.AddParameter(“caption”, caption);

// Add Files
request.AddFile(“Filedata”, FiledataPath);

// (2) Execute request and get response
IRestResponse response = client.Execute(request);

// Save response. This is to see the response for our learning.
m_lastResponse = response;

return response.StatusCode.ToString();
}

I took Field API Intro Lab2 project, and modified to run /api/library/publish. The workflow here is:

  • /api/login (same as in Lab2)
  • /fieldapi/admin/v1/project_names (same as in Lab2) 
  • /api/library/publish 

The sample application’s UI looks like below:

2015_0806_uploadingFileToLibrrary_UI

Note: for simplicity, I have hard-coded tags in this sample:

string tags = “MyTag,YourTag”;

When the call returns the status code “Created”, you can also verify in the UI if the file is uploaded:

LibraryUI

 

A sample project is can be downloaded from here.

Mikako

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s