Attaching a File to an Equipment
Field /api/projects Returning Erroneous is_active Value?

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.  


Supported Request Methods: Post


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:

"created_at":"2015-08-12 01:08:31 -0500",
"fcreate_date":"2015-08-12 01:08:31 -0500",
"fmod_date":"2015-08-12 01:08:31 -0500",
"updated_at":"2015-08-12 01:08:31 -0500",

The line breaks are added for readability. 

Publish – Sample Code

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

        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: 


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: 



A sample project is can be downloaded from here