This is the first of five posts giving an overview of creating modern webpart solutions for SharePoint 2013+ environments. The webpart library is part of the gd-sprest SharePoint REST Framework. The demo code can be found in github.
Developing for SharePoint 2013 environments can be difficult, given the new modern web stack. This post will give step through various ways of creating client-side solutions in SharePoint 2013 using the modern web stack.
The original code for this library came from the gd-sprest-react library. I wanted to make sure the solution was framework independent, so you can reference this from any framework you choose to work with. My goal in this post is to give examples of developing webparts using the following frameworks:
2) TypeScript – Example of using TypeScript to create a demo webpart
3) React – Example of using the gd-sprest-react library
4) VueJS – Example of using VueJS to create a demo webpart
5) AngularJS – Example of using AngularJS to create a demo webpart
I have an article going over the high-level details of this approach. I recommend referring to this post for a high level understanding of the solution. The solution utilizes the ScriptEditor webpart where a defined template is used:
- source url Configuration Element – Optional element for storing custom configuration.
- here Target Element – The target element to render the component to.
- Render Events – Display, Edit and Post render events exist, passing the webpart information.
The library will determine the webpart id, based on the parent elements. Having this information allows for multiple instances of the same webpart object to exist on the same page.
- gd-sprest-js.js (816 KB)
- gd-sprest-js.min.js (385 KB)
- gd-sprest-fabric.js (1.06 MB)
- gd-sprest-fabric.min.js (627 KB)
The gd-sprest-fabric library includes the office fabric css files, so you don’t need to reference them. If you use the other file, then you must reference the Office Fabric-UI Core 5.x library. For this example, we will reference the gd-sprest-fabric.min.js file directly from github.
Step 1 – Upload Assets (Optional)
This step is only required if your environment doesn’t have access to the internet, of if you prefer to reference a file from within SharePoint.
1) Download the library
2) Upload the file to SharePoint
Step 2 – Create a Demo Page
This demo will be tested in SharePoint Online, so make sure to create a classic webpart page.
1) Create a webpart page of your choice
2) Edit the page
3) Add a script part editor
4) Edit the “Snippet” and add the following:
The gd-sprest-js library has various webpart templates available. This example will use the list webpart template to allow the list to be configured by the user. Below are the webpart properties we will use in this example.
* odataQuery – (Optional) Setting the ODATA query to order by the “Title” field
* cfgElementId – The id of the configuration element.
* elementId – The id of the webpart element to render to.
* onRenderItems – The render list items event. We will use the fabric “List” component to render the item “Title” and “ID” properties.
If you are not referencing the file from github, update the src location to the file in step 1.
The gd-sprest libraries are available through the $REST global variable.
It’s optional to use the SP.SOD.executeOrDelayUntilScriptLoaded method, but wanted to demonstrate that it’s available.
5) Save the page, so the code and html is saved.
Step 3 – Configure the WebPart
The last step is to configure the webpart. Since we are using the list webpart template, there is no need to write code to reference the list in the current or other webs.
1) Edit the page and you will see a configuration button
2) Clicking on the configuration button will display a panel to configure the webpart.
3) For this example, I’ll use the “Dev” list. Click on the list dropdown and click on “Save”.
Optional – You can set the relative web url to another site, and select a list from a different web. Click the “Refresh” button to reload the lists dropdown.
4) After the page saves the webpart data, stop editing and view the list data.