XPCOM Javascript Collection Access

48 views
Skip to first unread message

Anda Zhou

unread,
Jul 22, 2016, 12:52:17 PM7/22/16
to zotero-dev
Hello!

I have a rather simple question, but haven't found a solution after perusing Zotero's source code on Github. I am trying to use the xpcom javascript API to access the currently active Zotero collection in the zoteroPane. I want to add items to the currently selected collection. I can get the following to output the correct current collection name, but when I try to add items the Library freezes:

var collection = Zotero.getActiveZoteroPane().getSelectedCollection();

I have the itemID from itemID = item.save()

I have tried both various combinations of addToCollection(collection), collection.addItem(item), collection.addItem(itemID) and none of worked.

Could someone help me figure out the proper way to go about doing this?

Thank you very much!

Dan Stillman

unread,
Jul 22, 2016, 1:17:44 PM7/22/16
to zoter...@googlegroups.com
1) Are you using Zotero 4.0 or 5.0? That affects the correct way to do this.

2) Are you watching the Browser Console for errors?

Anda Zhou

unread,
Jul 22, 2016, 1:28:13 PM7/22/16
to zotero-dev
I am using Zotero 4.0

I am getting error logs to the console. I'm able to add items and set fields and such just fine. Everything else on the xpcom works, just this collection issue. When I try addToCollection, I get an "addToCollection" is not defined error. When I tried collection.addItem I managed to freeze the library panel in that it would remain collapsed I wouldn't be able to view child collections.

Thank you for the quick response

Dan Stillman

unread,
Jul 22, 2016, 1:35:11 PM7/22/16
to zoter...@googlegroups.com
On 7/22/16 1:28 PM, Anda Zhou wrote:
> I am using Zotero 4.0
>
> I am getting error logs to the console. I'm able to add items and set
> fields and such just fine. Everything else on the xpcom works, just
> this collection issue. When I try addToCollection, I get an
> "addToCollection" is not defined error. When I tried
> collection.addItem I managed to freeze the library panel in that it
> would remain collapsed I wouldn't be able to view child collections.

"addToCollection" exists in 5.0, not 4.0. If you're looking for example
code for 4.0, you'd want to look at the 4.0 branch.

For the collection.addItem() issue I'd need to see the exact code you're
using, including the item creation.

Anda Zhou

unread,
Jul 22, 2016, 1:43:46 PM7/22/16
to zotero-dev
I am creating an item with: 

var newItem = new Zotero.Item("journalArticle");

Then setting various fields using newItem.setField;

Then saving.

                var itemID = newItem.save();
                
                collection.addItem(itemID);

I don't get an error, but the item still gets added to the root collection. The collection is created with:

  var collection = Zotero.getActiveZoteroPane().getSelectedCollection();

Dan Stillman

unread,
Jul 22, 2016, 1:47:18 PM7/22/16
to zoter...@googlegroups.com
On 7/22/16 1:43 PM, Anda Zhou wrote:
> I am creating an item with:
>
> var newItem = new Zotero.Item("journalArticle");
>
> Then setting various fields using newItem.setField;
>
> Then saving.
>
> var itemID = newItem.save();
> collection.addItem(itemID);
>
> I don't get an error, but the item still gets added to the root
> collection. The collection is created with:
>
> var collection = Zotero.getActiveZoteroPane().getSelectedCollection();

Well, everything gets added to the library root. Are you saying it's not
also being added to the selected collection?

Can you provide a Debug ID [1] for the operation?


[1] https://www.zotero.org/support/debug_output#debug_output_logging

Anda Zhou

unread,
Jul 22, 2016, 2:10:46 PM7/22/16
to zotero-dev
Solved. I have a nested function, moved the collection declaration from the outside function to the inside function right above item.save() and now works marvelously. I thought JS's variable scope meant I could use variables from an above/outside function, but nonetheless it works now! Thanks very much for your time!
Reply all
Reply to author
Forward
0 new messages