mui-spfx-controls
Version:
SPFx component library built with MUI
107 lines • 5.59 kB
JavaScript
import { __assign, __awaiter, __generator } from "tslib";
import { PrincipalSource, PrincipalType } from '@pnp/sp';
import { getSp } from '../config/pnp.config';
import { generateImageUrl } from '../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 = 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 __awaiter(this, void 0, void 0, function () {
var response;
var _this = this;
return __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 : PrincipalSource.All,
PrincipalType: principalType !== null && principalType !== void 0 ? principalType : 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 = generateImageUrl(context, value.Description);
}
else if (value.EntityData.Email && value.EntityData.Email !== '') {
image = generateImageUrl(context, value.EntityData.Email);
}
return __assign(__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 __awaiter(this, void 0, void 0, function () {
var resolvedUser, image;
return __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: PrincipalSource.All,
PrincipalType: PrincipalType.All,
QueryString: query,
})];
case 1:
resolvedUser = _a.sent();
image = '';
if (resolvedUser.Description && resolvedUser.Description !== '') {
image = generateImageUrl(context, resolvedUser.Description);
}
else if (resolvedUser.EntityData.Email &&
resolvedUser.EntityData.Email !== '') {
image = generateImageUrl(context, resolvedUser.EntityData.Email);
}
return [2 /*return*/, __assign(__assign({}, resolvedUser), { Image: image })];
}
});
});
};
return PeopleService;
}());
export { PeopleService };
//# sourceMappingURL=PeopleService.js.map