Thursday, August 20, 2009

How to copy the favorites from one user to another

The favorites menu is a popular thing in Dynamics Ax. It has been around for quite some time now in Ax, in one form or another.
A common request that I get from users is: "Hey, I like this guy's favorites menu, can't I copy it to me own settings?'.
The answer in standard Ax is: No, you can't. But as with almost anything in Ax: It can be done with a little modification.

In order to be able to copy the favorites menu, you have to know where it is stored. This is done in the system table SysPersonalization, in the 'Buffer' field. We are looking for records in this table with the elementtype of UserMenu.

This next job will copy the favorites menu from user A to user B. (If user B has a favorities menu setup, it will be lost, as it first gets deleted.)

server static void FavoritesJob(Args _args)
{ SysPersonalization FromSysPersonalization;
SysPersonalization ToSysPersonalization;
UserId FromUserId='UserA';
UserId ToUserId='UserB';
;

ttsbegin;

// step 1 - delete current favorites menu from user
while select forupdate ToSysPersonalization
where ToSysPersonalization.ElementType==UtilElementType::UserMenu
&& ToSysPersonalization.UserId==ToUserId
{
ToSysPersonalization.doDelete();
}

// step 2 - duplicate from user A
while select FromSysPersonalization
where FromSysPersonalization.UserId==FromUserId
&& FromSysPersonalization.ElementType==UtilElementType::UserMenu
{
ToSysPersonalization.data(FromSysPersonalization);
ToSysPersonalization.UserId=ToUserId;
ToSysPersonalization.doInsert();
}

ttscommit;
}

(use at own risk)

Note: I have used the methods dodelete() and doinsert(), instead of delete() and insert(). Reason: You would run into trouble with access permissions otherwise.

Tuesday, August 18, 2009

Check the state of a Windows service

This is yet another example of the use of CLR Interop from within Dynamics Ax. This time, we're going to retrieve the current state of a Windows service (Spooler, Telnet, ...) on any given Windows machine.
I'm using the ServiceController class from the .NET framework for this. For this example to work, make sure you have added a reference in the AOT to the System.ServiceProcess assembly.

static boolean GetServiceState(str _servicename,str _machinename)
{ System.ServiceProcess.ServiceController ServiceController;
System.ServiceProcess.ServiceControllerStatus ServiceStatus;
str myStatus;

InteropPermission permission = new InteropPermission(InteropKind::ClrInterop);
;

if(!_servicename !_machinename)
return false;

try
{
permission.assert();

ServiceController= new System.ServiceProcess.ServiceController(_servicename,_machinename);
ServiceStatus=ServiceController.get_Status();
myStatus=ServiceStatus.ToString();

if(myStatus=='Running')
return true;
} catch
{

}

return false;
}

Make sure you have appropriate rights for executing this code.

This code can be modified to start and stop services as well.

Monday, August 17, 2009

Create a XML file in Ax

This blog post is dedicated to XML files. A simple example, showing you how to create an XML file. Luckily, Ax already contains the framework to create an XML file, so it can do most of our work.


static void XML_Basic(Args _args)
{ XMLDocument xmlDoc;
XMLNode nodeRoot, commentNode;

XMLNode PanelNode;

XMLNode LengthNode;
XMLNode WidthNode;

FileName xmlFileName;

;

// create XML
xmlDoc = XMLDocument::newBlank();
nodeRoot = xmlDoc.documentElement();

// create XML nodes
commentNode = xmlDoc.appendChild(xmlDoc.createComment('Comment'));
commentNode.text('XML file from
http://dynamics-ax-live.blogspot.com');

PanelNode = xmlDoc.appendChild(xmlDoc.createElement('Panel'));
LengthNode = PanelNode.appendChild(xmlDoc.createElement('Length'));
LengthNode.text('2440');
WidthNode = PanelNode.appendChild(xmlDoc.createElement('Width'));

WidthNode.text('1220');

// write XML to file
xmlFileName=@'C:\MyXML.XML';
new FileIoPermission(xmlFileName, 'rw').assert();
xmlDoc.save(xmlFileName);
CodeAccessPermission::revertAssert();}


There are 3 basic steps in this small 'tutorial'. Step 1, create the XML document. Step 2, add the XML nodes. And step 3, save the XML document in a file. This example will create a very basic XML file, which introduces you to the world of tags, elements and attributes.






As XML files are a common way of exchanging information, you may want to get familiar to the terminology over here.

Sunday, August 2, 2009

Bug with Workspace toolbar

There seems to be a bug in Ax 2009 SP1, regarding the Workspace toolbar.
For common users to see it, access to the security key SysDevelopment is required. A bit strange, as this is a 'No access' - 'Full control' kind of security key. When giving plain users access to this key, and even when you switch off all it's subkeys, you still give your users access to the AOT. Doesn't seem to be desired functionality here now, is it?