2

I know that I can save Add-In state per document using document.settings.

Is there a way to save state (e.g. active license) across documents for the same Add-In?

Jonas Kemper
  • 3,745
  • 3
  • 14
  • 21

2 Answers2

2

How you save user state depends on your needs.

If you want to track per-machine state you can save the data in HTML5 Local Storage.

If you want to track per-user state for signed-in users (no matter what machine or document they use), you should save their User ID and state on your database. For my own add-in, I implemented this design with Firebase:

function readUserStateAsync(customerId, callback){
    var myUserReference = firebase.database().ref("myCustomers/" + customerId);
    myUserReference.once('value').then(function(userState) {
        callback(userState.val());
    });
}

function writeUserStateAsync(customerId, userState, callback){
    var myUserReference = firebase.database().ref("myCustomers");
    myUserReference.child(customerId).set(userState, function(){ callback("success"); });
}

-Michael Saunders, program manager for Office add-ins

Michael Saunders
  • 2,662
  • 1
  • 12
  • 21
1

To add to Michael Saunders' answer: you can also get a unique ID for the signed-in Office user (or more specifically, to the user identity who acquired the add-in): https://msdn.microsoft.com/en-us/library/office/jj164035.aspx

  • I read that complex page, but is there a way to simply get a unique user ID using JavaScript so I can send it to my server? My goal is to store user-specific information on my server so that user can use the info across different documents and hosts as long as they have the add-in installed. I'd prefer not to require an added log-in. – Andrew Hall Aug 27 '16 at 18:05
  • 1
    @ExcelAnalyst, the "cid" field of https://msdn.microsoft.com/en-us/library/office/jj163880.aspx is a *reasonably* good value to use. There are several caveats, though. a) It may sometimes be empty (if the add-in was bought on behalf of the organization, or if the user is somehow not signed in with a Microsoft Account). b) it is not guaranteed to stay the same forever (that's what the validation-returned ID is guaranteed to be, whereas the cid one might someday change if the hashing algorithm changes). But, when not empty, it's a pretty good indicator of same user. – Michael Zlatkovsky - Microsoft Aug 28 '16 at 19:00
  • 1
    PS: To be clear, the **converse** of the last statement would not be true. I.e., different cid values do NOT necessarily mean different users (could be different hashes of the same user, over a long enough period of time). But same CID value -- I think -- will [almost] always mean the same user. If you need a guarantee, though, you'll need to use the server-side validation. – Michael Zlatkovsky - Microsoft Aug 28 '16 at 19:01
  • 1
    One other note: in Office 2016, users can actually acquire free add-in anonymously. So in those cases, the "cid" field could be empty. – Michael Zlatkovsky - Microsoft Sep 08 '16 at 23:45