How to find the Office AddIn Host it is a Word application or Excel using office.js?
Asked Answered
P

3

7

I am creating a office AddIn which works in both excel and word applications and based on host if it is a word or excel host i want to execute different logic. I am using office.js to create office Addin.

for example :-

let say type="Excel" // after some logic executed 

if(type=="Excel")
 {
//run code for excel applications 
}
else
{
//run code for word applications
}

I have tried to use the bellow:-

 if (Office.context.requirements.isSetSupported('ExcelApi', '1.1')) {
            alert("yes it is excel");
        }

but it is not working when i run it in excel.

I have sent the host in manifest file also

 <Hosts>
     <Host Name="Document" />
    <Host Name="Workbook" />
  </Hosts>

also I got some code alter doing a lot of googling I found the bellow code which is not working for me

function getHostInfo() {
    var _requirements = Office.context.requirements;
    var types = ['Excel', 'Word'];
    var minVersions = ['1.1', '1.0']; // Start with the highest version

    // Loop through types and minVersions
    for (var type in types) {
        for (var minVersion in minVersions) {

            // Append "Api" to the type for set name, i.e. "ExcelApi" or "WordApi"
            if (_requirements.isSetSupported(types[type] + 'Api', minVersions[minVersion])) {
                return {
                    type: types[type],
                    apiVersion: minVersions[minVersion]
                }
            }
        }
    }
};

Thank you

Paolo answered 13/6, 2016 at 8:37 Comment(0)
D
1

Update Dec 5, 2016: We will soon be releasing an API to detect the host and platform information (partially in response to the fact that the _host_info URL paramater, which folks had unofficially relied on, needed to be recently removed for Office Online). We also have a temporary workaround in anticipation of the forthcoming official API. See "In Excel Online, OfficeJS API is not passing the host_Info_ parameter anymore to Excel Add-In" for more info.

Note that for many light-up scenarios, you would still be better off using API Set detection. See "Neat ways to get environment (i.e. Office version)" for more information on requirement sets.


The if (Office.context.requirements.isSetSupported('ExcelApi', '1.1')) should work for you, IF you are in Excel 2016. It will not work (i.e., return false) in 2013.

If you are targeting Office 2013, and need a solution just for Word & Excel, you can use the ability to write OpenXML as a distinguishing factor (Word can, Excel can't). So check for Office.context.requirements.isSetSupported('OoxmlCoercion'). It will return true for Word, false for Excel.

Dogy answered 13/6, 2016 at 16:53 Comment(19)
Hi @Michael will the code Office.context.requirements.isSetSupported('OoxmlCoercion') work on Office 2016 as well ? I am unable to check as I don't have office 2016 installed on my machine. thanks.Paolo
Yes, it will work on all platforms/versions where Office Add-ins are supported.Dogy
Hi @Michael Zlatkovsky, Sorry to say but the code that you said it will work it is not working today I was doing testing of my addin and I had written the logic as per the host application but it is not working please look in to this. function someFunction(obj) { //check for the host is excel or Word if (Office.context.requirements.isSetSupported('OoxmlCoercion')) { //logic when it is word host } else { //do someting when it is excel }Paolo
Are you referencing the CDN version of Office.js?Dogy
Could you try the CDN location? And/or updating your Office.js Nuget package to latest?Dogy
I have tried the Cdn's "appsforoffice.microsoft.com/lib/1.1/hosted/office.js" and also " also I tried "appsforoffice.microsoft.com/lib/1/hosted/Office.js" but none of them worked.Paolo
Very strange. Could you describe what versions of Office you're testing on (desktop or online, what version # if desktop, and what version of online -- onedrive vs. onedrive for business, vs on-prem sharepoint?)Dogy
It is office 2013 desktop version. I tested the code after i created a demo application for Word Addin and it is working there but not in my code where i need to work on mixed environment with AddIn in word and excel both with the same addInPaolo
@MichaelZlatkovsky The office.js API are envolving. It is so vague to determine a host by function sets or functions which may exist tomorrow. Please provide a method to excactly determine the host! (All the other stuff may of course done by set or function checks!)Manicdepressive
@ndee, see my comment at #32840959. For 2013, you sort of have to live with what has shipped (though FWIW, those APIs by definition are static in time). For 2016+, you can always find out the host by querying Office.context.requirements.isSetSupported("ExcelApi"), or WordApi, etc.Dogy
@PradeepGaba, I'm not sure what you mean / what your question is. If you are able to make the code work in a demo application, what is different about your other application?Dogy
@MichaelZlatkovsky in addIn manifest i have written the bellow code <DefaultSettings> <SourceLocation DefaultValue="localhost:12345" /> </DefaultSettings> which actually a address of a mvc application in which all the logic resides and I am hosting this mcv application in the add in and this mvc application is using office.js which is working fine. but only in this case were i need to determine the host it is not working. today i created a word addIn as a demo and i replaced the demo addin manifest the above code but still condition goes false :(Paolo
@MichaelZlatkovsky if I am creating a new project the logic you said is working but not when i replaced the manifest code with the bellow one <DefaultSettings> <SourceLocation DefaultValue="localhost:12345" /> </DefaultSettings>Paolo
Are you sure that the code you're running on localhost:123456 is doing the right thing? In particular, is it pointing at the CDN? And does it have Office.initialize? Again, sample code (or link to zipped-up as-simple-as-possible project) would help.Dogy
It is working fine now .It was my mistake i was pointing to wrong cdn. thank you.Paolo
@MichaelZlatkovsky do you have any idea what is is the official release date of github.com/OfficeDev/office-js-docs/tree/WordJs_1.3_Openspec/… which is currently a preview version.Paolo
Almost all of the code for 1.3 will be available in the "June" update to Office 2016, which usually gets released within a few weeks of the month that it's named after (so in this case, a few weeks from today). Once it's out, we'll release the new Office.js on the beta CDN, and update it again next month when the rest of 1.3 is finished. And from Beta to Prod is usually a couple of months (though you can certainly develop agains the beta).Dogy
@PradeepGaba, see update to my answer, regarding detecting host and platform.Dogy
Thanks @MichaelZlatkovskyPaolo
B
0

You can always inspect the location.search object - should return a string like ?_host_Info=Word|Win32|16.01|en-US.

Benjy answered 16/8, 2016 at 5:10 Comment(1)
we are evaluating adding an API to detect host ("Excel, Word, Outlook, etc) & platform (Desktop, Mac, Web, etc) info. To make sure we're taking it into account, would you mind describing the particular use-cases you have that led you to seek out _host_Info? Thanks!Dogy
A
0

Note the upcoming API that provides a formal API to get such information.

Specification link: https://github.com/OfficeDev/office-js-docs/tree/ContextAdditions_OpenSpec

This is a better way to get the information you are looking for rather than relying on URL query string or session storage. Such methods are bit risky as underlying behavior could change without warning. It is always a good option to use published APIs.

Archle answered 3/12, 2016 at 2:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.