pnp / pnpcore

The PnP Core SDK is a modern .NET SDK designed to work for Microsoft 365. It provides a unified object model for working with SharePoint Online and Teams which is agnostic to the underlying API's being called
https://aka.ms/pnp/coresdk/docs
MIT License
288 stars 188 forks source link

ServerRedirectedEmbedUrl and LinkingUrl Values Inconsistent or Incomplete #1455

Closed sguidos closed 2 months ago

sguidos commented 2 months ago

Category

Describe the bug

The ServerRedirectedEmbedUrl attribute returns a valid URL for files of type .PDF, .DOCX, .DOC, .PPTX, .PPT and .XLSX, but returns an empty string with .XLS (!) and other formats that are viewable in SharePoint by simply clicking on the file in the Documents view (.TXT, .JPG, .PNG, .GIF, etc).

Ironically, the URL returned in the Parent.LinkingUrl is empty for .PDF but valid for .XLS file types.

Steps to reproduce

            using (var context = await pnpContextFactory.CreateAsync(Program.SharePointSiteAuthConfigName)) // name must match the site configuration name created in Program.cs
            {
                var site = await context.Site.GetAsync(site => site.ServerRelativeUrl);
                string targetFolderName = site.ServerRelativeUrl + "/Shared%20Documents/" + txtNamedFolder.Text;
                IFolder namedFolder = context.Web.GetFolderByServerRelativeUrl(targetFolderName);

                IFile addedFile = await namedFolder.Files.AddAsync(Path.GetFileName(txtFileToUpload.Text),
                  System.IO.File.OpenRead(txtFileToUpload.Text), true);

                var addedFileProps = addedFile.ListItemAllFields;
                addedFileProps.Load(l => l.ServerRedirectedEmbedUrl, l => l.UniqueId, l => l.Parent);
                string embedUrl = addedFileProps.ServerRedirectedEmbedUrl;
                if ((string.IsNullOrEmpty(embedUrl)) && (addedFileProps.Parent != null) && (addedFileProps.Parent is IFile))
                {
                    IFile parentFile = (IFile)addedFileProps.Parent;
                    embedUrl = parentFile.LinkingUrl;
                }
                if (string.IsNullOrEmpty(embedUrl))
                {
                    MessageBox.Show($"Cannot Preview {Path.GetFileName(txtFileToUpload.Text)} in SharePoint", "Warning");
                }
                else
                {
                    DisplayDocumentInBrowser(embedUrl);
                }
            }

Expected behavior

A valid ServerRedirectedEmbedUrl and LinkingUrl value is returned for all recognized file types. Any file type that I can "click and preview" in the Documents list should return a valid ServerRedirectedEmbedUrl and LinkingUrl value.

Environment details (development & target environment)

Additional context

I am simply trying to let a user upload a file to our SharePoint Online instance and then Preview what was uploaded in their browser.

Thanks for your contribution! Sharing is caring.

jansenbe commented 2 months ago

@sguidos : this is not an issue in PnP Core SDK as we simply return what the SharePoint API returns. Taking a quick peek at the internal SharePoint source I think this will only return a valid URL for the files that can be edited using Office online editors (Word, Excel, PowerPoint,...)

If you want to get a thumbnail it's best to use the respective GetThumnbails method (see https://pnp.github.io/pnpcore/using-the-sdk/files-intro.html#getting-thumbnails-for-a-file) or GetPreview method (https://pnp.github.io/pnpcore/using-the-sdk/files-intro.html#getting-an-embeddable-preview-url).

sguidos commented 2 months ago

GetPreview() is exactly what I was looking for, and is working with a wide variety of file types. Thanks!