Angular on a modern SharePoint page (part 3)
In my previous article, we have created an angular element, which reads data from our SharePoint list and displays it using bootstrap 5. You can get the final project code here. And now we are ready to move forward and create the SPFx web part to host our element on a modern SharePoint page.
The generator will complete its work and we will have 2 project folders in our directory. Type in terminal
where we can use "+" to add our SPFx web part on the page
Ok, now we are good to go with our modifications.
We are ready to package the solution. Type in terminal the following commands:
Now go to Pages -> New -> Site Page and create a new page.
Name it "Announcements" and click on "+" to add our web part.
Now our page is ready to use and our angular element is deployed to the SharePoint modern page.
Cool! We have created an SPFx project, which uses our angular element, and added it to the SharePoint modern page. You can get the final project code here.
In the next article, we will try to use our component in SharePoint online.
Stay tuned :)
Before we begin
We will use SharePoint 2019 as our hosting platform. During writing this post and creating the demo project I have some issues with the different versions of the different tools. I have found it very useful to use Node Version Manager (nvm), which helps you to use different versions of the node environment on the same computer. You can download it for the Windows OS here.
So, what we will need to create the SPFx web part:
- node - version 10.24.1
- gulp - version 3.9.1
- yo - version 3.1.0
- microsoft/generator-sharepoint - version 1.12.1 (the latest version, supporting SharePoint 2019)
Let's install all that we need on our development machine.
- To get the proper version of the node, type in terminal
nvm install 10.24.1
nvm use 10.24.1 - To get the proper version of the Gulp, type in terminal
npm install -g gulp@3.9.1 - To get the proper version of the Yeoman, type in terminal
npm install -g yo@3.1.0 - To get the proper version of the SPFx project generator, type in terminal
npm i -g @microsoft/generator-sharepoint@1.12.1
SPFx project
First, open the terminal window and go to the same level, where we have or angular elements project sharepoint-angular. Type:
yo @microsoft/sharepoint
This will start the Yeoman generator for our new project. Answer for the following questions:
What is your solution name?
spfx-announcements
spfx-announcements
Which baseline packages do you want to target for your component(s)?
SharePoint 2019 onwards, including SharePoint Online
SharePoint 2019 onwards, including SharePoint Online
Where do you want to place the files?
Create a subfolder with solution name
Create a subfolder with solution name
Do you want to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without running any feature deployment or adding apps in sites?
N
Which type of client-side component to create?
WebPart
What is your Web part name?
Announcements
What is your Web part description?
leave blank
Which framework would you like to use?
No JavaScript framework
code .
to open them in VS Code
gulp trust-dev-cert
gulp serve
If everything is correct - gulp will open the SharePoint workbench in the browser
Open the src/webparts/announcements/AnnouncementsWebPart.ts and make required modifications, so it looks like this:
import { Version } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration,
PropertyPaneTextField
} from '@microsoft/sp-webpart-base';
import { escape } from '@microsoft/sp-lodash-subset';
import styles from './AnnouncementsWebPart.module.scss';
import * as strings from 'AnnouncementsWebPartStrings';
import "../../../../sharepoint-angular/dist/sharepoint-angular/main";
import "../../../../sharepoint-angular/dist/sharepoint-angular/polyfills";
import "../../../../sharepoint-angular/dist/sharepoint-angular/runtime";
require("../../../../sharepoint-angular/dist/sharepoint-angular/scripts");
require("../../../../sharepoint-angular/dist/sharepoint-angular/styles.css");
export interface IAnnouncementsWebPartProps {
description: string;
}
export default class AnnouncementsWebPart extends BaseClientSideWebPart<IAnnouncementsWebPartProps> {
public render(): void {
window['_spPageContextInfo'] = this.context.pageContext.legacyPageContext;
this.domElement.innerHTML = `<announcements-element></announcements-element>`;
}
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyPaneTextField('description', {
label: strings.DescriptionFieldLabel
})
]
}
]
}
]
};
}
}
Now open config/package-solution.json and change the version and zipped package:
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/package-solution.schema.json",
"solution": {
"name": "spfx-announcements",
"id": "7d823249-26f8-4251-911f-c499d02a510e",
"version": "1.0.0.6",
"includeClientSideAssets": true
},
"paths": {
"zippedPackage": "solution/spfx-announcements.sppkg"
}
}
gulp clean
gulp bundle --production
gulp package-solution --production
If everything passed ok, we will receive this message:
Created file: sharepoint\solution\spfx-announcements.sppkg
So, now we have the SPFx package, which we can deploy and use on our modern page.
SharePoint Application Catalog
First, we will upload our package to our Application catalog.
NOTE: you should have administrative privileges to complete this task.
Open the application catalog and use the Upload button or just drag and drop our package file.
On the pop-up screen click on the "Deploy" button.
If everything is processed well - Deployed column should display "Yes".
SharePoint modern site
NOTE: you should create an Announcements list manually if you do not have one already.
Now let's open our modern website. Select Tools -> Site contents.
Select New -> App
Select "From Your Organization", then select our new web part and enable it
Press on the "Publish" button to publish the page.
Cool! We have created an SPFx project, which uses our angular element, and added it to the SharePoint modern page. You can get the final project code here.
In the next article, we will try to use our component in SharePoint online.
Stay tuned :)
Comments
Post a Comment