console.clear();
let mContext = {
// URL du site courant (contexte de la page SharePoint classique).
// ⚠️ Limite : si _spPageContextInfo n'existe pas (hors page SP), prévoir une URL de repli (… || "https://…").
siteUrl: _spPageContextInfo ? _spPageContextInfo.webAbsoluteUrl : "Shared%20Documents",
query: "Id le 500",// and FSObjType eq 0
//query: "Id le 500",// and FSObjType eq 0
doclib: "Shared%20Documents",
select: "",//"&$select=TotalSize,TotalFileCount,LastModified",
//select: "?$select=FileDirRef,HasUniqueRoleAssignments,Id,Title,FileLeafRef,Modified,vCreated,FSObjType,ContentTypeId,FileSizeDisplay",
list: null,
listFields: null,
listFieldsNotHidden: null,
webServerRelativUrl: "/sites/FDI_SandBox",
fetchGetOptions: {
method: 'GET',
headers: {
'Accept': 'application/json;odata=verbose'
}
}
}
// get FormDigestValue
mContext.getRequestDigest = async function () {
const response = await fetch(`${mContext.siteUrl}/_api/contextinfo`, {
method: "POST",
headers: { Accept: "application/json;odata=verbose" },
credentials: "include",
});
const data = await response.json();
return data.d.GetContextWebInformation.FormDigestValue;
}
mContext.ExecuteQuery = async function ExecuteQuery(req, fetchOptions, maxRetry = 3, wait = 1, trynum = 1) {
console.log("ExecuteQuery", req, fetchOptions, maxRetry, wait, trynum);
if (trynum >= maxRetry) {
console.log("ExecuteQuery Error", req);
console.log("ExecuteQuery Error", fetchOptions);
throw new Error(`ExecuteQuery error! maxRetry >= trynum`);
}
try {
let startDate = new Date();
let diffMinutes = 0;
respList1 = await fetch(req, fetchOptions);
endDate = new Date();
const diffMs = endDate - startDate;
diffMinutes = Math.floor(diffMs / (1000));
console.log(`seconds ${diffMinutes} queryNumber ${trynum}`)
//avoid 429 too much queries
if (!respList1.ok && respList1.status == 429) {
const errorDetails = await respList1.text(); // Get error details from the response
let err = JSON.parse(errorDetails);
console.error(`HTTP error! Status: ${respList1.status}, Details: ${err.error.message.value}`);
await sleep(wait * 10);// * trynum
return await ExecuteQuery(req, fetchOptions, maxRetry, (wait * 5), ++trynum);
}
//avoid 503 server unavailable / connections error
if (!respList1.ok && respList1.status == 503) {
const errorDetails = await respList1.text(); // Get error details from the response
let err = JSON.parse(errorDetails);
console.error(`HTTP error! Status: ${respList1.status}, Details: ${err.error.message.value}`);
await sleep(wait * 10);// * trynum
//return await ExecuteQuery(req, fetchOptions, maxRetry, (wait * 5), ++trynum);
}
//avoid 403 reload page in another tab
if (!respList1.ok && respList1.status == 403) {
//debugger;
window.open(_spPageContextInfo.webAbsoluteUrl, '_blank')
const errorDetails = await respList1.text(); // Get error details from the response
let err = JSON.parse(errorDetails);
console.error(`HTTP error! Status: ${respList1.status}, Details: ${err.error.message.value}`);
await sleep(wait * 10);// * trynum
window.open(_spPageContextInfo.webAbsoluteUrl, '_blank')
//return await ExecuteQuery(req, fetchOptions, maxRetry, (wait * 5), ++trynum);
}
if (!respList1.ok) {
console.log("ExecuteQuery Error", respList1);
const errorDetails = await respList1.text(); // Get error details from the response
const errorDetails2 = await respList1.json(); // Get error details from the response
console.log("ExecuteQuery Error", respList1);
console.error(`HTTP error! Status: ${respList1.status}, Details: ${errorDetails}`);
let err = JSON.parse(errorDetails);
console.error(`HTTP error! Status: ${respList1.status}, Details: ${err.error.message.value}`);
console.log(err.error.message.value);
// Find a validation error returned by SharePoint.
const validationError = errorDetails2.find(
function findValidationError(result) {
return result.HasException === true;
}
);
// Throw the SharePoint field validation error.
if (validationError !== undefined) {
throw new Error(
"SharePoint rejected the field update. " +
`Field: '${validationError.FieldName}'. ` +
`Details: '${validationError.ErrorMessage}'.`
);
}
throw new Error(`HTTP error! Status: ${respList1.status}`);
}
return respList1
} catch (error) {
console.log("req", req);
console.log("fetchOptions", fetchOptions);
console.log(error);
throw new Error(`HTTP error! Status: ${error}`);
}
}
function formatDateForValidateUpdateListItem(isoDate) {
const date = new Date(isoDate);
if (Number.isNaN(date.getTime())) {
throw new Error(
`La date SharePoint reçue est invalide : '${isoDate}'.`
);
}
const pad = value => String(value).padStart(2, "0");
let hours = date.getHours();
const minutes = date.getMinutes();
const ampm = hours >= 12 ? "PM" : "AM";
// Conversion 24h -> 12h
hours = hours % 12;
hours = hours === 0 ? 12 : hours;
return (
`${pad(date.getMonth() + 1)}/` +
`${pad(date.getDate())}/` +
`${date.getFullYear()} ` +
`${hours}:` +
`${pad(minutes)} ${ampm}`
);
}
// Get a readable error message.
/**
* Change the "Modified By" system field of a SharePoint list item.
*
* Internal SharePoint field name: Editor
*
* Important:
* - listUrl must be the root URL of the list or library.
* - The list must be in the Web identified by mContext.siteUrl.
*
* Examples:
* /sites/FDI_SandBox/Lists/MyList
* /sites/FDI_SandBox/Shared Documents
*
* @param {string} listUrl
* @param {number|string} itemId
* @param {string} userEmail
* @returns {Promise
SharePont Keep Modified And Edited
Add a comment
