Edit multiple items at once

I would like to be able to select multiple Zotero items and change, e.g., their Item Type, Author, etc. thanks
  • edited September 7, 2013
    this has been planned for a long time and is on the medium-term agenda, if we're luck it might make it into Zotero 4.2, more likely the major version after.
  • excellent ☺
    thanks
  • For now, one could do that using the Javscript API as mentioned here: https://www.zotero.org/support/dev/client_coding/javascript_api

    Just install the ExecuteJS plugin in Firefox and then open a new tab and open Zotero there. Now, use ExecuteJS to execute your custom JS which modifies all the required items at once.
  • used the code below, didn't work. any guidance will be appreciated
    __________________________________________

    var fieldName = "itemType";
    var oldValue = "Book";
    var newValue = "Journal Article";

    var fieldID = Zotero.ItemFields.getID(fieldName);
    var s = new Zotero.Search;
    s.addCondition(fieldName, 'is', oldValue);
    var ids = s.search();
    if (ids) {
    for each(var id in ids) {
    var item = Zotero.Items.get(id);
    var mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
    item.setField(mappedFieldID ? mappedFieldID : fieldID, newValue);
    item.save();
    }
    alert(ids.length + " items updated");
    }
    else {
    alert("No items found");
    }
  • SyntaxError: missing ( after for
  • for each doesn't exist in Firefox anymore.

    You could try

    for (var i in ids) {
    var item = Zotero.Items.get(ids[i]);

    I think that should work.
  • Thanks adamsmith!

    I changed the lines as instructed, got rid of the "for each" error

    getting this one instead:
    Error: Invalid field 'itemType" for base field in ItemFields.getFieldIDFromTypeAndBase()

    I wanted to change multiple entries that have "Item Type" field "Books" to "Journal Article"

    Thanks again!

    Peter
  • edited December 1, 2015
    ah sorry for not spotting that. Those should be "book" and "journalArticle" respectively. See http://aurimasv.github.io/z2csl/typeMap.xml for a full list.

    edit: though it's also possible that this basic method just doesn't work for item types; I don't know that part of the code well enough.
  • yes those variables were changed, but still got:
    Error: Invalid field 'itemType" for base field in ItemFields.getFieldIDFromTypeAndBase()


    var fieldName = "itemType";
    var oldValue = "book";
    var newValue = "journalArticle";

    var fieldID = Zotero.ItemFields.getID(fieldName);
    var s = new Zotero.Search;
    s.addCondition(fieldName, 'is', oldValue);
    var ids = s.search();
    if (ids) {
    for (var i in ids) {
    var item = Zotero.Items.get(ids[i]);
    var mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
    item.setField(mappedFieldID ? mappedFieldID : fieldID, newValue);
    item.save();
    }
    alert(ids.length + " items updated");
    }
    else {
    alert("No items found");
    }
  • Try this:

    var oldValue = "book";
    var newValue = "journalArticle";

    var s = new Zotero.Search;
    s.addCondition('itemType', 'is', oldValue);
    var ids = s.search();
    if (ids) {
    for (var i in ids) {
    var item = Zotero.Items.get(ids[i]);
    item.setField('itemType', newValue);
    item.save();
    }
    alert(ids.length + " items updated");
    }
    else {
    alert("No items found");
    }


    If that doesn't work, then I don't think that itemType can be changed with a simple bit of code.
  • Hi bwiernik

    the code gave me this message: "itemType" is not a valid itemData field.

    thanks to you and adamsmith for trying!

    I guess I will be spending the next hour changing entries.


    Peter
  • fail with Error: Invalid field 'itemType" for base field
    with following script :(

    var fieldName = "itemType";
    var oldValue = "journalArticle";
    var newValue = "report";
    var fieldID = Zotero.ItemFields.getID(fieldName);

    var items = Zotero.getActiveZoteroPane().getSelectedItems();

    await Zotero.DB.executeTransaction(async function () {
    for (let item of items ) {
    let mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
    item.setField(mappedFieldID ? mappedFieldID : fieldID, newValue);
    await item.save();
    }
    });

    return items.length + " item(s) updated";
  • For batch editing of item types through Zotero's JavaScript API, see this discussion.

    You could also use the Zutilo add-on, which now provides a "Paste item type" function.

    The Zutilo functions for batch editing of item metadata are: "Copy item fields", "Paste into empty item fields", "Paste non-empty item fields", "Paste all item fields", and "Paste item type". They are explained in Zutilo's Command reference. Use cases are discussed in the Usage notes.
  • You should keep in mind that changing the type of Zotero items can give rise to data loss for some fields. Currently, only valid fields are preserved. See this discussion.
  • yes, it works with Zutil menu command "Copy item fields" and "Paste item type", thanks a lot!
  • and also work with following script for selected items, refs https://forums.zotero.org/discussion/78245/i-need-javascript-code-to-change-a-lot-items-type

    var fieldName = "report";
    const zoteroPane = Zotero.getActiveZoteroPane();
    const selected = zoteroPane.getSelectedItems();
    for (const item of selected) {
    item.setType(Zotero.ItemTypes.getID(fieldName));
    await item.saveTx();
    }
    return selected.length + " item(s) updated";
  • "this has been planned for a long time and is on the medium-term agenda". It's 2022. Any news of "Search and Replace"? Please. Implement. It. How to replace hundreds of items' attributes? KR
Sign In or Register to comment.