Hi,
If you're querying the sharepointdocument entity via Web API and getting either a 404 or an empty value: [] response - here's why and how to fix it.
Root Cause
The sharepointdocument entity is a virtual entity. It has no physical rows in the database. Records are dynamically resolved at runtime in the context of a parent record (Account, Contact, Opportunity, etc.). This means:
- Direct GUID lookup -> 404 (the record doesn't exist in DB)
- Global query with no filter -> empty result
Fix 1 - Read directly from the selected grid row (Recommended)
If your button is on the SharePoint document subgrid, the data is already loaded. No extra API call needed:
function onButtonClick(selectedControl) {
const selectedRows = selectedControl.getGrid().getSelectedRows();
if (selectedRows.getLength() === 0) {
Xrm.Navigation.openAlertDialog({ text: "Please select a document." });
return;
}
selectedRows.forEach(function(row) {
const entity = row.getData().getEntity();
const locationName = entity.attributes.get("locationname")?.getValue();
const absoluteUrl = entity.attributes.get("absoluteurl")?.getValue();
const title = entity.attributes.get("title")?.getValue();
const fileType = entity.attributes.get("filetype")?.getValue();
console.log("Location:", locationName);
console.log("URL:", absoluteUrl);
});
}
Fix 2 - Web API query with parent record filter
If you must use the Web API, always filter by the parent record's ID:
const parentId = Xrm.Page.data.entity.getId().replace(/[{}]/g, "");
const entityLogicalName = "account"; // change as per your parent entity
const result = await Xrm.WebApi.retrieveMultipleRecords(
"sharepointdocument",
`?$filter=regardingobjectid_${entityLogicalName} eq '${parentId}'` +
`&$select=locationname,absoluteurl,title,filetype,relativelocation`
);
console.log(result.entities);