mui-spfx-controls
Version:
SPFx component library built with MUI
110 lines • 5.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PeopleService = void 0;
var tslib_1 = require("tslib");
var sp_1 = require("@pnp/sp");
var pnp_config_1 = require("../config/pnp.config");
var generateImageUrl_1 = require("../utils/generateImageUrl");
/**
* Service class for interacting with SharePoint People Picker.
*/
var PeopleService = /** @class */ (function () {
/**
* Initializes the PeopleService instance.
* @param {WebPartContext} context - The SharePoint WebPart context.
*/
function PeopleService(context) {
this.sp = (0, pnp_config_1.getSp)(context);
}
/**
* Restricts unvalidated email addresses from appearing in search results.
* @param {IBasePeoplePickerEntity} user - The user entity to validate.
* @returns {boolean} Returns true if the user is validated, otherwise false.
*/
PeopleService.prototype.restrictUnInvalidated = function (user) {
if (user.EntityData.PrincipalType) {
return user.EntityData.PrincipalType !== 'UNVALIDATED_EMAIL_ADDRESS';
}
return true;
};
/**
* Searches for users in SharePoint based on a query string.
* @param {WebPartContext} context - The SharePoint WebPart context.
* @param {string} query - The search query string.
* @param {number} [maximumSuggestions=25] - The maximum number of suggestions to return.
* @param {PrincipalSource} [principalSource=PrincipalSource.All] - The source from which to retrieve users.
* @param {PrincipalType} [principalType=PrincipalType.All] - The type of principals (users, groups, etc.) to include in the search.
* @returns {Promise<IPeoplePickerEntity[]>} A list of matched people entities.
*/
PeopleService.prototype.searchUser = function (context, query, maximumSuggestions, principalSource, principalType) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var response;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.sp.profiles.clientPeoplePickerSearchUser({
AllowEmailAddresses: true,
AllowMultipleEntities: false,
AllUrlZones: false,
MaximumEntitySuggestions: maximumSuggestions !== null && maximumSuggestions !== void 0 ? maximumSuggestions : 25,
PrincipalSource: principalSource !== null && principalSource !== void 0 ? principalSource : sp_1.PrincipalSource.All,
PrincipalType: principalType !== null && principalType !== void 0 ? principalType : sp_1.PrincipalType.All,
QueryString: query,
})];
case 1:
response = _a.sent();
return [2 /*return*/, response
.filter(function (value) { return _this.restrictUnInvalidated(value); })
.map(function (value) {
var image = '';
if (value.Description && value.Description !== '') {
image = (0, generateImageUrl_1.generateImageUrl)(context, value.Description);
}
else if (value.EntityData.Email && value.EntityData.Email !== '') {
image = (0, generateImageUrl_1.generateImageUrl)(context, value.EntityData.Email);
}
return tslib_1.__assign(tslib_1.__assign({}, value), { Image: image });
})];
}
});
});
};
/**
* Resolves a single user based on a query string.
* @param {WebPartContext} context - The SharePoint WebPart context.
* @param {string} query - The user query string.
* @returns {Promise<IPeoplePickerEntity>} The resolved user entity.
*/
PeopleService.prototype.resolveUser = function (context, query) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var resolvedUser, image;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.sp.profiles.clientPeoplePickerResolveUser({
AllowEmailAddresses: true,
AllowMultipleEntities: false,
AllUrlZones: false,
MaximumEntitySuggestions: 25,
PrincipalSource: sp_1.PrincipalSource.All,
PrincipalType: sp_1.PrincipalType.All,
QueryString: query,
})];
case 1:
resolvedUser = _a.sent();
image = '';
if (resolvedUser.Description && resolvedUser.Description !== '') {
image = (0, generateImageUrl_1.generateImageUrl)(context, resolvedUser.Description);
}
else if (resolvedUser.EntityData.Email &&
resolvedUser.EntityData.Email !== '') {
image = (0, generateImageUrl_1.generateImageUrl)(context, resolvedUser.EntityData.Email);
}
return [2 /*return*/, tslib_1.__assign(tslib_1.__assign({}, resolvedUser), { Image: image })];
}
});
});
};
return PeopleService;
}());
exports.PeopleService = PeopleService;
//# sourceMappingURL=PeopleService.js.map