Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, September 27, 2018 5:25:02 AM(UTC)
Vicki2018

Groups: Registered
Posts: 29

Thanks: 3 times

How can I get document with specific URL path from sharepoint for display directly?

 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

#2 Posted : Thursday, September 27, 2018 3:12:17 PM(UTC)

Hadi  
Hadi

Groups: Manager, Tech Support, Administrators
Posts: 218

Was thanked: 12 time(s) in 12 post(s)

Once you have the ClientContext, you just need to get the filename you are looking for and get the URL for that file like so:

On-prem SharePoint:

Code:
Microsoft.SharePoint.Client.List root = context.Web.Lists.GetByTitle("Shared Documents");
string downloadUri = GetCombinedPath(context, root, request.FileUri.ToString());

byte[] data = null;
using (Microsoft.SharePoint.Client.FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, downloadUri))
{
   using (var ms = new MemoryStream())
   {
      fileInfo.Stream.CopyTo(ms);
      ms.Position = 0;
      using (RasterCodecs codecs = new RasterCodecs())
      using (RasterImage img = codecs.Load(ms))
         viewer.Image = img;
   }
}


Code:
      private static string GetCombinedPath(ClientContext context, Microsoft.SharePoint.Client.List root, string uri)
      {
         context.Load(root, a => a.ParentWebUrl);
         context.ExecuteQuery();

         string combined = "/" + SharedDocumentsList + "/" + uri;
         string parentWebUrl = root.ParentWebUrl;
         if (!string.IsNullOrEmpty(parentWebUrl) && parentWebUrl != "/")
            combined = parentWebUrl + combined;
         return combined;
      }


With SharePoint Online:

Code:
List root = clientContext.Web.Lists.GetByTitle("Documents");
string downloadUri = GetCombinedPath(clientContext, root, fileUri);
var camlQuery = new CamlQuery();
Microsoft.SharePoint.Client.File file = clientContext.Web.GetFileByServerRelativeUrl(downloadUri);
ClientResult<Stream> data = file.OpenBinaryStream();
clientContext.Load(file);
clientContext.ExecuteQuery();
using (System.IO.MemoryStream mStream = new System.IO.MemoryStream())
{
if (data != null)
{
  data.Value.CopyTo(mStream);
  mStream.Position = 0;
  using (RasterCodecs codecs = new RasterCodecs())
  using (RasterImage img = codecs.Load(mStream))
    viewer.Image = img;
}
Hadi Chami
Developer Support Manager
LEAD Technologies, Inc.

LEAD Logo
 
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.061 seconds.