如何使用 SharePoint CSOM 获取文档预览图像 [英] How to get document preview image using SharePoint CSOM
问题描述
我想以编程方式从 SharePoint 检索文档的缩略图.我正在尝试做的是以下内容:
I would like to programmatically retrieve thumbnails of documents from SharePoint. What I'm trying to do is the following:
document.GetImagePreviewUrl(width, height, clientType);
这只是返回一个空的ClientResult
.我不确定要输入什么作为 clientType
值.
This just returns an empty ClientResult
. I am not sure what to enter as clientType
value.
我也尝试使用 this 方法(通过使用 WebClient
并下载文件).但这只是返回一个 403
响应.
I have also tried to use this method programmatically (by using WebClient
and downloading the file). But that just returns a 403
response.
我在这里看到的可能解决方案如下:
The possible solutions I see here are the following:
- 弄清楚要输入什么作为
clientType
并以这种方式检索预览网址. - 弄清楚如何以编程方式告诉 SharePoint 我已获得授权(例如使用
WebClient
和标头).
- Figure out what to enter as
clientType
and retrieve the preview url that way. - Figure out how to tell SharePoint that I am authorized programmatically (using
WebClient
and headers for instance).
关于这两个选项我确实需要一些帮助,我不知道从哪里开始,因为这两个选项都没有很好的记录.
I do need some help regarding these two options, I am not sure where to start since both options aren't well documented.
推荐答案
我想出了一个办法,403
错误是因为 sharepoint 不知道我是谁.经过一些研究和摆弄,我发现您发送到预览页面的请求包含一个身份验证 cookie.这个cookie可以通过使用这段代码的代码生成:
I've figured out a way to do it, the 403
error was caused because sharepoint had no idea who I was. After some research and fiddling I found out that the request you send to the preview page contains an authentication cookie. This cookie can be generated by code using this piece of code:
// Create an authentication cookie which we can send with the request so sharepoint knows who we are.
var authCookie = credentials.GetAuthenticationCookie(new Uri(imageUrl));
client.Headers.Add(HttpRequestHeader.Cookie, authCookie);
// Download the image data to a byte array
image = client.DownloadData(imageUrl);
这篇关于如何使用 SharePoint CSOM 获取文档预览图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!