#1
Posted
:
Thursday, September 27, 2018 5:25:02 AM(UTC)
Groups: Registered
Posts: 29
Thanks: 3 times
How can I get document with specific URL path from sharepoint for display directly?
#2
Posted
:
Thursday, September 27, 2018 3:12:17 PM(UTC)
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.
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.