Created
May 31, 2020 17:36
-
-
Save johnnycardy/26e2e680823a632da20ed8b8d97a628f to your computer and use it in GitHub Desktop.
Code for creating test list items for demos etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private createListItem() : void { | |
this.context.spHttpClient.post(`https://TENANT.sharepoint.com/_api/web/lists/GetByTitle('Opportunities')?$select=ListItemEntityTypeFullName`, SPHttpClient.configurations.v1, { | |
method: 'get', | |
headers: new Headers({ | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
}) | |
}).then(result => { | |
result.json().then(type => { | |
function randomInteger(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
let regions = ["North America", "South America", "APAC", "EMEA"]; | |
//Half-assed way to get a realistic distribution | |
let stages = ["Lead", "Lead", "Lead", "Lead", "Lead", "Lead", "Lead", "Lead", | |
"Qualify", "Qualify", "Qualify", "Qualify", "Qualify", | |
"Solution","Solution","Solution", | |
"Proposal", "Proposal", | |
"Finalize"]; | |
function getOppSize(revenue:number) : string { | |
if(revenue < 450000) { | |
return "Small"; | |
} else if(revenue > 800000) { | |
return "Large"; | |
} else { | |
return "Medium"; | |
} | |
} | |
//Generate some dummy data | |
var region = regions[randomInteger(0, regions.length-1)]; | |
let revenue = randomInteger(10000, 1000000); | |
let partnerDriven = Math.random() > 0.65; | |
let salesStage = stages[randomInteger(0, stages.length-1)]; | |
let opportunitySize = getOppSize(revenue); | |
let body:string = JSON.stringify({ | |
Title: "Opportunity " + randomInteger(1, 10000000), | |
Region: region, | |
EstimatedDealSize: revenue, | |
PartnerDriven: partnerDriven, | |
SalesStage: salesStage, | |
OpportunitySize: opportunitySize | |
}); | |
this.context.spHttpClient.fetch(`https://TENANT.sharepoint.com/_api/web/lists/GetByTitle('Opportunities')/items`, SPHttpClient.configurations.v1, { | |
method: 'post', | |
headers: new Headers({ | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
'Content-Length': body.length.toString() | |
}), | |
body: body | |
}).then(result => { | |
result.json().then(json => { | |
this.createListItem(); | |
}); | |
}); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment