Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save beckettkev/1749e8e652ee7d9350cc to your computer and use it in GitHub Desktop.

Select an option

Save beckettkev/1749e8e652ee7d9350cc to your computer and use it in GitHub Desktop.
Get all x Property for Users in SharePoint
var userProfileDataCollection;
SearchPropertyFetcher = function(options) {
var id, skip;
var properties = options.properties || ['PreferredName','WorkEmail'];
var base = options.url || window.location.protocol + '//' + window.location.host;
function wash(results) {
var people = [];
var pairs;
results.forEach(function(row, index) {
pairs = { };
row.Cells.forEach(function(item, i) {
//build a new object and store the key and value as an associated pair
if (properties.indexOf(item['Key']) > -1 || item['Key'] === 'ID') {
pairs[item['Key']] = item['Value'];
}
});
people.push({
"Cells": pairs
});
});
return people;
}
function processResults(results) {
//the actual data will be in something horrible that you will want to clense
var results = wash(results.PrimaryQueryResult.RelevantResults.Table.Rows);
id = results.length === 0 ? -1 : userProfileDataCollection.values.length + results.length;
//now we should be able to get at an item like data[0].Cells[property] with none of this key lookup nonesense;
var payload = results.map(function(item, i) {
var current = {};
properties.forEach(function(property, i) {
current[property] = item.Cells[property]
});
return current;
});
return typeof payload !== 'undefined' ? payload : [];
}
var getAllPropertyForUsers = function (startId) {
userProfileDataCollection = userProfileDataCollection || {
values: []
};
if (typeof startId === 'undefined') {
//reset terms if fetching fresh data
userProfileDataCollection.values = [];
skip = '';
} else {
skip = '&startrow=' + startId;
}
var selection = '';
properties.forEach(function(property, i) {
selection += property + ',';
});
selection = selection !== '' ? selection.substring(0, selection.length -1) : '';
var url = base + "/_api/search/query?sourceid='b09a7990-05ea-4af9-81ef-edfab16c4e31'&querytext='*'&selectproperties='" + selection + "'&rowlimit=500&" + skip;
jQuery.ajax({
url: url,
dataType: 'json',
type: 'GET',
headers: {
"Accept": "application/json; charset=utf-8",
"X-RequestDigest": document.getElementById('__REQUESTDIGEST').value
},
success: function(results) {
//concatenate results if not
userProfileDataCollection.values = userProfileDataCollection.values.concat(processResults(results));
if (id !== -1) {
//more to fetch recursively fetch job titles and purge until we have them all
getAllPropertyForUsers(id);
} else if (id === -1) {
//we have all of the properties
console.log('we have all the properties in the data bag.');
}
},
fail: function(xhr, status, err) {
console.log(err.toString());
}
});
};
getAllPropertyForUsers();
};
//To make this work...
SearchPropertyFetcher({
properties: ['MANAGED PROPERTY 1', 'MANAGED PROPERTY 2', 'MANAGED PROPERTY 3'],
url: _spPageContextInfo.webAbsoluteUrl
});
//when the process has finished you should be notified. You will be able to access the properties by using something like... data.properties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment