Home » Adding Scripts to a SharePoint Web Part

Adding Scripts to a SharePoint Web Part

There’s two things here. The first is how to include the scripts in your solution and the second is how to reference the scripts in your code.

How to include your script in the solution

Option 1: Add Microsoft.PowerBI.Javascript

Right click on your references folder and choose “Manage nuget” packages. Add Microsoft.PowerBI.Javascript assembly. This conveniently creates a “scripts” folder with powerbi.js files inside of i. But it does not automatically add these scripts as a feature. So you will need to manually do this.

Option 2: Add a Module to your solution

To add Javascript to a SharePoint web part, I add a Module item (add item > Module), delete the Sample.txt file, and using the ClientScriptManager to ensure that the files are placed (a) toward the end of the form element or (b) at the beginning of the <form> element. I think this approach is slightly better than adding Microsoft.PowerBI.Javascript.

How to reference the scripts in your code

There are three approaches I take here:

  1. SharePoint Script Link:
  2. Using ClientScriptManager (see this post)
    1. RegisterStartupScript: Places something like this <script type=’text/javascript’ src=’path/to/script.js’></script> before the close </form> tag:

    2. RegisterClientScriptBlock: Places something like this <script type=’text/javascript’ src=’path/to/script.js’></script> after the open <form> tag and below the view state information:
    3. A simple <script> tag:

Leave a Reply

Your email address will not be published. Required fields are marked *

*