Question

How to download a view via API

  • 31 January 2024
  • 13 replies
  • 118 views

Userlevel 4
Badge +5

I have a view that I want to download via API.
I can list the details of the view, but I don’t know what to use to actually get the entire list/results of the view via API.
https://incidentiq.stoplight.io/docs/v1/89b6c035bf2bf-get-user-view

 


13 replies

Userlevel 6
Badge +12

You have to do a POST call, and specify the view in the payload.

Here’s how I accomplish that:

 var payloadData = JSON.stringify({
"ProductId": {ProductId},
"Filters": [
{
"Facet": "View",
"Id": {ViewId}
}
]
});
var url = "https://{yourschool}.incidentiq.com/api/v1.0/users?$s={number of results per page}"; // to avoid having to script out looping through pages, I usually just set this number insanely high, like 99999, and get all my results on one page. This works as long as you don't hit a limit with the size of JSON file you have to parse through. I think Google Apps Script has a 50MB limit or something
var options = {
method: 'POST',
headers: {
"SiteId": {Your site ID},
"Authorization": "Bearer {your API key}",
"Pragma": "no-cache",
"Accept": "application/json, text/plain, */*",
"Client": "WebBrowser",
"Accept-Encoding": "gzip, deflate",
"Content-Type": "application/json",
},
payload: payloadData,
redirect: 'follow',
muteHttpExceptions: true
};


From there you just have to parse your response out however you want it.

Userlevel 6
Badge +12

I should have mentioned that this is just a URL fetch in JavaScript.

Userlevel 1
Badge +2

Hey Everyone! 

I hope you are both doing well so far today! My name is Drew Thaxton, I am a Customer Support Specialist at Incident IQ who specializes in helping with our API calls, and I just wanted to reach back out to confirm that the API call provided by @jclark should be the correct call you will want to use to pull the users returned by a custom view! Below I also went ahead and supplied this call as well but in cURL if that may be beneficial. Please note that for the words contained in brackets, your district will want to replace the bracketed words with that information it specifies for your district.

curl --location 'https://{SITE}.incidentiq.com/api/v1.0/users?%24s=20&%24o=FullName%20ASC' \
--header 'accept: application/json, text/plain, */*' \
--header 'accept-language: en-US,en;q=0.9' \
--header 'client: WebBrowser' \
--header 'content-type: application/json' \
--header 'productid: 88df910c-91aa-e711-80c2-0004ffa00010' \
--header 'siteid: {SITEID}' \
--header 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' \
--header 'Authorization: Bearer {TOKEN}' \
--data '{"OnlyShowDeleted":false,"Filters":[{"Facet":"View","Id":"{VIEWID}"}]}'

 

Best, 
Drew

Userlevel 7
Badge +12

@jo.cpa I called in the big guns for you. I hope this helps. 😄

Userlevel 4
Badge +5

Hello.
No, I’m not trying to pull users from a View.  I’m trying to pull Assets data from a View.

Is there a location in the API docs that explains this?  I only see lists views and get details on a view.  Nothing about pulling the results of a view.

Userlevel 1
Badge +2

Hey @jo.cpa 

That shouldn’t be a problem at all! If you would like to return the results of an asset view, then you should be able to make a slight adjustment to that call so that it points to the assets and asset view instead of the user view. You should be able to make this change by updating the API URL and ViewId provided in the Body of the API call. 

curl --location 'https://{SITE}.incidentiq.com/api/v1.0/assets/?%24s=20&%24o=AssetTag%20ASC' \
--header 'accept: application/json, text/plain, */*' \
--header 'accept-language: en-US,en;q=0.9' \
--header 'client: WebBrowser' \
--header 'content-type: application/json' \
--header 'productid: 88df910c-91aa-e711-80c2-0004ffa00010' \
--header 'siteid: {SITEID}' \
--header 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' \
--header 'Authorization: Bearer {TOKEN}' \
--data '{"OnlyShowDeleted":false,"Filters":[{"Facet":"View","Id":"{VIEWID}"}]}'

Regarding the API documentation though, at this time, I was not able to locate this call in our documentation provided, but I can definitely reach out to the team to see about adding this call as well in a future update!

Best Regards,
Drew

Userlevel 6
Badge +12

Hello.
No, I’m not trying to pull users from a View.  I’m trying to pull Assets data from a View.

Is there a location in the API docs that explains this?  I only see lists views and get details on a view.  Nothing about pulling the results of a view.

Your original post linked a user view, so that was my assumption - sorry about that.


The calls mentioned are the same you would just replace “users” with “assets” in the endpoint URL and the rest will work the same just as Drew mentioned.

I had a complaint about the API documentation myself. The old documentation, while being hard to follow, listed WAY more endpoints that you could use. What’s published now seems better for reading, but is incredibly inconclusive when it comes to the available endpoints you can use.

Userlevel 4
Badge +5

@jclark Couldn’t agree more with you comment about the API docs.
Thanks for the reply.
Drew’s curl worked for me to figure out.
I’m using postman to model calls before we put it into code/system.

Hi,

@jclark is there a way to get around the limit of the JSON file?

I’m using the API call you provided in Postman, but using $s=99999 gives me an error saying “Maximum response size reached”. The view I’m trying to call includes over 19000 users. Even adjusting to 20000 still causes the error and I’d like to be able to pull all the results in one call, if that’s even possible.

Userlevel 6
Badge +12

@JBauer 693f843 mva I’m assuming that’s just a Postman limitation? I am able to run that in Apps Script without issue, but I’ve hit some other limits there too.

You could try paging to get around that. If you lower the page size, the response should be smaller, but also give you a number of pages, and you could loop through them.

For example: if you set your URL to look at 200 users at a time, you would include the page index, so…  

/api/v1.0/users?p=0&$s=200

where the p=0 is your page index, so after it finds the first 200 items, you’d increase to p=1 and run it again to get the next 200 users

/api/v1.0/users?p=1&$s=200

Your JSON output should have a Paging section at the bottom to tell you how many pages there are and how many total rows (users) and how many pages. My example, I have a view set up with 798 users in it, and if I call 200 at a time it looks like this.

"Paging": {
"TotalRows": 798,
"PageCount": 4,
"PageSize": 200,
"PageIndex": 0
},

So looping through it like if PageIndex < PageCount, increase the “p” by 1.

 

Also worth noting, I do not have a formal background in any of this and I also don’t have much experience with Postman, so this may not be the exact answer you need, but I hope it’s helpful regardless.

@jclark Looks like increasing the limit in Postman did the trick. I appreciate your assistance and for providing all the detailed and insightful info.

Userlevel 6
Badge +12

@jclark Looks like increasing the limit in Postman did the trick. I appreciate your assistance and for providing all the detailed and insightful info.

No problem!!

Userlevel 7
Badge +12

Great job @jclark! @JBauer 693f843 mva Glad you were able to get this figured out 😄

Reply