October 28, 2021

How to get list of web parts from Modern SharePoint Page?

Introduction:

In this blog, we will learn how can we get web parts added to the Modern Sitepage.
On the classic page, we can get a list of all web parts using "?contents=1" query string, but it will not work for modern web parts. For the modern web part, we need to use the PowerShell script.

Script:

Step 1: First we will read the URL of the site of which page we want to get the web parts.
 $siteURL = Read-Host "Provide site url";  
  • Here we are taking user input for site URL, we can also give static Site URL.

Step 2: Now we will connect to the site using the Connect-PnPOnline command. 
 Connect-PnPOnline -Url $siteURL  
  • This command will ask for credentials.

Step 3: Now we will get the page using "Get-PnPClientSidePage" command.
 $page = Get-PnPClientSidePage -Identity 'Home'  
Here we are using the Home page, we can use the name of any of your page.

Step 4: In $page will have all the information for the Home page. So now we will get all web parts available on this page as below:
 $webParts = $page.Controls   

Step 5: Now $webParts will have a list of all web parts available on the Home page. So we will use a loop to read each web part information:
 foreach($webpart in $webparts) {   
   Write-Host "WebPart Id "   
   $webpart.InstanceId   
   Write-Host "Title "   
   $webpart.Title   
 }  

Here we are fetching the web part ID and web part name. 

Step 6: Complete PowerShell script will be as below:
 $siteURL = Read-Host "Provide site url"   
 Connect-PnPOnline -Url $siteURL  
 $page = Get-PnPClientSidePage -Identity 'Home' #Get the page on which you are going to perform add and remove web parts.  
 $webParts = $page.Controls   
 #if there are more than one webparts   
 foreach($webpart in $webparts) {   
   Write-Host "WebPart Id "   
   $webpart.InstanceId   
   Write-Host "Title "   
   $webpart.Title   
 }  
This PowerShell will give output as below screenshot:



It will return Custom as well as OOTB web parts.

Conclusion:

This is how we can get a list of web parts. Hope this helps!

If you have any questions you can reach out our SharePoint Consulting team here.

October 21, 2021

Programming by Examples in Power Apps

Overview:

During this article, we will talk about new rolled out functionality by Microsoft this year in Power Apps Ideas. We will talk about “Programming by Examples” feature in detail. So, Let’s get started!

What do we mean by Programming by Examples?

“Programming by Examples” is a new AI Driven capability in Power Apps Ideas that automatically generate Power Fx Formula for us. This new feature is powered by PROSE (Programming by Examples and Natural Language), researched and developed by the Microsoft PROSE team. 
The same technology is already used in other Microsoft products as well, e.g., Flash fill in Excel, Intellicode suggestions in Visual Studio, and Table extraction in Power Query, etc.

Note:  At this moment, it is only available for US Region. 

Enable Preview Feature:

Go to File menu, Open Settings, click on Upcoming Features and make sure Ideas Panel is turn on!
Once we turn on the above feature, the Ideas Panel will be available for you!

  1. Please take a look at the following Gallery control which is showing account information.
  2. Now, we want to show the date as “June 3 2021”. Then how can we do that?
  3. The above example output generated the Power Fx formula automatically. Select the formula and check the result. It will show you the following result!
So, this is how we can apply “Programming By Examples” in Power Apps. 

Let’s check some more examples:

Apply PBE on Text Columns:

  • We want only First name from Full name. Then just provide your expected output and the formula will be generated automatically. 
  • Below is the end outcome!

Apply PBE on Date column by Training the example:

  • We want to display the Month name in 3 characters. So, how can we do that?
  • Select Label, click on “Train from Examples’
  • Provide sample outcome for few fields and click on “Get Ideas”.
  • This will generate Power Fx formula
  • Apply the formula and check the result.

Conclusion:

This is how Power Apps Ideas feature works in Power Apps. Isn’t that cool? Happy Power Apping!!

If you have any questions you can reach out our SharePoint Consulting team here.

September 30, 2021

SharePoint Framework - Export to PDF - Export the HTML Content to PDF File using SPFx webpart

Introduction

We recently implemented a SharePoint Framework (SPFx) webpart for a consulting firm based out of Alpharetta, Georgia, United States to display the calendar events inside the calendar cells (a custom calendar). The additional requirement was to allow users to export the HTML content of the calendar to a PDF File with all details.

So, in this blog, we will learn how to export the HTML content to a PDF file in a custom webpart implemented using SharePoint Framework (SPFx).

Overview

Below is the overview of the Calendar's HTML content.



Approach

For exporting the content to a PDF file, follow the below steps:

Step 1: Install the npm package "html2canvas" using the command "npm i html2canvas --save".

Step 2: Import the npm package as shown below.

import html2canvas from 'html2canvas';
(window as any).html2canvas = html2canvas;

Step 3: Now, implement the below code for exporting the content to a PDF file.

Below are the points that need to be kept in mind for providing the values.

1. Inside the getElementById() value, user needs to provide the main div "id" property value inside which the overall HTML is been implemented.
2. Inside the jsPDF(), if the user wants to export the content in portrait mode, provide the value as "p" or for exporting the content in landscape mode, provide the value as "l".
3. "imgWidth" and "imgHeight" property values can also be provided manually as per the requirement.

      let eventHTMLDiv = document.getElementById('EventsHTML');
      html2canvas(eventHTMLDiv)
        .then((canvas=> {
          var imgData = canvas.toDataURL('image/jpeg'1.0);

          var doc = new jsPDF('l''mm''a4');

          var imgWidth = doc.internal.pageSize.getWidth();
          var imgHeight = canvas.height * imgWidth / canvas.width;

          doc.addImage(imgData'JPEG'1010, imgWidthimgHeight);

          doc.save('September.pdf');
        });

Below is the parameters overview for the "addImage()" method.

(method) jsPDF.addImage(imageData: string | HTMLCanvasElement | HTMLImageElement | Uint8Array, format: string, x: number, y: number, w: number, h: number)

1. imageData: Need to provide the canvas converted in Data URL format.
2. format: In which format, the image should be added e.g JPEG/JPG/PNG.
3. x: How much margin should be kept from the top in the PDF file.
4. y: How much margin should be kept from the left in the PDF file.
5. w: width of the canvas.
6. h: height of the canvas.

The Exported content looks like below in the PDF file "September.pdf".



Conclusion

This is how we can export the HTML content to a PDF File using SPFx webpart.

If you have any questions you can reach out our SharePoint Consulting team here.