Hello SharePoint enthusiasts! 😊
In this article, I will show you how to use Angular on a regular SharePoint page without using the SharePoint framework. 🚀
Is it possible to use Angular with the regular SharePoint pages (2013/2016/2019)? Yes, absolutely, with the help of Angular Elements!
Why should we use Angular Elements?
- Reusability to a Whole New Level
With Angular Elements, we can make our components truly reusable. Meaning, you can use Angular Components in other frameworks and libraries, such as React, Vue, Ember, and even SharePoint!
- Publish Parts of the Application
Angular Elements also allows you to develop and publish parts of the application independently!
Angular elements are Angular components packaged as custom elements (also called Web Components), a web standard for defining new HTML elements in a framework-agnostic way.
Ok, so to achieve this we need to create an Angular Elements project, build it and copy results to someplace in SharePoint. The ideal place for this is the SiteAssets folder, where we can store all compiled scripts and reference them later on our SharePoint page.
What we need:
Let's start!
Angular side
First of all, we have to create a new Angular project. For this, type in a terminal window:
ng new sharepoint-angular
Answer for the following questions:
Do you want to enforce stricter type checking and stricter bundle budgets in the workspace?
NWould you like to add Angular routing?
NWhich stylesheet format would you like to use?
CSS
Ok, a new angular project has been created. Now type in a terminal window:
cd sharepoint-angular
And we will add Angular elements support by typing:
ng add @angular/elements
After receiving the "Packages installed successfully" message we are good to go! Now type:
code .
VS code opens our newly created Angular project
Let's clean our project a little bit. Go to src/app and remove everything related to app.component
Now open app.module.ts and remove all references to app.component
import { HttpClientModule } from '@angular/common/http';
And add this module to the imports section
imports: [
BrowserModule,
HttpClientModule
]
So the updated app.module.ts now looks like this:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
],
imports: [
BrowserModule,
HttpClientModule
],
providers: [],
bootstrap: []
})
export class AppModule { }
Ok, now we are ready to create our new component. Type in terminal:
ng g c components/first-component
Now open src/apps/components/first-component.component.html and modify HTML
<div style="background-color:#0072c6;width:300px;height:100px;color:white;padding:10px;">
<h1 style="color:white">Hello From Angular</h1>
<div> </div>
<div>First Element is working...</div>
</div>
Great, now we need to register our angular element. For this open updated app.module.ts and add entryComponents section and ngDoBootstrap function:
import { Injector, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { FirstComponentComponent } from './components/first-component/first-component.component';
import { createCustomElement } from '@angular/elements';
@NgModule({
declarations: [
FirstComponentComponent
],
imports: [
BrowserModule,
HttpClientModule
],
entryComponents: [FirstComponentComponent],
providers: [],
bootstrap: []
})
export class AppModule {
constructor(private injector: Injector) {}
ngDoBootstrap() {
const firstElment = createCustomElement(FirstComponentComponent, {
injector: this.injector,
});
customElements.define('first-element', firstElment);
}
}
Ok, so now we are good to go. Let's build our project:
ng build --prod --output-hashing=none
After this, in dist/sharepoint-angular folder we will get 3 javascript files, which we will use on our SharePoint in the next step.
SharePoint part
Ok, now once we have everything we need, it's time to work with our SharePoint. First of all, open your SharePoint designer and connect to the desired site. Then select SiteAssets library and create a new folder: sharepoint-angular
Copy 3 files from the previous step and upload them to this folder.
Now go to the Site Pages library and create the new page "Angular.aspx".
Now right-click on it and select "Edit file in Advanced Mode".
In the <head> block add references to our script files:
<script src="../SiteAssets/sharepoint-angular/polyfills.js"></script>
<script src="../SiteAssets/sharepoint-angular/runtime.js"></script>
<script src="../SiteAssets/sharepoint-angular/main.js"></script>
And in the <body> section add our first element:
<h1>Test angular elements</h1>
<first-element></first-element>
Now, open the page in the browser and check the result:
Cool! We have proved that everything is working as expected.
In the
next article, we will try to read data from our SharePoint list and add more interactivity to our component.
Stay tuned :)
Comments
Post a Comment