How to get the list of folders in the sidebar using AppleScript?
It is okay to get the list of all items too since I will be able to search for them
You can't. Finder's Sidebar is a location for shortcuts to be easily accessible by the user.
From Apple's Human Interface Design Guidelines:
the sidebar in a Finder window includes a list of frequently accessed locations and tag-based search shortcuts. The user can select one to access its contents or results in the main portion of the window.
Additionally, having a folder list is not practical in this element. Anything more than 2 levels of hierarchy (which is highly plausible in a folder listing) is discouraged. In this instance, Apple states that a column view be used.
In general, refrain from exposing more than two levels of hierarchy within a sidebar. In some cases, a second sidebar may be warranted when a data hierarchy is deeper than two levels. If your app requires the navigation of deeply nested objects, consider implementing a column view.
Bottom line; Apple discourages 3rd party developers from doing this, meaning Apple's Finder is definitely not going to show files like this.
You can put a shortcut to a folder, but you won't get a list of folders.