Created
March 1, 2018 15:21
-
-
Save phawrylak/24afbd8f141fa523518040e2cc9b09ad to your computer and use it in GitHub Desktop.
Example of getting OneDrive thumbnail URL for SharePoint List Item using method from modern list tile view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Web } from "sp-pnp-js"; | |
export default class ThumbnailService { | |
public async getThumbnailUrl( | |
webUrl: string, | |
listId: string, | |
listItemId: number, | |
width: number, | |
height: number | |
): Promise<string> { | |
const viewXml: string = ` | |
<View Scope='RecursiveAll'> | |
<Query> | |
<Where> | |
<Eq> | |
<FieldRef Name='ID' /> | |
<Value Type='Counter'>${listItemId}</Value> | |
</Eq> | |
</Where> | |
</Query> | |
<ViewFields> | |
<FieldRef Name='ContentType'/> | |
<FieldRef Name='DocIcon'/> | |
<FieldRef Name='PreviewOnForm' Explicit='TRUE'/> | |
<FieldRef Name='ThumbnailOnForm' Explicit='TRUE'/> | |
</ViewFields> | |
<RowLimit>1</RowLimit> | |
</View>`; | |
const web = new Web(webUrl); | |
const listData = await web.lists.getById(listId).renderListDataAsStream({ RenderOptions: 4103, ViewXml: viewXml }); | |
const item = (listData.ListData.Row as any[])[0]; | |
if (!item) { | |
return null; | |
} | |
return `${(listData.ListSchema[".thumbnailUrl"] as string) | |
.replace("{.mediaBaseUrl}", listData.ListSchema[".mediaBaseUrl"]) | |
.replace("{.fileType}", item[".fileType"]) | |
.replace("{.callerStack}", listData.ListSchema[".callerStack"]) | |
.replace("{.spItemUrl}", item[".spItemUrl"]) | |
.replace("{.driveAccessToken}", listData.ListSchema[".driveAccessToken"])}&width=${width}&height=${height}&cropMode=dochead`; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment