|
| 1 | +--- |
| 2 | +page_type: sample |
| 3 | +products: |
| 4 | +- office-excel |
| 5 | +- office-365 |
| 6 | +languages: |
| 7 | +- javascript |
| 8 | +- csharp |
| 9 | +extensions: |
| 10 | + contentType: samples |
| 11 | + technologies: |
| 12 | + - Add-ins |
| 13 | + createdDate: 01/29/2020 1:25:00 PM |
| 14 | +description: "" |
| 15 | +--- |
| 16 | + |
| 17 | +# Use a shared library to migrate your Visual Studio Tools for Office add-in to an Office web add-in |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +Visual Studio Tools for Office (VSTO) add-ins can only run in Office on Windows. By migrating your code to an Office web add-in, you can expand the reach of your add-in to mobile and online platforms. Migrating the code will involve rewriting it in the JavaScript language, and also updating it to use the Office JavaScript APIs. However, you can reduce migration cost by creating a shared library and keeping some of your existing add-in code in its current language. |
| 22 | + |
| 23 | +This sample includes a **/start** folder that accompanies the [Tutorial: Migrate your VSTO Add-in to an Office web add-in with a shared code library](https://docs.microsoft.com/office/dev/add-ins/tutorials/migrate-vsto-to-office-add-in-shared-code-library-tutorial). If you want to learn how to go through the steps for creating a shared library for an existing VSTO add-in, follow the tutorial. |
| 24 | + |
| 25 | +The sample contains a **/completed** folder that contains the completed solution. If you want to explore each section of the completed solution, follow the steps in this readme to learn more. |
| 26 | + |
| 27 | +The sample starts with an Excel VSTO add-in that provides functionality for the user to analyze a cell of text. They can view the unicode values of the text characters. The logic to convert the text to unicode is what will be shared to the web add-in. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +## Applies to |
| 32 | + |
| 33 | +- Excel, Outlook, Word, and PowerPoint, on Windows, Mac, and in a browser. |
| 34 | + |
| 35 | +## Prerequisites |
| 36 | + |
| 37 | +To set up your development environment: |
| 38 | + |
| 39 | +1. Install [Visual Studio 2019](https://visualstudio.microsoft.com/downloads/). |
| 40 | +2. Install the following workloads: |
| 41 | + - ASP.NET and web development |
| 42 | + - .NET Core cross-platform development. **Note:** You need at least .NET Core version 2.2 or later to run the completed sample. |
| 43 | + - Office/SharePoint development |
| 44 | + - Visual Studio Tools for Office (VSTO). **Note:** This is an **Individual** component. |
| 45 | + |
| 46 | +You will also need an Office 365 account. You can join the [Office 365 Developer Program](https://aka.ms/devprogramsignup) that includes a free 1 year subscription to Office 365. |
| 47 | + |
| 48 | +## Solution |
| 49 | + |
| 50 | +Solution | Author(s) |
| 51 | +---------|---------- |
| 52 | +Shared library VSTO migration | David Chesnut (**Microsoft**) |
| 53 | + |
| 54 | +## Version history |
| 55 | + |
| 56 | +Version | Date | Comments |
| 57 | +---------| -----| -------- |
| 58 | +1.0 | December 2, 2019 | Initial release |
| 59 | + |
| 60 | +## Disclaimer |
| 61 | + |
| 62 | +**THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.** |
| 63 | + |
| 64 | +---------- |
| 65 | +## Analyze your VSTO add-in |
| 66 | + |
| 67 | +The first step to migration is to analyze the code in your VSTO add-in. You'll want to identify each section of code as following into one of the following three categories. |
| 68 | + |
| 69 | +1. UI code that interacts with the user, such as task pane code that uses a WinForm. |
| 70 | +2. Document code that interacts with the document through the Office APIs, such as updating Excel range values. |
| 71 | +3. Business logic, such as algorithms or helper functions, that can work independently from the UI or document. |
| 72 | + |
| 73 | +UI code and document interaction code will need to be rewritten for the Office web add-in. However, business logic code can be shared between the VSTO add-in and Office web add-in. |
| 74 | + |
| 75 | +## Prepare the solution to run |
| 76 | + |
| 77 | +You'll need to install the required NuGet packages, and add test certificates for the solution to build and run correctly. |
| 78 | + |
| 79 | +1. Start Visual Studio 2019 and open the **/completed/Cell-Analyzer.sln** solution. |
| 80 | +2. In **Solution Explorer**, right-click **Solution 'Cell-Analyzer'**, and choose **Restore NuGet Packages**. |
| 81 | +3. In **Solution Explorer**, right-click the **Cell-Analyzer** project, and choose **Properties**. |
| 82 | +4. Choose the **Signing** category in the properties. |
| 83 | +5. Choose **Sign the ClickOnce manifests**, and then chose **Create Test Certificate**. |
| 84 | +6. In the **Create Test Certificate** dialog, enter and confirm a password. Then choose **OK**. |
| 85 | + |
| 86 | +## Part 1: Share code in a class library |
| 87 | + |
| 88 | +To share the business logic code, you create a class library. Then refactor the business logic and relocate it into the shared library. The advantage is that you will not need to rewrite the code in JavaScript for the Office web add-in. Later the Office web add-in will access the shared library through REST API calls. |
| 89 | + |
| 90 | +### Explore the shared class library |
| 91 | + |
| 92 | +1. Start Visual Studio 2019 and open the **/completed/Cell-Analyzer.sln** solution. |
| 93 | +2. Expand the **CellAnalyzerSharedLibrary** project in the solution explorer and open the **CellOperations.cs** file. |
| 94 | + |
| 95 | +The method **GetUnicodeFromText** is the shared function between both the VSTO add-in and Office web add-in. |
| 96 | + |
| 97 | +### Run the VSTO add-in with the shared class library |
| 98 | + |
| 99 | +1. Start Visual Studio 2019 and open the **/completed/Cell-Analyzer.sln** solution. |
| 100 | +2. In **Solution Explorer** right-click the **Cell-Analyzer** project and choose **Set as Startup Project**. |
| 101 | +3. From the **Debug** menu, choose **Start Debugging**. |
| 102 | + |
| 103 | +> **Note:** In the completed folder, the Cell Analyzer add-in is already configured to use the shared class library. |
| 104 | +
|
| 105 | +## Part 2: Create a REST API wrapper for shared code |
| 106 | + |
| 107 | +The Office web add-in will need to access the shared library. To do this we'll provide a REST API that it can access. We can do this by creating an ASP.NET CORE application that references the class library. |
| 108 | + |
| 109 | +### Explore the REST API project |
| 110 | + |
| 111 | +1. Start Visual Studio 2019 and open the **/completed/Cell-Analyzer.sln** solution. |
| 112 | +2. Expand the **CellAnalyzerRESTAPI** project in the solution explorer and open the **/Controllers/AnalyzeUnicodeController.cs** file. |
| 113 | + |
| 114 | +The method **AnalyzeUnicode** is a wrapper that calls into the shared class library to perform the actual operation. |
| 115 | + |
| 116 | +### Run the REST API project |
| 117 | + |
| 118 | +1. Start Visual Studio 2019 and open the **/completed/Cell-Analyzer.sln** solution. |
| 119 | +2. In **Solution Explorer** right-click the **CellAnalyzerRESTAPI** project and choose **Set as Startup Project**. |
| 120 | +3. From the **Debug** menu, choose **Start Debugging**. |
| 121 | +4. In the browser that opens, enter the following URL: **https://localhost:44323/api/analyzeunicode?value=test** |
| 122 | + |
| 123 | +You should see the list of characters returned along with each unicode value. |
| 124 | + |
| 125 | +## Part 3: The Office web add-in |
| 126 | + |
| 127 | +The Office web add-in must be created and then modified to match the VSTO add-in UI and document interactions as much as possible. For UI you will need to create similar controls using HTML, CSS, or your favorite library. You will also need to rewrite code that interacts with the document to use the Office JavaScript API. |
| 128 | + |
| 129 | +### Explore the Office web add-in project |
| 130 | + |
| 131 | +1. Start Visual Studio 2019 and open the **/completed/Cell-Analyzer.sln** solution. |
| 132 | +2. Expand the **CellAnalyzerRESTAPI** project in the solution explorer and open the **/Controllers/AnalyzeUnicodeController.cs** file. |
| 133 | + |
| 134 | +The method **AnalyzeUnicode** is a wrapper that calls into the shared class library to perform the actual operation. |
| 135 | + |
| 136 | +### Run the Office web add-in project |
| 137 | + |
| 138 | +1. Start Visual Studio 2019 and open the **/completed/Cell-Analyzer.sln** solution. |
| 139 | +2. In **Solution Explorer** right-click the top node **Solution 'Cell-Analyzer'** and choose **Set Startup Projects**. |
| 140 | +3. In the **Solution 'Cell-Analyzer' Property Pages** dialog, select **Multiple startup projects**. |
| 141 | +4. Enable the Start action for each of the following projects. |
| 142 | + - CellAnalyzerRESTAPI |
| 143 | + - CellAnalyzerWebAddinWeb |
| 144 | + - CellAnalyzerWebAddin |
| 145 | +5. Choose **OK**. |
| 146 | +6. From the **Debug** menu, choose **Start Debugging**. |
| 147 | + |
| 148 | +Excel will run and sideload the Office web add-in. |
| 149 | + |
| 150 | +## Security notes |
| 151 | + |
| 152 | +### Cell-Analyzer notes |
| 153 | + |
| 154 | +This readme file has you create test certificates to run the Cell-Analyzer project. These are for testing and development purposes only. Do not reuse them in your own code solutions or in production environments. |
| 155 | + |
| 156 | +### CellAnalyzerRESTAPI notes |
| 157 | + |
| 158 | +In the **Startup.cs** file, the **ConfigureServices** method updates the CORS policy to allow port **localhost:44368**. This is for development purposes. In production code you should update this to use the appropriate allowed domains. |
| 159 | + |
| 160 | +### CellAnalyzerRESTAPI notes |
| 161 | + |
| 162 | +The Cell analyzer REST API project is configured to support CORS requests from https://localhost:44368 (the Office web add-in.) This is only for development purposes. In production code, you should configure CORS to support your deployed Office web add-in endpoint. |
| 163 | + |
| 164 | +## Questions and comments |
| 165 | + |
| 166 | +We'd love to get your feedback about this sample. You can send your feedback to us in the Issues section of this repository. Questions about developing Office Add-ins should be posted to Stack Overflow. Ensure your questions are tagged with [office-js]. |
| 167 | + |
| 168 | +## Additional resources |
| 169 | + |
| 170 | +- [Office Add-ins documentation](https://docs.microsoft.com/office/dev/add-ins/overview/office-add-ins) |
| 171 | + |
| 172 | +## Copyright |
| 173 | + |
| 174 | +Copyright (c) 2020 Microsoft Corporation. All rights reserved. |
| 175 | + |
| 176 | +This project has adopted the [Microsoft Open Source Code of Conduct ](https://opensource.microsoft.com/codeofconduct/). For more information, see the [Code of Conduct FAQ ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments. |
| 177 | + |
| 178 | +<img src="https://telemetry.sharepointpnp.com/pnp-officeaddins/samples/vsto-shared-library-excel" /> |
0 commit comments