Showing posts with label exist. Show all posts
Showing posts with label exist. Show all posts

Wednesday, January 4, 2012

Verify the URL Link and the File/Folder path in AX


I have experienced to verify the URL link in AX, Currently AX don’t contains any API that validate the URL Link
I have written a method that verifies the URL
public static boolean WebPathExists(str _url)
{
    boolean                    isExists;
    System.Net.WebRequest      UrlWebReq;
    System.Net.WebResponse     UrlWebRes;

    try
    {
        // Encode the URL first before passing to the Create method
        _url = System.Web.HttpUtility::UrlPathEncode(_url);
        UrlWebReq = System.Net.WebRequest::Create(_url);
        // This is needed if secure link is access like the AX share point portal
        // This is works only for the client calls
        UrlWebReq.set_Credentials(System.Net.CredentialCache::get_DefaultCredentials());
         UrlWebRes = UrlWebReq.GetResponse();
        // if there isn't any exception in the response (passed the above line of code) then the path is  correct.
        isExists  = true;
    }
    catch
    {
        isExists = false;
    }

    return isExists;
}

To checks the existence of Windows directory/File Path, there are some methods available in AX WinAPI class

WinAPI::folderExists(_filePath)
WinAPI::fileExists(_folderPath)