Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Quick helper script to get all job titles for users on a given site. This is not possible from the User Profile or Search (due to the base properties it returns in search results).
/*
The following JS example gets all Job Titles from the Site User Table and stores them in an array (collection)
Due to it's dependancies on map, filter, sort etc.. you will need to add the relevant shim to get this to work on IE9<
*/
var collection, id;
var siteBaseUrl = '<SHAREPOINT BASE URL GOES HERE>';
function processPayLoad(data) {
return data.value.map(function(item, i) {
if (i === (data.value.length -1)) {
id = item.ID;
}
return item.JobTitle;
//sort the array of Job Titles ready for duplicate removal
}).sort().filter(function(item, pos, ary) {
//remove all duplicates from payload
return !pos || item != ary[pos - 1];
});
}
function getAllJobTitles(siteUrl, startId) {
//we need to process the list 5000 items at a time.
var token = encodeURIComponent('Paged=TRUE&p_SortBehavior=0' + (typeof startId === 'undefined' ? '' : '&p_ID=' + startId) + '&$top=5000');
var url = siteUrl + '/_api/web/SiteUserInfoList/items?$select=JobTitle,ID&$filter=JobTitle%20ne%20null&$top=5000&$skiptoken=' + token;
id = -1;
jQuery.ajax({
url: url,
dataType: 'json',
type: 'GET',
headers: {
"Accept": "application/json; charset=utf-8",
"X-RequestDigest": document.getElementById('__REQUESTDIGEST').value
},
success: function(data) {
//insert if it's the first time
if (typeof collection === 'undefined') {
collection = processPayLoad(data);
} else {
//concatenate results if not
collection.concat(processPayLoad(data));
}
if (id !== -1) {
getAllJobTitles(siteBaseUrl, id);
} else {
console.log('JobTitle Gatherer - Process has finished');
}
},
fail: function(xhr, status, err) {
console.log(status, err.toString());
}
});
}
getAllJobTitles(siteBaseUrl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment