Reputation: 41
I am trying to get the image from a URL as a BitmapImage but don't know how. The URL was in a list Sharepoint and I already retrieved like this:
public async Task<Microsoft.SharePoint.Client.ListItemCollection> getListContent(string listName)
{
Microsoft.SharePoint.Client.ListItemCollection items;
string userName = Settings.Default.User;
string password = Settings.Default.Pass;
string siteUrl = Settings.Default.URL;
using (ClientContext clientContext = new ClientContext(siteUrl))
{
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray())
{
securePassword.AppendChar(c);
}
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
await clientContext.ExecuteQueryAsync();
Microsoft.SharePoint.Client.List TargetList = clientContext.Web.Lists.GetByTitle(listName);
CamlQuery query = CamlQuery.CreateAllItemsQuery();
items = TargetList.GetItems(query);
clientContext.Load(items);
clientContext.ExecuteQuery();
}
return items;
}
One of these fields contains the url to an image in a sharepoint library and I am trying to retrieve it like this:
public Model.Control createModelControl(Microsoft.SharePoint.Client.ListItem item)
{
return new Model.Control
{
BmpImg = new BitmapImage(new Uri((item["Image"] as FieldUrlValue)?.Url))
}
When I inspect the result, this is what I have : enter image description here
I think I need to load it with a client context again, but am not sure of how to do it. With Sharepoint on premise, there was a method called "DownloadData" but it does not seem to exist with SPO...
Any ideas?
Upvotes: 2
Views: 953
Reputation: 41
I managed to have it work, here is how:
public static BitmapImage DownloadDocumentFromSharepoint(string imgUrl)
{
try
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
string userName = localSettings.Values["User"].ToString();
string password = localSettings.Values["Pass"].ToString();
string siteUrl = localSettings.Values["siteUrl"].ToString();
using (ClientContext ctx = new ClientContext(siteUrl))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = new SharePointOnlineCredentials(userName, password);
ctx.Load(ctx.Web, a => a.ServerRelativeUrl);
ctx.ExecuteQueryAsync().GetAwaiter().GetResult();
Microsoft.SharePoint.Client.File res = ctx.Web.GetFileByUrl(imgUrl);
ctx.Load(res);
ctx.ExecuteQueryAsync().GetAwaiter().GetResult();
Windows.Storage.Streams.IRandomAccessStream fileContent = null;
BitmapImage btmImgRes = new BitmapImage();
ClientResult<Stream> data = res.OpenBinaryStream();
ctx.Load(res);
ctx.ExecuteQueryAsync().GetAwaiter().GetResult();
using (MemoryStream mStream = new MemoryStream())
{
if (data != null)
{
data.Value.CopyTo(mStream);
mStream.Position = 0;
fileContent = mStream.AsRandomAccessStream();
btmImgRes.SetSource(fileContent);
}
}
return btmImgRes;
}
}
catch (Exception ex)
{
throw new Exception("fail to download document from sharepoint");
}
}
Upvotes: 2