Search code examples
c#dynamics-crmcrm

CRM Dynamics Plugin access to a network folder


I hope that someone could help me with this issue, I have to download a file attached to a note in a CRM case and save it in a network folder. To connect with the folder I am using the WNetAddConnection2 function but I am getting the error code 120 that is

ERROR_CALL_NOT_IMPLEMENTED 120 (0x78) This function is not supported on this system

I tried to connect to the folder from a console project in my laptop and I can do it, so the problem seems to be that it is not possible to connect to the folder from CRM.

This is the function I use:

public ConnectToSharedFolder(string networkName, NetworkCredential credentials)
{
            _networkName = networkName;

            var netResource = new NetResource
            {
                Scope = ResourceScope.GlobalNetwork,
                ResourceType = ResourceType.Disk,
                DisplayType = ResourceDisplaytype.Share,
                RemoteName = networkName
            };

            var userName = string.IsNullOrEmpty(credentials.Domain)
                ? credentials.UserName
                : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);

            var result = WNetAddConnection2(
                netResource,
                credentials.Password,
                userName,
                0);

            if (result != 0)
            {
                throw new Win32Exception(result, "Error connecting to remote share");
            }
        }

Do anyone know if this it's possible to do it? Thanks!


Solution

  • Dynamics Online only allows plugins in Sandboxed mode, which only allows outside access via HTTP or HTTPS.

    You will not be able to write to a network folder from a Dynamics Online plugin.

    The plugin would have to save the file to somewhere like an Azure Blob storage container and then have a separate process like a Power Automate Flow download it from there to the network folder.

    Come to think of it, you could probably post the file directly to Power Automate Flow HTTP endpoint and have the Flow save it to the network folder, skipping Azure Blob Storage.

    Even if the system were on-prem and you could register the plugin outside of sandbox mode, you'd want to consider that the system might move online someday, so it would still not be a great practice to write to a network folder.