none
Is there an example of how to write an app for the web version of Office in C#? RRS feed

Answers

  • Also, while not quite "apples to apples," there is a code sample that demonstrates using ASP.NET.

    Apps for Office: Use Microsoft Translator and ASP.NET in an app (http://code.msdn.microsoft.com/officeapps/Apps-for-Office-Use-fe2fcd95)

    thanks,

    -sdo

    • Marked as answer by DavidThi808 Thursday, May 30, 2013 9:26 PM
    Wednesday, May 29, 2013 10:02 PM
    Moderator
  • At this point, unfortunately there is no equivalent .Net API for apps for Office.
    Tuesday, April 30, 2013 6:53 PM
  • Yes...you can write a web service as i wrote in previous post.

    You can "change" and more or less copy paste all the existing code you have and create a web serivce for it then call it with ajax.

    All you need to do on server side is:

    • a) limit yourself to CORS enabled browsers (all except older then IE9) and add CORS to server side or
    • b) add jsonp support to web service calls

    What i do is always return a json object { stats: statusCode, data: ... } . Then before final return (Render or in MVC AfterExecute) i check if request has url parameter "callback" and if so, i wrap the response into jsonp supported format.


    Anze Javornik

    Tuesday, April 30, 2013 10:44 PM

All replies

  • I am not sure, but i belive that office web apps do not yet support Apps for Office.

    Some people reported they get an error if they try to open a document with app saying "App Warning This app is not available in the Excel Web App. Open this document in Excel to use the app". Look more here .

    Maybe this already got fixed...


    Anze Javornik

    Monday, April 29, 2013 11:02 PM
  • You can first develop the app locally, then publish it to the Office store. Once published, the web based Office will be able to recognize it when opening a document containing references to the app.

    Hope that helps.


    Monday, April 29, 2013 11:03 PM
  • I understand that part. My question is how do I create one using C# & ASP.NET instead of HTML5 and JavaScript. What we have has a ton of C# code in it and JavaScript is not up to the type of code we have.

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Monday, April 29, 2013 11:05 PM
  • When i tried to use any other page file different from .html i got app error and it did not even hit the page load in aspx (i tried on mail apps only).

    You could create a a html file in which you would wrap all aspx method calls and handle responses...like changing your current code to be more of a web service then asp pages.

    I also never tried how app behaves if you insert additional iframe and try loading server side code there...that might work.


    Anze Javornik

    Monday, April 29, 2013 11:13 PM
  • The web project associated with the Office App supports aspx page with code-behind. You can add Web Forms to the project and C# code files as well.

    Let me know if you get errors during development.

    Tuesday, April 30, 2013 12:30 AM
  • Is there a sample anywhere? The JavaScript communication with Office is difficult to map to a C# ASP.NET app. Thanks Dave

    Who will win The Windward International Collegiate Programming Championships?

    Tuesday, April 30, 2013 2:28 AM
  • At this point, unfortunately there is no equivalent .Net API for apps for Office.
    Tuesday, April 30, 2013 6:53 PM
  • Hi;

    I don't understand. You said previously that this is supported. And fundamentally it's a JavaScript API so this should be doable. Why is it not possible?

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Tuesday, April 30, 2013 7:42 PM
  • Or... Is there a way to create a .NET server component that the HTML5/JavaScript part can call? So the server component exists to do the heavy lifting but the client part is all HTML5/JavaScript.

    ??? - thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Tuesday, April 30, 2013 10:38 PM
  • Yes...you can write a web service as i wrote in previous post.

    You can "change" and more or less copy paste all the existing code you have and create a web serivce for it then call it with ajax.

    All you need to do on server side is:

    • a) limit yourself to CORS enabled browsers (all except older then IE9) and add CORS to server side or
    • b) add jsonp support to web service calls

    What i do is always return a json object { stats: statusCode, data: ... } . Then before final return (Render or in MVC AfterExecute) i check if request has url parameter "callback" and if so, i wrap the response into jsonp supported format.


    Anze Javornik

    Tuesday, April 30, 2013 10:44 PM
  • Hi;

    For the web service - do you recomend a REST API? And then do you prefer JSON over XML for the returned data (I've always used XML so I can use XPath to access specific nodes)?

    thanks - dave


    Who will win The Windward International Collegiate Programming Championships?

    Wednesday, May 1, 2013 1:00 PM
  • I would recomend the REST API and JSON.

    REST API will allow your services to be easily called from javascript and JSON has a lot of support in javascript already and .Net . Also for cross domain calls you need the server to return a so called JSONP (which is bascially a JSON).

    Another option is to create the app with MVC which works so that every method has its own url and is kinda web service alike...this way a lot of page rendering, parsing, ... is already built in.


    Anze Javornik

    Thursday, May 2, 2013 8:35 AM
  • Hi Anze,

    Could you please provide some sample. I have the Cloud Service built with REST API but I have no success yet in making POST call to one of Web API method.

    I'm following all your posts and they are very helpful to me.

    I'm using dataType: 'jsonp' in ajax call and also add jsonp support to server side. I also have CORS supported on Server side.
    I would like to do POST operation either with AJAX or XMLHttpRequest but both are not working.

    I have to pass an Object to one of API call which does calculation and pass results as another Object.

    Thank you in advance for all your help.

    -Vik

    Friday, May 3, 2013 6:20 PM
  • Hi.

    Ajax POST does not support jsonp as dataType. How jsonp works (and why post does not work) is in simple form explained here . JSONP requests are always GET.

    To do a POST request with json you need to:

    • set dataType to 'json'
    • set crossDomain: true
    • add CORS support to server side
    • use CORS supported browser (IE10, Chrome, Firefox, ... IE9 or older do not support CORS)

    Code below is a simplifed sample of MVC project i am using (it is more or less the same as Web API method)

    Server side:

    Base functions (handleing conversion and so on):

    public bool IsJsonp { get { return Request.QueryString["callback"] != null; } }
            public bool IsJson { get { return Request.AcceptTypes.Contains("application/json") || Request.AcceptTypes.Contains("*/*"); } }
            public string JsonpCallback { get { return Request.QueryString["callback"]; } }
    
    public virtual void AddHeaders()
    {
        if (!IsUrlReferrelLoopback)
        {
            // if we are working with x-domain, make sure we only allow to registered application
            //Response.ContentType = "text/html";
            Response.Headers.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
            //Response.Headers.Add("Access-Control-Max-Age", "1000");
            Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type");
            Response.Headers.Add("Access-Control-Allow-Origin", Request.Headers.AllKeys.Contains("Origin") ? Request.Headers["Origin"] : Request.UrlReferrer.Host);
            Response.Headers.Add("Access-Control-Allow-Credentials", "true");
        }
    }
    Web service (MVC method)
    [HttpPost]
    public ActionResult Test(string param)
    {
        return Json(new
            {
                    status = 200
            }, "text/plain", JsonRequestBehavior.AllowGet);
        // setting dataType to text/plain sometimes help...you should use application/json if it doesnt couse any problems
    }

    Javascript

    <script>
        $.ajax({
            url: pathToService + '/Test',
            data: { param: 'testValue' },
            crossDomain: true,
            dataType: 'json',
            cache: false,
            type: 'POST'
        });
    </script>


    Anze Javornik

    Friday, May 3, 2013 9:27 PM
  • Hi Anze,

    Thank you for your quick and valuable response.

    I implemented CORS and JSONP support on my Server side which is ASP.net 4 web APIs:
    http://aspnetwebstack.codeplex.com/wikipage?title=CORS%20support%20for%20ASP.NET%20Web%20API&referringTitle=Specs
    http://www.west-wind.com/weblog/posts/2012/Apr/02/Creating-a-JSONP-Formatter-for-ASPNET-Web-API

    I made my ajax call as explained below but I'm still getting: JavaScript runtime error: Access is denied. It is failing in side jquery during socket open (xhr.open)

    GET is working but POST is causing an issue.

    Thank you again,

    Vik

    Monday, May 6, 2013 1:55 PM
  • Are you making an ajax call with jsonp? If so then you should know, that jsonp does not support POST requests as it is a GET only.

    To use CORS you should use only JSON...note that this (CORS) also needs to be supported by browser (all but IE9 or older do support this)

    If IE9 and older browsers still need support for this, then you need to proxy the web service by writting your own web service with JSONP support and make the method for GET operation. In this method proxy the target ewb service by calling POST web service.


    Anze Javornik

    Monday, May 6, 2013 2:48 PM
  • I'm making ajax call with json for POST operation.

    I'm using IE 10 and not needed to support old browsers.

    Code Snippet(I also try with JSON.stringify but no success) :

                    var requestUrl = "http://127.0.0.1:81/api/webAPI/Test";

                    $.ajax({
                        url: requestUrl,
                        cache: false,
                        crossDomain: true,
                        type: 'POST',
                        //contentType: "application/json; charset=utf-8",
                        dataType: 'json', 
                        data: { inputMsg: 'Hello World' },
                        //data: { inputMsg: JSON.stringify('Hello') },
                        success: function (data, textStatus, jqXHR) {
                            // here you have result in your data parameter
                            app.showNotification("Success", "Got data from Service");
                        },
                        //complete: function (data, textStatus, jqXHR) {
                        //    // here you have result in your data parameter
                        //    app.showNotification("Success", "Got data from Service");
                        //},
                        error: function (data, textStatus, errorThrown) {
                            app.showNotification("An error occured", errorThrown);
                        }
                    });

            [EnableCors("*", "*", "*")]
            [HttpPost]
            public HttpResponseMessage Test([System.Web.Http.FromBody] string inputMsg)
            {
                try
                {
                    string returnVal = string.Empty;

                    if (ModelState.IsValid)
                    {
                        return new HttpResponseMessage(HttpStatusCode.OK);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                return new HttpResponseMessage(HttpStatusCode.OK);
            }

    If I change the code to GET it works fine.

    Thank you for your continued support.

    Monday, May 6, 2013 7:26 PM
  • I am not sure how EnableCorsAttribute works, since i wrote my own classes to handle this before this library was available.

    This behaviour can also be browser specific...for example some browsers need to get all the exact mathods allowed not just "*". (Some suggest that you need to manully add to allow method POST in the header and also for accept header you need to allow Contet-Type)

    Read here for more info . Also check if you can gt a more detailed error information. Either try your code in IE10 and F12 tool in script tag or (if this just gives Access Denied error) try it in FireFox using Firebug...this sometimes gives a lot more details on error and then paste this detailed error.


    Anze Javornik

    Tuesday, May 7, 2013 6:55 AM
  • Also, while not quite "apples to apples," there is a code sample that demonstrates using ASP.NET.

    Apps for Office: Use Microsoft Translator and ASP.NET in an app (http://code.msdn.microsoft.com/officeapps/Apps-for-Office-Use-fe2fcd95)

    thanks,

    -sdo

    • Marked as answer by DavidThi808 Thursday, May 30, 2013 9:26 PM
    Wednesday, May 29, 2013 10:02 PM
    Moderator
  • Hi;

    This example was exactly what I was looking for. Absolutely perfect!

    THANK YOU


    Who will win The Windward International Collegiate Programming Championships?

    Thursday, May 30, 2013 9:27 PM