Introduction
Publishing bulk files is a difficult task to do manually. In this article, I have automated files to check-in and publish using batch calls (reduce the number of calls).
Steps
Open a command prompt and create a directory for the SPFx solution.
md spfx-Bulkpublishing
Navigate to the above-created directory.
cd spfx-Bulkpublishing
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit Enter for the default name (spfx-Bulkpublishing in this case) or type in any other name for your solution.
Selected choice - Hit Enter
Target for the component
Here,
we can select the target environment where we are planning to deploy
the client web part; i.e., SharePoint Online or SharePoint OnPremise
(SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest).
Place of files
We may choose to use the same folder or create a subfolder for our solution.
Selected choice - same folder.
Deployment option
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice - N (install on each site explicitly).
Permissions to access web APIs
Choose
if the components in the solution require permission to access web APIs
that are unique and not shared with other components in the tenant.
Selected choice - N (solution contains unique permissions)
Type of client-side component to create
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice - WebPart
Web part name
Hit Enter to select the default name or type in any other name.
Selected choice - Bulkpublishing
Web part description
Hit Enter to select the default description or type in any other value.
Framework to use
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice - React
The
Yeoman generator will perform a scaffolding process to generate the
solution. The scaffolding process will take a significant amount of
time.
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command:
npm shrinkwrap
In the command prompt, type the below command to open the solution in the code editor of your choice.
NPM Packages used:
In BulkpublishingWebPart.ts:
- import * as React from 'react';
- import * as ReactDom from 'react-dom';
- import { Version } from '@microsoft/sp-core-library';
- import {
- IPropertyPaneConfiguration,
- PropertyPaneTextField
- } from '@microsoft/sp-property-pane';
- import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
- import * as strings from 'BulkpublishingWebPartStrings';
- import Bulkpublishing from './components/Bulkpublishing';
- import { IBulkpublishingProps } from './components/IBulkpublishingProps';
-
- export interface IBulkpublishingWebPartProps {
- description: string;
- libraryName:string;
- }
-
- export default class BulkpublishingWebPart extends BaseClientSideWebPart <IBulkpublishingWebPartProps> {
-
- public render(): void {
- const element: React.ReactElement<IBulkpublishingProps> = React.createElement(
- Bulkpublishing,
- {
- description: this.properties.description,
- LibraryName:this.properties.libraryName,
- context:this.context
- }
- );
-
- ReactDom.render(element, this.domElement);
- }
-
- protected onDispose(): void {
- ReactDom.unmountComponentAtNode(this.domElement);
- }
-
- protected get dataVersion(): Version {
- return Version.parse('1.0');
- }
- protected texboxvalidated(value:string):string {
- if(value.length==0){
-
- return "Enter Library Name";
- }
-
- }
-
- protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
- return {
- pages: [
- {
- header: {
- description: strings.PropertyPaneDescription
- },
- groups: [
- {
- groupName: strings.BasicGroupName,
- groupFields: [
- PropertyPaneTextField('libraryName', {
- label: 'LibraryName',
- multiline:false,
- resizable:false,
- onGetErrorMessage:this.texboxvalidated,
- placeholder:"please enter list name"
- }),
- PropertyPaneTextField('description', {
- label: strings.DescriptionFieldLabel
- })
- ]
- }
- ]
- }
- ]
- };
- }
- }
In IBulkpublishingProps.ts:
- import { WebPartContext } from "@microsoft/sp-webpart-base";
-
- export interface IBulkpublishingProps {
- description: string;
- LibraryName:string;
- context:WebPartContext;
- }
In Bulkpublishing.tsx:
- import * as React from 'react';
- import { IBulkpublishingProps } from './IBulkpublishingProps';
- import "@pnp/polyfill-ie11";
- import {sp} from "@pnp/sp/presets/all";
- export default class Bulkpublishing extends React.Component<IBulkpublishingProps, {}> {
- constructor(props: IBulkpublishingProps) {
- super(props);
- sp.setup({
- spfxContext:this.props.context,
- sp: {
- headers: {
- "Accept": "application/json; odata=verbose"
- }
- },
- ie11: true
- });
- this.BulkPublishingFiles=this.BulkPublishingFiles.bind(this);
- }
-
- private BulkPublishingFiles=async(e)=>{
- e.preventDefault();
- e.stopPropagation();
- e.nativeEvent.stopImmediatePropagation();
- console.log(e);
- let library = await sp.web.lists.getByTitle(this.props.LibraryName);
- library.items.expand("Folder", "File").getAll().then((items) => {
- const batch = sp.createBatch();
- items.forEach(async item => {
- if(item["File"]){
- if(item["File"]["CheckOutType"]==0){
- await sp.web.getFileByServerRelativeUrl(item.File.ServerRelativeUrl).checkin();
- }
-
- await sp.web.getFileByServerRelativeUrl(item.File.ServerRelativeUrl).inBatch(batch).publish();
- console.log( "Processing..." + " " +item.File.ServerRelativeUrl);}
- });
- batch.execute().then(() => {
- console.log('All done!');
- }).catch(console.log);
- });
- }
- public render(): React.ReactElement<IBulkpublishingProps> {
- return (
- <div>
- <button onClick={this.BulkPublishingFiles}> Publish Files</button>
- </div>
- );
- }
- }
Expected Output:
Since I used the batch operation concept in this webpart, it reduces the number of calls to Sharepoint (throttling error 429 minimized).
Conclusion
In this article, we learned how to bulk publish files from the document library using pnpjs. I hope this helps someone. Happy coding :)