Overview
Populating a SharePoint List with data is required for different purposes quite often. In this article I am going to demonstrate how to populate Contacts list with a fake data using CSOM.
Provision column to a List via SharePoint CSOM
We will utilize Contacts List for demonstration purposes. But before we proceed, let’s add a Geolocation field to a list.
Since the Geolocation column is not available by default in SharePoint lists, it’s common practice to provision this field to a list programmatically. The code below demonstrates how to add the Geolocation field by using the SharePoint client object model:
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
function ProvisionField(fieldSchema, listTitle,OnSuccess,OnError) | |
{ | |
var context = SP.ClientContext.get_current(); | |
var list = context.get_web().get_lists().getByTitle(listTitle); | |
var fields = list.get_fields(); | |
var fieldToAdd = fields.addFieldAsXml(fieldSchema, true, SP.AddFieldOptions.AddToDefaultContentType); | |
fieldToAdd.update(); | |
context.load(fieldToAdd); | |
context.executeQueryAsync(OnSuccess(fieldToAdd),OnError); | |
} | |
function AddGeolocationFieldToContactsList() | |
{ | |
ProvisionField("<Field Type='Geolocation' DisplayName='Location'/>","Contacts", | |
function(field){ | |
console.log('Geolocation field has been added'); | |
}, | |
function(sender,args){ | |
console.log('Error occured while adding Geolocation field:' + args.get_message()); | |
}) | |
} |
Generate list data
For populating Contacts list we will utilize Faker.js JavaScript library that is intended for generating massive amounts of fake data, below is provided the complete code:
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
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); | |
} | |
); |
Results
After running the script the Contacts list will be populated with a fake data as demonstrated below.
Contacts default view
Contacts map view