spws
Version:
SharePoint Web Services Wrapper
75 lines • 3.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
// SPWS Library
var __1 = require("../..");
// Classes
var classes_1 = require("../../classes");
/**
* Gets the current user's ID
* @param options The SharePoint web URL
* @param options.webURL The SharePoint web URL
* @param options.getFromWindow If false, the user ID will be scraped from the SharePoint page.
* @example
* ```
* // Get the current user ID
* const res = await getCurrentUserID();
*
* // Get the current user ID
* const res = await getCurrentUserID({ getFromWindow: false });
* ```
*/
var getCurrentUserID = function (_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.webURL, webURL = _c === void 0 ? __1.defaults.webURL : _c, _d = _b.getFromWindow, getFromWindow = _d === void 0 ? true : _d;
return new Promise(function (resolve, reject) {
var globalUserID = getFromWindow &&
((window._spPageContextInfo && window._spPageContextInfo.userId) || window._spUserId);
// If the user ID exists in window
if (globalUserID)
return resolve({
data: globalUserID.toString(),
responseText: "",
responseXML: document.implementation.createHTMLDocument(""),
status: 200,
statusText: "success",
});
// Create xhr
var xhr = new XMLHttpRequest();
xhr.open("GET", "".concat(webURL, "/_layouts/viewlsts.aspx"), false);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
// Get SharePoint User ID variable
var spUserId = xhr.responseText.match(/userId:[0-9]+/gim);
// If not found, reject with error
if (!spUserId)
return reject(new classes_1.SpwsError({
message: "Page does not contain the _spUserId, unable to getCurrentUser",
}));
// Return the ID from the matched string
return resolve({
data: spUserId[0].split(":")[1],
responseText: xhr.responseText,
responseXML: xhr.responseXML,
status: xhr.status,
statusText: xhr.statusText,
});
}
else if (xhr.status === 404) {
// Create response error
reject(new classes_1.SpwsError({
status: xhr.status,
statusText: xhr.statusText,
message: "Site not found",
}));
}
else {
// Create response error
reject(new classes_1.SpwsError({ message: "Unable to get SharePoint User ID" }));
}
}
};
xhr.send();
});
};
exports.default = getCurrentUserID;
//# sourceMappingURL=getCurrentUserID.js.map