Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
dev:client_coding:javascript_api [2018/09/08 17:16] – [API Methods] new subsubsection about creating a new collection zuphilipdev:client_coding:javascript_api [2019/01/22 00:34] – [Zotero JavaScript API] dstillman
Line 7: Line 7:
 ====== Zotero JavaScript API ====== ====== Zotero JavaScript API ======
  
-Whereas Zotero's [[dev:web_api|Server API]] allows read and write access to online Zotero libraries, it is also possible to access the local Zotero client through the JavaScript API. (It is also possible to [[dev/client_coding/direct_sqlite_database_access|directly access the local SQLite database]], but that approach is much more fragile.)+Whereas Zotero's [[dev:web_api|Web API]] allows read and write access to online Zotero libraries, it is also possible to access the local Zotero client through the local JavaScript API. (It is also possible to [[dev/client_coding/direct_sqlite_database_access|directly access the local SQLite database]], but that approach is much more fragile.)
  
 Note that the (mostly user-contributed) documentation of the JavaScript API is not comprehensive. If you use the JavaScript API in ways beyond what's described here, please consider expanding this wiki page. Note that the (mostly user-contributed) documentation of the JavaScript API is not comprehensive. If you use the JavaScript API in ways beyond what's described here, please consider expanding this wiki page.
Line 205: Line 205:
 <code javascript> <code javascript>
 async function createCollection(name) { async function createCollection(name) {
-    let collection = new Zotero.Collection();+    var currentCollection = ZoteroPane.getSelectedCollection(); 
 +    var collection = new Zotero.Collection();
     collection.name = name;     collection.name = name;
 +    collection.parentID = currentCollection.id;
     var collectionID = await collection.saveTx();     var collectionID = await collection.saveTx();
     return collectionID;     return collectionID;
 } }
 </code> </code>
 +
 +This function creates a new collection with a given name and makes
 +it a subcollection of the currently selected one. Since the saving
 +will result in a Promise object, we add the ''await'' before the
 +function call and make sure that this is within an ''async'' function.
  
 ==== Zotero Search Basics ==== ==== Zotero Search Basics ====
Line 334: Line 341:
 the Zotero database.  This includes items and notes. the Zotero database.  This includes items and notes.
  
-<code javascript>  var related_items = item.relatedItemsBidirectional </code>+<code javascript>  var related_items = item.relatedItems</code> 
 + 
 +==== Set two items as related to each other  ==== 
 + 
 +Given two items ''itemA'' and ''itemB''. We can set them as related items 
 +to each other by using the ''addRelatedItem'' function: 
 + 
 +<code javascript>itemA.addRelatedItem(itemB); 
 +await itemA.saveTx(); 
 +itemB.addRelatedItem(itemA); 
 +await itemB.saveTx();</code>
  
-This returns a list of items just like in the search examples. 
  
 ==== Get an Item's Attachments === ==== Get an Item's Attachments ===
dev/client_coding/javascript_api.txt · Last modified: 2022/07/02 18:22 by dstillman