Skip to content

Instantly share code, notes, and snippets.

@phawrylak
Created March 1, 2018 15:21
Show Gist options
  • Save phawrylak/24afbd8f141fa523518040e2cc9b09ad to your computer and use it in GitHub Desktop.
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
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