How to get document preview image using SharePoint CSOM

孤者浪人 提交于 2021-02-10 12:37:28

问题


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);

This just returns an empty ClientResult. I am not sure what to enter as clientType value.

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:

  • 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.


回答1:


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);


来源:https://stackoverflow.com/questions/33387858/how-to-get-document-preview-image-using-sharepoint-csom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!