Skip to content

Instantly share code, notes, and snippets.

@vgrem
Last active January 3, 2016 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrem/8409445 to your computer and use it in GitHub Desktop.
Save vgrem/8409445 to your computer and use it in GitHub Desktop.
SharePoint List data generator
function AddListItem(listTitle,itemProps,OnItemAdded,OnItemError)
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(listTitle);
var itemCreateInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(itemCreateInfo);
for(var propName in itemProps) {
listItem.set_item(propName, itemProps[propName])
}
listItem.update();
context.load(listItem);
context.executeQueryAsync(
function() {
OnItemAdded(listItem);
},
OnItemError
);
}
function GenerateContacts()
{
var contactsCount = 108;
for(var i = 0; i < contactsCount; i++){
var contactEntry = CreateContactEntry();
AddListItem('Contacts',contactEntry,
function(contactItem){
console.log('Contact ' + contactItem.get_item('Title') + ' has been created successfully');
},
function(sender,args){
console.log('Error occured while creating contact:' + args.get_message());
});
}
}
function CreateContactEntry()
{
var contactCard = Faker.Helpers.createCard();
return {'Title': contactCard.username,
'FullName': contactCard.name,
'Email': contactCard.email,
'Company': contactCard.company.name,
'WorkPhone': contactCard.phone,
'WorkAddress': contactCard.address.streetA,
'WorkCity': contactCard.address.city,
'WorkZip': contactCard.address.zipcode,
'WorkCountry': contactCard.address.ukCountry,
'WebPage': 'http://' + contactCard.website,
'Location': 'Point(' + contactCard.address.geo.lng + ' ' + contactCard.address.geo.lat + ')'};
}
var scriptbase = _spPageContextInfo.siteAbsoluteUrl + '/_layouts/15/';
$.getScript(scriptbase + 'SP.js',
function () {
$.getScript(_spPageContextInfo.siteAbsoluteUrl + '/SiteAssets/Faker.js', GenerateContacts);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment