Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested answer

CSV Export from Enterprise Portal

(0) ShareShare
ReportReport
Posted on by 165

Hello all,

I've created a new class that exports CSV from selected records on the list page.

It's working fine when run on AX client, but when running the button from Enterprise Portal, it doesn't save the CSV file on the folder. No errors encountered.

Is there additional permissions needed? Below is my code:

client server public static void main(Args _args)
{
#File
#xppTexts
CommaIo file;
container line;
str filepath;
container confilter = ['.csv', '*.csv'];
CommaTextIo commaTextIo;
FileIOPermission permission;
tableA tableA;
Common multiSelectionRecord;
MultiSelectionContext multiSelectionContext;
TextBuffer textBuffer;
str fileName;
utcDateTime dateTime = DateTimeUtil::getSystemDateTime();

;


fileName = curUserId() + DateTimeUtil::toFormattedStr(dateTime, 231, DateDay::Digits2,
DateSeparator::None, DateMonth::Short, DateSeparator::None, DateYear::Digits4, TimeSeparator::Dot, TimeSeparator::Dot);

filepath = strFmt("C:\\temp\\%1.csv", filename);
new FileIOPermission(filepath, "rw").assert();
textBuffer = new TextBuffer();


//write header
textBuffer.appendText("Item Id");

multiSelectionContext = _args.multiSelectionContext();
if(multiSelectionContext)
{
multiSelectionRecord = multiSelectionContext.getFirst();
tableA = multiSelectionRecord;
while(tableA)
{


textBuffer.appendText(strFmt('%1', tableA.ItemId));
textBuffer.appendText(#newline);
tableA = multiSelectionContext.getNext();
}
}
textBuffer.toFile(filepath, FileEncoding::UTF8);
CodeAccessPermission::revertAssert();
}

  • Martin Dráb Profile Picture
    232,093 Most Valuable Professional on at
    RE: CSV Export from Enterprise Portal

    Please never describe your problem with mere "it doesn't work". Give us more information.

    For example, if you get an error, tell us what the error messages says and where the error was thrown from.

    That is works on client proves absolutely nothing. That's executed by a different process (AX client vs. web server), by a different account and often on a different machine. I don't dispute that you have access to C:\Temp on your machine, but that's not the question. We're talking about if Enterprise Portal have access to that path. There may even be no C:\Temp folder on the server where EP is installed.

  • mrpequit Profile Picture
    165 on at
    RE: CSV Export from Enterprise Portal

    Hi Martin,

    WinAPIIServer/WinAPI doesn't work when called from EP unfortunately. I've also tested the path and I have access to them since the file writes when run on ax client. It now works only for  network shared folders.

  • Suggested answer
    Martin Dráb Profile Picture
    232,093 Most Valuable Professional on at
    RE: CSV Export from Enterprise Portal

    Please always use Insert > Code (in the rich formatting view) to paste source code. Your code is very difficult to read because you didn't paste it in the right way and line indentation got lost. I tried to fix it for you, but the code looks invalid, because of the extra } at line 41. Can you please review your code and update it?

    client server public static void main(Args _args)
    {
    	#File
    	#xppTexts
    	CommaIo file;
    	container line;
    	str filepath;
    	container confilter = ['.csv', '*.csv'];
    	CommaTextIo commaTextIo;
    	FileIOPermission permission;
    	tableA tableA;
    	Common multiSelectionRecord;
    	MultiSelectionContext multiSelectionContext;
    	TextBuffer textBuffer;
    	str fileName;
    	utcDateTime dateTime = DateTimeUtil::getSystemDateTime();
    	;
    
    	fileName = curUserId()   DateTimeUtil::toFormattedStr(dateTime, 231, DateDay::Digits2,
    	DateSeparator::None, DateMonth::Short, DateSeparator::None, DateYear::Digits4, TimeSeparator::Dot, TimeSeparator::Dot);
    
    	filepath = strFmt("C:\\temp\\%1.csv", filename);
    	new FileIOPermission(filepath, "rw").assert();
    	textBuffer = new TextBuffer();
    
    
    	//write header
    	textBuffer.appendText("Item Id");
    
    	multiSelectionContext = _args.multiSelectionContext();
    	if(multiSelectionContext)
    	{
    	multiSelectionRecord = multiSelectionContext.getFirst();
    	tableA = multiSelectionRecord;
    	while(tableA)
    	{
    		textBuffer.appendText(strFmt('%1', tableA.ItemId));
    		textBuffer.appendText(#newline);
    		tableA = multiSelectionContext.getNext();
    	}
    	}
    	textBuffer.toFile(filepath, FileEncoding::UTF8);
    	CodeAccessPermission::revertAssert();
    }

    Anyway, I see that your code assumes that the account that the IIS application pools is running under have permissions to wrote to C:\temp. It normally shoudn't, which is likely the reason of failure. Use WinAPIServer::getTempPath()  to get the corrent temporary folder path.

    By the way, you could have tested whether you can access the path or not.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,296 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,093 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans