Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get recent documents for current user
// within your web part onInit method, get the request digest token
const digestCache: IDigestCache = this.context.serviceScope.consume(DigestCache.serviceKey);
const requestDigest = await digestCache.fetchDigest(this.context.pageContext.web.serverRelativeUrl);
//now on your data access layer
// get token
const authRequestHeaders: Headers = new Headers();
authRequestHeaders.append("Accept", "application/json;odata.metadata=minimal");
authRequestHeaders.append("Content-Type", "application/json; charset=utf-8");
authRequestHeaders.append("odata-version", "4.0");
authRequestHeaders.append("x-requestdigest", this._requestDigest);
var resourceData = {
"resource": "https://officeapps.live.com",
};
const authRequestOptions: IHttpClientOptions = {
headers: authRequestHeaders,
body: JSON.stringify(resourceData),
};
const authEndpointUrl = this._webAbsoluteUrl + '/_api/SP.OAuth.Token/Acquire';
const authRawResponse = await this._httpClient.post(authEndpointUrl, HttpClient.configurations.v1, authRequestOptions);
const auth = await authRawResponse.json();
// get data
const dataRequestHeaders: Headers = new Headers();
dataRequestHeaders.append("Authorization", "Bearer " + auth.access_token);
dataRequestHeaders.append("Content-Type", "application/json");
dataRequestHeaders.append("X-Office-Platform", "Web");
dataRequestHeaders.append("X-Office-Application", "120");
dataRequestHeaders.append("X-Office-Version", "*");
const dataRequestOptions: IHttpClientOptions = {
headers: dataRequestHeaders,
};
const dataEndpointUrl = `https://ocws.officeapps.live.com/ocs/v2/recent/docs?apps=Word,Excel,PowerPoint,Visio,OneNote,Sway,PdfViewer&show=${count}&sort=Date`;
const dataRawResponse = await this._httpClient.get(dataEndpointUrl, HttpClient.configurations.v1, dataRequestOptions);
const data = await dataRawResponse.json();
@bk-td
Copy link

bk-td commented Dec 1, 2020

Hi Joel,

Great script. I'm look to get a bearer token for the https://ocws.officeapps.live.com/ocs/v2/recent/docs
Any token retrieved via MSAL doesn't work, any idea how to get this working?

Thanks.

@joelfmrodrigues
Copy link
Author

joelfmrodrigues commented Dec 3, 2020

My code is using a bearer token, but no idea if MSAL would work. Can you not get a token the same way I did (from /_api/SP.OAuth.Token/Acquire)?

@bk-td
Copy link

bk-td commented Dec 3, 2020

Don't think so, because I'm not using a webpart, but an WPF app (with Azure AD App registration).
But I'll look into it, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment