Need to download image from sharepoint to D365 bitmap field by x++


How to download Image from sharepoint in D365 by x++ and assign it to bitmap table field that it could be used in reports ?

This is quite a lot of requirements - which ones do you need help with?

If I was you, I would split the problem to several parts and deal with them one by one:

  1. Connecting to SharePoint.
  2. Downloading a file from SharePoint.
  3. Saving the file content to database.


Ok in reality i need help on all the things, never did that and cant find too much information,

im trying to do it without any particular sharepoint classes, but doesnt seem to be possible, getting 403 unauthorized error

imageFilePathName = ‘http link to png file in sharepoint’; (direct link to image…)

new InteropPermission(InteropKind::ClrInterop).assert();

myWebClient = new System.Net.WebClient();

myWebClient.Credentials = new System.Net.NetworkCredential(“login”, “password”, “”);

System.Byte[] fileData = myWebClient.DownloadData(imageFilePathName);

All right, so I suggest you focus on the first point only for now.

please make clear whether you mean SharePoint Online or on-premises SharePoint.

sharepoint online

There is already support for SharePoint Online inside F&O, namely in document management (and electronic reporting, which utilizes document management). You may be able to reuse this logic.

For example, look at ERFileSourceSharePoint.appendFilesByDocuType():

proxy = this.getProxy(_docuType.Host, _docuType.Site);
FileResults fileResults = SharePointHelper::GetFiles(proxy, _docuType.FolderPath, '');    
if (fileResults && fileResults.Results)
	IEnumerable results = fileResults.Results;
	var fileListEnumerator = results.GetEnumerator();
	while (fileListEnumerator.MoveNext())
		FileResult fileResult = fileListEnumerator.Current;

Investigating, will let you know does this SharePointHelper class can help doing that

Hi, so i’ve got the image, but there is an issue, the image is way smaller and totally destroyed, it looks like i only see part of it (the top 1/8 of image) what am i doing wrong here ? my code is below

FileContents file = SharePointHelper::GetFileContents(proxy, uri);

System.Byte[] imageBuffer = new System.Byte4096;

int countRead = file.Content.Read(imageBuffer, 0, imageBuffer.Length);

System.IO.MemoryStream ms = new System.IO.MemoryStream(imageBuffer);

Binary binary = binary::constructFromMemoryStream(ms);

Bitmap bitmap = binary.getContainer();

salesPackingSlipHeaderTmp.SignatureImage = bitmap;

Image format is PNG, im getting it by passing in URI Sharepoint API,

URI is - “’/folder/folder2/Signature%20(1).png’)”);

System.Byte[] imageBuffer = new System.Byte4096; i tried setting this to higher value - doesnt help, what should i be setting here ? i couldnt see any length or size in FileContents SP Object

What if you get rid off the byte array completely and copy data directly between streams?

using (System.IO.MemoryStream ms = new System.IO.MemoryStream())

By the way, please use Insert > Insert Code to paste source code. It makes code easier to read.

Hey, if i use this code - i get invalid token ‘(’ , earlier i was solving this by adding a assignment to a new variable, now as i see CopyTo is VOID and i cant assign it, yes i understand it doesnt need to be assigned and it copies the data to the ms stream variable but i still get the Invalid token ‘(’ error and i cant compile

using (System.IO.MemoryStream ms = new System.IO.MemoryStream())

It would helped if you told us where the error was.

I assume it’s on the line inside the loop - .NET Interop from X++ often doesn’t like chains of references. Try assigning file.Content to a separate variable:

System.IO.Stream fileStream = file.Content;

Your statement “CopyTo is VOID and i cant assign it” sounds like you’re trying to assign a return value of CopyTo() to something, which is indeed wrong. Please look again at my code and check if you have the same.

Thanks man, it works fine