Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a ListItem in another site using the REST API
(async function() {
function getFailedResponseData(response) {
return new Promise( async (resolve) => {
const ct = response.headers.get("Content-Type") || "";
let data = {};
if (ct.match("application/json")) {
data = await response.json();
} else {
data = {
"odata.error": {
"code": `0x8000FFFF, E_UNEXPECTED, httpStatus(${response.status}${response.statusText ? ", " + response.statusText : ""})`,
"message": {
"lang": "en-us",
"value": await response.text()
}
}
}
}
resolve(data);
});
}
function getSiteContextInfo(siteUrl) {
return new Promise( async (resolve, reject) => {
const url = `${siteUrl}/_api/contextinfo`;
const response = await fetch(url, {
"credentials": "include",
"method": "POST",
"headers": {
"Accept": "application/json;odata=nometadata",
"Cache-Control": "no-cache"
},
});
if (response.ok) {
const data = await response.json();
resolve({
"formDigest": data.FormDigestValue || "",
"webFullUrl": `${data.WebFullUrl}`
});
} else {
let data = await getFailedResponseData(response);
reject(data);
}
});
}
function addEDMSOperation(siteContext, opRequest) {
return new Promise( async (resolve, reject) => {
const body = {
"Title": opRequest.EDMSTargetItemUrl || "",
"EDMSTargetSiteUrl": opRequest.EDMSTargetSiteUrl || "",
"EDMSTargetSiteId": opRequest.EDMSTargetSiteId || "",
"EDMSTargetWebId": opRequest.EDMSTargetWebId || "",
"EDMSTargetListId": opRequest.EDMSTargetListId || "",
"EDMSTargetItemId": opRequest.EDMSTargetItemId || "",
"EDMSAction": opRequest.EDMSAction || "",
"EDMSOperationStatus": "Pending",
"EDMSOperationOutcome": "Pending"
};
const url = `${siteContext.webFullUrl}/_api/web/lists/GetByTitle('EDMS Transactions')/items`;
const response = await fetch(url, {
"credentials": "include",
"method": "POST",
"headers": {
"X-RequestDigest": `${siteContext.formDigest}`,
"Accept": "application/json;odata=nometadata",
"Content-Type": "application/json;odata=nometadata",
"Cache-Control": "no-cache"
},
"body": JSON.stringify(body)
});
if (response.ok) {
const data = await response.json();
resolve(data);
} else {
let data = await getFailedResponseData(response);
reject(data);
}
});
}
let siteContext = null;
const otherSiteUrl = "https://platinumdogsconsulting.sharepoint.com/sites/hubsitetest";
//const otherSiteUrl = "https://platinumdogsconsulting.sharepoint.com/sites/audiotonix";
try {
siteContext = await getSiteContextInfo(otherSiteUrl);
console.log(`CONTEXTINFO site: ${siteContext.webFullUrl}`);
console.log(`CONTEXTINFO digest: ${siteContext.formDigest}`);
} catch(e) {
console.warn(`ERROR GETTING CONTEXTINFO for [${otherSiteUrl}]: `, e);
}
if (siteContext) {
try {
//"https://platinumdogsconsulting.sharepoint.com/sites/hubsitetest/Lists/EDMS%20Transactions/AllItems.aspx"
const opRequest = {
"EDMSTargetItemUrl": `${window.location.href}`,
"EDMSTargetSiteUrl": `${_spPageContextInfo.webServerRelativeUrl}`,
"EDMSTargetSiteId": `${_spPageContextInfo.siteId.replace(/[\{\}]*/gi, '')}`,
"EDMSTargetWebId": `${_spPageContextInfo.webId.replace(/[\{\}]*/gi, '')}`,
"EDMSTargetListId": `${_spPageContextInfo.listId.replace(/[\{\}]*/gi, '')}`,
"EDMSTargetItemId": `${_spPageContextInfo.pageItemId}`,
"EDMSAction": "Close"
}
const opData = await addEDMSOperation(siteContext, opRequest);
console.log(`ADDEDMSOPERATION at ${siteContext.webFullUrl}`, opData);
} catch(e) {
console.warn(`ERROR CALLING ADDEDMSOPERATION for [${siteContext.webFullUrl}]: `, e);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.