Exporting Case Comments and Attachments with FogBugz API


Follow

Overview

You can export all case comments (not just the last comment) and export all case attachments (including direct links to the attachments) using the FogBugz API.

We have provided also a sample Python script to download multiple attachments that you can use as a guide to develop similar functionality in your preferred language.

 


 

Solution

The steps below are executed using Postman, but can be executed using any tool capable of executing API calls.

You can return both, a list of cases and any other exact information about them at the same time using the search command as described in the FogBugz API: Listing, Searching and Viewing Cases article.

 

Retrieving Case Comments

  1. Get a token, either using the API, or using the Web UI.
  2. To search for a case or cases, use search API command, also passing the token retrieved and the previous step. For instance, to retrieve the metadata of case #24, use the following call body:
    {
    "token": "<Enter token from step 1>",
     "q": "case:24",
     "cols": ["events"]
     } 

    Note: Alternatively you can also use a GET request, for eg:

    https://trilogy-team.manuscript.com/api.asp?cmd=search&q=24&cols=events&token=yourtoken
  3. Comments are listed in the results under the element (aka column) s, as indicated below for this example:

    mceclip1.png

     

  4. Compare the comment or comments with FogBugz UI, as seen below:

    mceclip2.png

     

 

Retrieving Attachments

  1. Execute the steps listed under Retrieving case comments to get the case events.
  2. If you have an attachment, it will be presented under the rgAttachments property of the event where it was attached, as seen below for this example:

    API_Retrieve_Attachment.png

  3. Create the direct link to the attachment by

    • Using your instance URL

    • Add the content of the sURL value after you replace in it each “&amp;” string with the “&” sign

    • remove sTicket= from the sURL value
    • Add your token to the end of the URL

    • Eg:

      https://trilogy-team.manuscript.com/default.asp?pg=pgDownload&pgType=pgFile&ixBugEvent=115&ixAttachment=2&sFileName=TextFile.txt&token=mytoken

       

    Note: The token at the end of the full URL is important, otherwise the attachment download will silently fail.

  4. Repeat the procedure above for all cases to retrieve all the comments or attachments you need.

 

Python Script to Download Multiple Attachments

The Python script attached to this article downloadAttachments.py can assist you in the process of retrieving attachments automatically. 

Note: This script is offered only as a guide to develop similar functionality in your preferred language. Our Support Team should not be contacted with customization requests for this script.

To use the script, follow the steps below:

  1. Ensure you have the latest Python installed (the script was tested with v3.10.2). Python can be downloaded from python.org for any operating system. How to use Python on Windows or Linux is also documented on docs.python.org

    Note: the script uses the following Python modules (you might need to install them using pip):
    • requests (eg: python -m pip install requests)
    • json
  2. Retrieve an API token from a user with Administrator rights.
  3. Create and save a filter with all of the cases in your instance.

    Note: Filters can list a maximum of 100.000 results. So if you have more than that, you will need to run the script 100 thousand cases at a time. The pagination can be done by using several saved filters.
  4. Access the filter and take note of the filter ID, which is the number in the URL after /f/filters/.
  5. Edit the information below in the attached script 
    • line 6: substitute <your-API-token> for your API token from step 2.
    • line 9: substitute <your-subdomain> for your domain. It's the part before the ".fogbugz.com" in your URL (less the https:// part).
    • line 12: substitute 11 for the id of your saved filter from step 3.

Once that is done, save the Python script and run it in an empty directory (eg: python downloadAttachments.py). The script will concatenate the case number to the attachment filename and download it. The end result will be as below:

Attachmet_Retrieval_Python_Result.png

 

Back to the top