SharePointUtilsCommunity
- This method allows lightning components in Salesforce Community to interact with sFiles API with stricter security checks.
Methods
global static String getErrorMessageMode()
AURAENABLED
global static string getItems(string session, string folderId, string nextLink, string optionsString)
AURAENABLED
Call getSettings method prior to calling getItems. getItems relies on session retrieved by the previous method.
getItems method allows listing of files and folders in a given SharePoint folder. It takes an input of session (creds), SharePoint folder Id, nextLink (when more than max file list size), and a string of options (currently only has showfolders). It returns a payload that contains list of folders / files (list of SharePointFileInfo) and nextLink (if over max file list size.) The example below shows how to call this method.
Parameters
Param | Description |
---|---|
session | Hashed information retrieved using the “getSettings” method, necessary to retrieve folders. |
folderId | SharePoint ID of the folder to open. |
nextLink | Use to retrieve more items when folder contains more than 100 items. Pass the nextLink that was returned the previous time that “getItems” was called. |
optionsString | Use to pass options as comma separated string. Only option currently is showFolders , a value that displays or hides folders. |
Returns
Type | Description |
---|---|
string | A serialized JSON that returns list of SharePointFileInfo. <ul> <li> results – Contains data and nextLink . data is a list of SharePointFileInfo, and nextLink displays a link to call the next 100 items, if the returned list of items is over 100.</li> <li>completedSuccessfully – A boolean value for success or error.</li> </ul> |
Example
await getItems(
{
session: this._session,
nextLink: this.nextLink,
optionsString: "showFolders"
})
.then((response) => {
this.handleFolderResponse(response);
})
.catch((error) => {
console.log(error);
});
global static string getSettings(string argsString)
AURAENABLED
This method is used to get the current record’s context as well as the object metadata settings related to the component. Call this method to instantiate a list view. It takes an input of argsString, which is a serialized JSON with necessary information. It returns a payload that contains the session, table columns, and the record’s SharePoint folder name and SharePoint Id. *
The example below shows a Lightning Web Component calling this method.
Parameters
Param | Description |
---|---|
argsString | A serialized JSON that includes uniqueId, recordId, siteIdField (optional), documentLibraryIdField(optional). <ul> <li> uniqueId hashed name of metadata object setting name</li> <li>recordId Salesforce record Id</li> <li>siteIdField Site Id field (optional)</li> <li>documentLibraryIdField Document Library Id (optional)</li> </ul> |
Returns
Type | Description |
---|---|
string | A serialized JSON that contains the status, errors (if any), and a results object that contains the SharePoint columns (dataColumns ), folder information (recordFolder ), and session . recordFolder contains parameters label , name , and webUrl . “Name” contains the following format: “0” (indicating that it is the root folder for that record) and the folder Id separated by a comma. This “0” can be useful when creating a list view with breadcrumbs that allow subfolder navigation. “webUrl” contains the direct link to open the folder in SharePoint. This can be useful if you would like to add a |
Example
const args = { uniqueId: this.uniqueId,
recordId: this.recordId,
siteIdField: this.siteIdField,
documentLibraryIdField: this.documentLibraryIdField };
<br/>
getSettings({ argsString: JSON.stringify(args) })
.then((response) => {
const payload = JSON.parse(response);
if(payload.completedSuccessfully){
let session = payload.results.session;
let columns = payload.results.dataColumns;
let folder = payload.results.recordFolder;
}
}
.catch((errorPayload) => {
console.log('Failed to get settings.');
});
global static string handleCommunityDownload(String session, String itemId)
AURAENABLED
Call this method to get the link to download a file in Community. It takes an input of session (creds) and item SharePoint Id. It returns a payload that contains a link.
Parameters
Param | Description |
---|---|
session | Hashed information retrieved using the “getSettings” method, necessary to retrieve folders. |
itemId | SharePoint ID of the file to download. |
Returns
Type | Description |
---|---|
string | Serialized JSON with results object, which contains downloadLink : a link to download the file from Salesforce Community. |
Example
const fileToDownloadId = '<ID OF FILE TO DOWNLOAD>';
handleCommunityDownload({
session: this._session,
itemId: fileToDownloadId
})
.then((response) => {
const payload = JSON.parse(response);
if (payload.completedSuccessfully) {
const file_path = payload.results.downloadLink;
} else {
throw payload.prettyErrorMessage;
}
})
.catch((error) => {
console.log('Error getting download link.');
});
global static string uploadFile(string argsString)
AURAENABLED
uploadFile method allows for secure upload from community using the “UniqueId.”
Parameters
Param | Description |
---|---|
argsString | A serialized JSON that includes uniqueId, recordId, documentId, siteIdField (optional), documentLibraryIdField(optional). <ul> <li> uniqueId hashed name of metadata object setting name</li> <li>recordId Salesforce record Id</li> <li>documentId ContentDocument Id of the uploaded file</li> <li>siteIdField Site Id field (optional)</li> <li>documentLibraryIdField Document Library Id (optional)</li> </ul> |
Returns
Type | Description |
---|---|
string | A serialized JSON that returns a status of success or error. |
Example
const file = {}; //uploaded file info with documentId
const args = { uniqueId: this.uniqueId,
itemId: file.documentId,
recordId: this.recordId,
siteIdField: this.siteIdField,
documentLibraryIdField: this.documentLibraryIdField };
<br/>
uploadFile({ argsString: JSON.stringify(args) })
.then((result) => {
const payload = JSON.parse(result);
if(payload.completedSuccessfully){
console.log('File upload success!');
}
else{
throw 'upload error';
}
}
.catch((error) => {
console.log('File upload error');
});