The most basic Evernote note will contain only text. They can get much more complex than that, of course, but we need to crawl before we can walk.
Prerequisites
To proceed with this example, we’ll assume that the following are already in place:
- You have a developer token (or a working OAuth implementation by which you can retrieve a valid authentication token)
- You have an instance of UserStore and NoteStore
Creating notes in Evernote is done using the NoteStore.createNote
API function. This function takes two arguments: a valid auth token (as a string) and an instance of Types.Note
. The Note
instance, in turn, will need to have the following attributes defined at minimum before we can send it to the API:
Note.title
— the title of the noteNote.content
— the body of the note, formatted as ENML (Evernote Markup Language)
Optionally, we can also set the GUID of the notebook where we’d like this note to reside (as Note.notebookGuid
). If we don’t provide this value, the note will be created in the account’s default notebook.
Each note’s body must begin with two things: the standard XML version declaration and the link to the ENML DOCTYPE definition for Evernote notes.
The XML version declaration will look like this:
<?xml version="1.0" encoding="UTF-8"?>
The ENML DOCTYPE definition (DTD) for notes will look like this:
<!DOCTYPE en-note SYSTEM "https://xml.evernote.com/pub/enml2.dtd">
If either of these nodes is omitted in the note body, the Evernote API will reject the note as malformed and invalid.
Assuming both of those pieces are in place, now we just need to add the body of the note, which must be wrapped in <en-note>
tags. Here is the body of a simple (yet complete) Evernote note:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "https://xml.evernote.com/pub/enml2.dtd">
<en-note>Hello, world!</en-note>
If we take the above XML and a title, we have the necessary parts to create a very simple text note.
Below you’ll find sample code that performs the entire note creation process.
from evernote.edam.type import ttypes | |
def makeNote(authToken, noteStore, noteTitle, noteBody, parentNotebook=None): | |
nBody = '<?xml version="1.0" encoding="UTF-8"?>' | |
nBody += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' | |
nBody += '<en-note>%s</en-note>' % noteBody | |
## Create note object | |
ourNote = ttypes.Note() | |
ourNote.title = noteTitle | |
ourNote.content = nBody | |
## parentNotebook is optional; if omitted, default notebook is used | |
if parentNotebook and hasattr(parentNotebook, 'guid'): | |
ourNote.notebookGuid = parentNotebook.guid | |
## Attempt to create note in Evernote account | |
try: | |
note = noteStore.createNote(authToken, ourNote) | |
except Errors.EDAMUserException, edue: | |
## Something was wrong with the note data | |
## See EDAMErrorCode enumeration for error code explanation | |
## http://dev.evernote.com/documentation/reference/Errors.html#Enum_EDAMErrorCode | |
print "EDAMUserException:", edue | |
return None | |
except Errors.EDAMNotFoundException, ednfe: | |
## Parent Notebook GUID doesn't correspond to an actual notebook | |
print "EDAMNotFoundException: Invalid parent notebook GUID" | |
return None | |
## Return created note object | |
return note |
- (void) makeNoteWithTitle:(NSString*)noteTile withBody:(NSString*) noteBody withParentNotebook:(EDAMNotebook*)parentNotebook { | |
NSString *noteContent = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
"<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" | |
"<en-note>" | |
"%@" | |
"</en-note>",noteBody]; | |
// Parent notebook is optional; if omitted, default notebook is used | |
NSString* parentNotebookGUID; | |
if(parentNotebook) { | |
parentNotebookGUID = parentNotebook.guid; | |
} | |
// Create note object | |
EDAMNote *ourNote = [[EDAMNote alloc] initWithGuid:nil title:noteTile content:noteContent contentHash:nil contentLength:noteContent.length created:0 updated:0 deleted:0 active:YES updateSequenceNum:0 notebookGuid:parentNotebookGUID tagGuids:nil resources:nil attributes:nil tagNames:nil]; | |
// Attempt to create note in Evernote account | |
[[EvernoteNoteStore noteStore] createNote:ourNote success:^(EDAMNote *note) { | |
// Log the created note object | |
NSLog(@"Note created : %@",note); | |
} failure:^(NSError *error) { | |
// Something was wrong with the note data | |
// See EDAMErrorCode enumeration for error code explanation | |
// http://dev.evernote.com/documentation/reference/Errors.html#Enum_EDAMErrorCode | |
NSLog(@"Error : %@",error); | |
}]; | |
} |
def make_note(note_store, note_title, note_body, parent_notebook=nil) | |
n_body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
n_body += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" | |
n_body += "<en-note>#{note_body}</en-note>" | |
## Create note object | |
our_note = Evernote::EDAM::Type::Note.new | |
our_note.title = note_title | |
our_note.content = n_body | |
## parent_notebook is optional; if omitted, default notebook is used | |
if parent_notebook && parent_notebook.guid | |
our_note.notebookGuid = parent_notebook.guid | |
end | |
## Attempt to create note in Evernote account | |
begin | |
note = note_store.createNote(our_note) | |
rescue Evernote::EDAM::Error::EDAMUserException => edue | |
## Something was wrong with the note data | |
## See EDAMErrorCode enumeration for error code explanation | |
## http://dev.evernote.com/documentation/reference/Errors.html#Enum_EDAMErrorCode | |
puts "EDAMUserException: #{edue}" | |
rescue Evernote::EDAM::Error::EDAMNotFoundException => ednfe | |
## Parent Notebook GUID doesn't correspond to an actual notebook | |
puts "EDAMNotFoundException: Invalid parent notebook GUID" | |
end | |
## Return created note object | |
note | |
end |
<?php | |
function makeNote($noteStore, $noteTitle, $noteBody, $parentNotebook = null) { | |
$nBody = '<?xml version="1.0" encoding="UTF-8"?>'; | |
$nBody .= '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'; | |
$nBody .= '<en-note>' . $noteBody . '</en-note>'; | |
// Create note object | |
$ourNote = new Note(); | |
$ourNote->title = $noteTitle; | |
$ourNote->content = $nBody; | |
// parentNotebook is optional; if omitted, default notebook is used | |
if (isset($parentNotebook) && isset($parentNotebook->guid)) { | |
$ourNote->notebookGuid = $parentNotebook->guid; | |
} | |
// Attempt to create note in Evernote account | |
try { | |
$note = $noteStore->createNote($ourNote); | |
} catch (EDAMUserException $edue) { | |
// Something was wrong with the note data | |
// See EDAMErrorCode enumeration for error code explanation | |
// http://dev.evernote.com/documentation/reference/Errors.html#Enum_EDAMErrorCode | |
print "EDAMUserException: " . $edue; | |
} catch (EDAMNotFoundException $ednfe) { | |
// Parent Notebook GUID doesn't correspond to an actual notebook | |
print "EDAMNotFoundException: Invalid parent notebook GUID"; | |
} | |
// Return created note object | |
return $note; | |
} | |
?> |
public Note makeNote(NoteStoreClient noteStore, String noteTitle, String noteBody, Notebook parentNotebook) { | |
String nBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; | |
nBody += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">"; | |
nBody += "<en-note>" + noteBody + "</en-note>"; | |
// Create note object | |
Note ourNote = new Note(); | |
ourNote.setTitle(noteTitle); | |
ourNote.setContent(nBody); | |
// parentNotebook is optional; if omitted, default notebook is used | |
if (parentNotebook != null && parentNotebook.isSetGuid()) { | |
ourNote.setNotebookGuid(parentNotebook.getGuid()); | |
} | |
// Attempt to create note in Evernote account | |
Note note = null; | |
try { | |
note = noteStore.createNote(ourNote); | |
} catch (EDAMUserException edue) { | |
// Something was wrong with the note data | |
// See EDAMErrorCode enumeration for error code explanation | |
// http://dev.evernote.com/documentation/reference/Errors.html#Enum_EDAMErrorCode | |
System.out.println("EDAMUserException: " + edue); | |
} catch (EDAMNotFoundException ednfe) { | |
// Parent Notebook GUID doesn't correspond to an actual notebook | |
System.out.println("EDAMNotFoundException: Invalid parent notebook GUID"); | |
} catch (Exception e) { | |
// Other unexpected exceptions | |
e.printStackTrace(); | |
} | |
// Return created note object | |
return note; | |
} |
var Evernote = require('evernote'); | |
function makeNote(noteStore, noteTitle, noteBody, parentNotebook) { | |
var nBody = '<?xml version="1.0" encoding="UTF-8"?>'; | |
nBody += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'; | |
nBody += "<en-note>" + noteBody + "</en-note>"; | |
// Create note object | |
var ourNote = new Evernote.Types.Note(); | |
ourNote.title = noteTitle; | |
ourNote.content = nBody; | |
// parentNotebook is optional; if omitted, default notebook is used | |
if (parentNotebook && parentNotebook.guid) { | |
ourNote.notebookGuid = parentNotebook.guid; | |
} | |
// Attempt to create note in Evernote account (returns a Promise) | |
noteStore.createNote(ourNote) | |
.then(function(note) { | |
// Do something with `note` | |
console.log(note); | |
}).catch(function (err) { | |
// Something was wrong with the note data | |
// See EDAMErrorCode enumeration for error code explanation | |
// http://dev.evernote.com/documentation/reference/Errors.html#Enum_EDAMErrorCode | |
console.log(err); | |
}); | |
} |