Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting – HostForLIFE.eu :: Spfx Crud Operations with Dynamic Controls (PNPJS)
Open a command prompt. Create a directory for the SPFx solution.
md spfx-pnp-Crud
Navigate to the above-created directory.
cd spfx-pnp-Crud
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit Enter to have default name (spfx-pnp-DropDown 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 deployed instantly to all sites and will 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: SpfxDynamicCOntrols
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
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 the 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,
On the command prompt, run the below command.
1 2 3 |
npm i @pnp/logging @pnp/common @pnp/odata @pnp/sp --save for Pollyfills npm install --save @pnp/polyfill-ie11 |
in SpfxDynamicCOntrolsWebPart.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import "@pnp/polyfill-ie11"; import { sp, Web } from '@pnp/sp'; export interface ISpfxDynamicCOntrolsWebPartProps { description: string; } export default class SpfxDynamicCOntrolsWebPart extends BaseClientSideWebPart<ISpfxDynamicCOntrolsWebPartProps> { protected onInit(): Promise<void> { return new Promise<void>((resolve: () => void, reject: (error?: any) => void): void => { sp.setup({ sp: { headers: { "Accept": "application/json; odata=nometadata" } } }); resolve(); }); } public render(): void { const element: React.ReactElement<ISpfxDynamicCOntrolsProps > = React.createElement( SpfxDynamicCOntrols, { description: this.properties.description, 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 getPropertyPaneConfiguration(): IPropertyPaneConfiguration { return { pages: [ { header: { description: strings.PropertyPaneDescription }, groups: [ { groupName: strings.BasicGroupName, groupFields: [ PropertyPaneTextField('description', { label: strings.DescriptionFieldLabel }) ] } ] } ] }; } } |
in SpfxDynamicCOntrols.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
import { ISpfxDynamicCOntrolsState } from './ISpfxDynamicCOntrolsState'; import * as $ from 'jquery'; require('./Styles/capstatus.css'); let ProjectMapId:any; var search = window.location.search; var params = new URLSearchParams(search); var MYProjectname = params.get('ProjectName'); var managerlistid: number; import { sp, Web } from '@pnp/sp'; export default class SpfxDynamicCOntrols extends React.Component<ISpfxDynamicCOntrolsProps, ISpfxDynamicCOntrolsState> { constructor(props: ISpfxDynamicCOntrolsProps, state: ISpfxDynamicCOntrolsState) { super(props); this.state = { Projectstatus: [{ mymilestone: "", myscore: "", id: "",Forcastedate:"",Actualdate:"",TgtResDate:"" }] }; this.fetchdatas = this.fetchdatas.bind(this); this.AddProject = this.AddProject.bind(this); this.updateproject = this.updateproject.bind(this); this.AddProgram = this.AddProgram.bind(this); this.UpdateProgram = this.UpdateProgram.bind(this); if (MYProjectname) this.fetchdatas(); } private mybutton() { if (!MYProjectname) { return <button onClick={this.AddProgram}> Add Project </button>; } else { return <button onClick={() => this.UpdateProgram()}> Update Project </button>; } } private createUI() { let widthstyle = { width:"100%" }; let heightstyle = { width:"100%" }; return this.state.Projectstatus.map((el, i) => ( <tr key={i}> <td><input type="text" style={widthstyle} mycustomattribute={el.id || ''} placeholder="MileStone" name="mymilestone" value={el.mymilestone || ''} onChange={this.handleChange.bind(this, i)}/></td> <td><input type="text" style={widthstyle} mycustomattribute={el.id || ''} placeholder="Percentage" name="myscore" value={el.myscore || ''} onChange={this.handleChange.bind(this, i)}/></td> <td><input type="text" style={widthstyle} mycustomattribute={el.id || ''} placeholder="Forcastedate" name="Forcastedate" value={el.Forcastedate || ''} onChange={this.handleChange.bind(this, i)}/></td> <td><input type="text" style={widthstyle} mycustomattribute={el.id || ''} placeholder="Actualdate" name="Actualdate" value={el.Actualdate || ''} onChange={this.handleChange.bind(this, i)}/></td> <td><input type="text" style={widthstyle} mycustomattribute={el.id || ''} placeholder="TgtResDate" name="TgtResDate" value={el.TgtResDate || ''} onChange={this.handleChange.bind(this, i)}/></td> <td><span><a href="" mycustomattribute={el.id || ''} onClick={(e) => {this.removeClick(e,this, i, el.id)}}>-</a></span></td> </tr> )); } private handleChange(i, e) { const { name, value } = e.target; let Projectstatus = [...this.state.Projectstatus]; Projectstatus[i] = { ...Projectstatus[i], [name]: value }; this.setState({ Projectstatus }); } private addClick() { this.setState(prevState => ({ Projectstatus: [...prevState.Projectstatus, { mymilestone: "", myscore: "", id: "",Forcastedate:"",Actualdate:"",TgtResDate:"" }] })); } private removeClick(event,mythis,i, delid) { event.preventDefault(); // var delid=Number($(this).attr("ProjectStatus")); if (MYProjectname) { let list = sp.web.lists.getByTitle("ProjectStatus"); list.items.getById(delid).delete().then(_ => { console.log("Deleted"); }); } let Projectstatus = [...this.state.Projectstatus]; Projectstatus.splice(i, 1); this.setState({ Projectstatus }); } private async AddProject(myid:number) { const web = new Web(this.props.context.pageContext.web.absoluteUrl); const batch = web.createBatch(); const list = web.lists.getByTitle("ProjectStatus"); const entityTypeFullName = await list.getListItemEntityTypeFullName(); for (let k = 0; k < this.state.Projectstatus.length; k++) { list.items.inBatch(batch).add({ ProjectNameId: myid, MileStone:this.state.Projectstatus[k].mymilestone, PercentageComplete: this.state.Projectstatus[k].myscore, ForcastDate: this.state.Projectstatus[k].Forcastedate, ActualDate: this.state.Projectstatus[k].Actualdate, TargetResolutionDate: this.state.Projectstatus[k].TgtResDate }, entityTypeFullName).then(b => { }); } batch.execute().then(() => { }); } private async updateproject() { const web = new Web(this.props.context.pageContext.web.absoluteUrl); const batch = web.createBatch(); const list = web.lists.getByTitle("ProjectStatus"); const entityTypeFullName = await list.getListItemEntityTypeFullName(); for (let k = 0; k < this.state.Projectstatus.length; k++) { if (!this.state.Projectstatus[k].id) { list.items.inBatch(batch).add({ ProjectNameId: managerlistid, MileStone: this.state.Projectstatus[k].mymilestone, PercentageComplete:this.state.Projectstatus[k].myscore, ForcastDate: this.state.Projectstatus[k].Forcastedate, ActualDate: this.state.Projectstatus[k].Actualdate, TargetResolutionDate: this.state.Projectstatus[k].TgtResDate }, entityTypeFullName).then(b => { // console.log(b); }); } else { list.items.inBatch(batch).getById(this.state.Projectstatus[k].id).update({ ProjectNameId:managerlistid, MileStone: this.state.Projectstatus[k].mymilestone, PercentageComplete: this.state.Projectstatus[k].myscore, ForcastDate: this.state.Projectstatus[k].Forcastedate, ActualDate: this.state.Projectstatus[k].Actualdate, TargetResolutionDate: this.state.Projectstatus[k].TgtResDate }).then(b => { // console.log(b); }); } } batch.execute().then(() => console.log("All done!")); // event.preventDefault(); } private fetchdatas() { var reg1 = new RegExp('<div class=\"ExternalClass[0-9A-F]+\">', ""); var reg2 = new RegExp('</div>$', ""); const web = new Web(this.props.context.pageContext.web.absoluteUrl); //const batch1 = web.createBatch(); const list1 = web.lists.getByTitle("Program"); list1.items.select('Id,ProgramName').filter("ProgramName eq '" + MYProjectname + "'").get().then(r => { managerlistid = r[0].Id; $("#ProjectName").val(r[0].ProgramName); }); const list2 = web.lists.getByTitle("ProjectStatus"); let FetchProjectDetails = []; list2.items.select('Id,MileStone,PercentageComplete,ForcastDate,ActualDate,TargetResolutionDate').filter("ProjectName/ProgramName eq '" + MYProjectname + "'").top(5000).get().then(r => { for (let i = 0; i < r.length; i++) { FetchProjectDetails.push({ mymilestone: r[i].MileStone, myscore: r[i].PercentageComplete, id: r[i].Id, Forcastedate:r[i].ForcastDate, Actualdate:r[i].ActualDate, TgtResDate:r[i].TargetResolutionDate }); } this.setState({ Projectstatus: FetchProjectDetails }); }); } public render(): React.ReactElement<ISpfxDynamicCOntrolsProps> { return ( <div className="wrapper"> <div className="container"> <div className="program_list"> <div> <div><label>Program Name:</label></div> <div><input className="input" type="text" id="ProjectName" /></div> </div> </div> <div className="milestone"> <table> <tbody> <tr> <th>Milestone Title</th> <th>%Complete</th> <th>Forecast Date</th> <th>Actual Date</th> <th>Target Resolution Date</th> </tr> <a href="#" id="Addbutton" onClick={this.addClick.bind(this)}>+</a><br></br> {this.createUI()} </tbody> </table> </div> {this.mybutton()} </div> </div> ); } private AddProgram(): void{ let projectname = $("#ProjectName").val(); var AddData ={ "ProgramName": projectname, }; sp.web.lists.getByTitle("Program").items.add(AddData).then(i => { this.AddProject(i.data.Id); }); } private UpdateProgram(): void { let projectname = $("#ProjectName").val(); var UpdateData ={ "ProgramName": projectname }; sp.web.lists.getByTitle("Program").items.getById(managerlistid).update(UpdateData).then(i => { this.updateproject(); }); } } |
in ISpfxDynamicCOntrolsProps.ts
1 2 3 4 5 |
import { WebPartContext } from '@microsoft/sp-webpart-base'; export interface ISpfxDynamicCOntrolsProps { description: string; context: WebPartContext; } |
in ISpfxDynamicCOntrolsState.ts
1 2 3 |
export interface ISpfxDynamicCOntrolsState { Projectstatus?:any[]; } |
Here I am using 2 lists, namely Program and ProjectStatus.
Program list contains ProgramName field.
ProjectStatus list contains MileStone,PercentageComplete,ForcastDate,ActualDate,TargetResolutionDate fields.
ynamic Fields are used in the second list.
Print article | This entry was posted by Peter on October 31, 2019 at 8:18 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |