Get SharePoint Document Id Programmatically Java
-
11-10-2020 - |
Pregunta
I have SharePoint 2013 site mounted as a network drive. I am accessing the document library through Java code, and want to know how to get the unique Document Id that SharePoint maintains for each document.
I try to get the id for a file on a file system like this
public static final String getFileId(Path path)
throws Exception {
try {
if(path != null) {
BasicFileAttributes basicAttributes = Files.readAttributes(path, BasicFileAttributes.class);
// Linux file system, get file id from inode
if(basicAttributes.fileKey() != null) {
return basicAttributes.fileKey().toString();
} else {
// Windows file system, get file id via Kernel32.dll
logger.debug("The fileKey() method returned null");
WinBase.SECURITY_ATTRIBUTES attr = null;
BY_HANDLE_FILE_INFORMATION fileInformation = new BY_HANDLE_FILE_INFORMATION();
HANDLE fileHandler = Kernel32.INSTANCE.CreateFile(path.toString(), Kernel32.GENERIC_READ, Kernel32.FILE_SHARE_READ, attr, Kernel32.OPEN_EXISTING, Kernel32.FILE_ATTRIBUTE_ARCHIVE, null);
Kernel32.INSTANCE.GetFileInformationByHandle(fileHandler, fileInformation);
String fileId = fileInformation.nFileIndexHigh + "-" + fileInformation.nFileIndexLow;
Kernel32.INSTANCE.CloseHandle(fileHandler);
return fileId;
}
}
} catch(Exception err) {
throw err;
}
return SCConstant.EMPTY;
}
The above method when run on SharePoint documents (residing on the mounted network drive), give 0 for the index high and index low, and hence for all documents fileId is 0-0. I need a way to get the document id that SharePoint uses to uniquely identify each document.
Solución
I solved my question so I am posting an answer.
What I first did was enable Document ID feature for SharePoint. We need to go to Site Settings
and under Site Collection Administration > Site collection features
, we need to activate Document Id service. A scheduled job generally assigns document ids to all document existing in SharePoint.
Next we can use the endpoint /GetFileByServerRelativeUrl(filePath)/ListItemAllFields
to get the document id via SharePoint REST API.