Thursday, December 18, 2008

Add a Dynamics AX user to be a memebr of Sharepoint site group

In AX 4.0, it is allowed to link an AX user to a particular Sharepoint site group from main menu -> Administration -> Users -> User relations button -> Web sites tab. Unfortunately, this is removed from AX 2009.
However, we can still add this feature back if we like by using X++.
 
The example below show how easy can add an AX user to Sharepoint site owner groups by using X++ and CLRInterop.
Before you run this job, please add Microsoft.Sharepoint.dll (WSS 3.0) as a reference on AOT if it is not registered in GAC.
You can find this dll in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI on the server that Enterprise Portal resides.
 
static void AddAXUserToSharepointGroup(Args _args)
{
    Microsoft.SharePoint.SPWeb                   spWeb;
    Microsoft.SharePoint.SPSite                    spSite;
    Microsoft.SharePoint.SPGroupCollection  spGroupCollection;
    Microsoft.SharePoint.SPGroup                spGroup;
    Microsoft.SharePoint.SPUserCollection    spUserCollection;
    Microsoft.SharePoint.SPUser                  spUser;
    EPWebSiteParameters                           epWebSiteParameters;
    UserInfo                                              userInfo;
    SysUserInfo                                         sysUserInfo;
    str                                                       loginName;
    System.Exception                                 ex;
    str                                                       siteGroupName;
    ;
    
    try
    {
        //Sharepoint site registered in AX2009
        select firstonly epWebSiteParameters;
        select firstonly userInfo
            where userInfo.id == curUserId();
        select firstonly sysUserInfo
            where sysUserInfo.Id == curUserId();
        loginName           = userInfo.networkDomain + @"\" + userInfo.networkAlias;
        spSite              = new Microsoft.SharePoint.SPSite(epWebSiteParameters.InternalUrl);
        spWeb               = spSite.get_RootWeb();
        //Create a user in Sharepoint
        spUserCollection    = spWeb.get_SiteUsers();
        spUserCollection.Add(loginName, sysUserInfo.Email, userInfo.name, "");
        //Add the user to a site owner group
        spUser              = spUserCollection.get_Item(loginName);
        spGroupCollection   = spWeb.get_SiteGroups();
        siteGroupName       = CLRInterop::getAnyTypeForObject(spWeb.get_Title()) + " Owners"; //Or Visitors, Members
        spGroup             = spGroupCollection.get_Item(siteGroupName);
        spGroup.AddUser(spUser);
        spGroup.Update();
    }
    catch (exception::CLRError)
    {
        ex = CLRinterop::getLastException();    
        if (ex)
        {
            info(ex.ToString());           
        }
    }
    catch (exception::Internal)            
    {
        ex = clrinterop::getLastException();        
        if (ex)
        {
            info(ex.ToString());            
        }
    }
}
 
December 08

Failed to create COM objects on AX 4 AOS

You may come across this issue when attempt to initialize a COM object on AX 4 AOS server side:
 
static server void ActivateCOMOnAOS()
{
    #define.Word('Word.Application')
    COM com;
    InteropPermission permission = new InteropPermission(InteropKind::ComInterop);
    ;
 
    permission.assert();
    //BP deviation documented
    com = new COM(#Word);
    CodeAccessPermission::revertAssert();
}
And an error message will appear:
COM object of class 'Word.Application' could not be created. Ensure that the object has been properly registered on computer 'ComputerName'.
 
It is because by default, Microsoft Word as a COM object can not be launched by the user NT AUTHORITY\NETWORK SERVICE. 
 
The way to resolve this issue is to modify the Activation permission for Word Application using the Component Services administrative tool.
 
Configure DCOM (Windows 2003)
  • Start -> All programs -> Administrative Tools -> Component Services.
  • Expand Component Services
  • Expand Computers
  • Expand My Computer
  • Select the DCOM Config item
  • Select the Microsoft Word Document item.
  • Right click and select Properties
  • Under Launch and Activation Permissions select the Customize option.
  • Click the Edit button
  • Click the Add button to add a new account to the list.
  • Click the OK button
  • On the dialog that is displayed enter Network Service as the account name.
  • Grant Network Service the launch and activate permission.

No comments:

How to identify the user that was used to change an object from AOT in AX2012

Get the object name for which we need to track these (user and date&time) information's. Login to SQL Server Management Studio an...