davidyack / Xrm.Tools.CRMWebAPI

This is an API helper for working with the Common Data Service (CDS) Web API
MIT License
143 stars 74 forks source link

GetList results in a 404 - 'File or directory not found' excepttion for too many columns #59

Open crmdev1 opened 6 years ago

crmdev1 commented 6 years ago

Odd behaviour , running against an IFD, account query with more than 22 columns causes a 404, same against contacts, allowed more columns but same result:

` [TestClass] public class GetListTest : UnitTestBaseClass { [TestMethod] public void TestMethod1() {

        Task.Run(async () =>
        {
            var api = GetAPI();

            string entitySet = "accounts";
            string selectAttributes = "<attribute name='accountid'/><attribute name='name'/><attribute name='emailaddress1'/><attribute name='websiteurl'/><attribute name='telephone1'/><attribute name='fax'/><attribute name='accountnumber'/><attribute name='address1_line1'/><attribute name='address1_line2'/><attribute name='address1_line3'/><attribute name='address1_city'/><attribute name='address1_postalcode'/><attribute name='address1_country'/><attribute name='address2_line1'/><attribute name='address2_line2'/><attribute name='address2_city'/><attribute name='address2_postalcode'/><attribute name='address2_country'/><attribute name='parentaccountid'/><attribute name='primarycontactid'/><attribute name='statecode'/><attribute name='statuscode'/>";
            selectAttributes += "<attribute name='telephone2'/>"; // Any extra field causes a 404

            string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>\r\n                                              <entity name='account'>\r\n                                                <filter type='and'>\r\n                                                  <condition attribute='primarycontactid' operator='eq' value='74eaeb77-e23b-e811-a9a7-000d3af3c361' />\r\n                                                </filter>\r\n                                              </entity>\r\n                                            </fetch>";

            CRMGetListOptions queryOptions = null;
            if (fetchXml != null)
            {
                queryOptions = new CRMGetListOptions { FetchXml = WebUtility.UrlEncode(FetchXmlWithSelectAttributes(fetchXml, selectAttributes)) };
            }
            var getListResult = await api.GetList(entitySet, QueryOptions: queryOptions);
            var list = getListResult.List;

        }).Wait();

    }

    private static string FetchXmlWithSelectAttributes(string fetchXml, string selectAttributes)
    {
        string entityTagPattern = @"<entity\s+name='\w+'\s*>";
        string entityTag = "";
        Regex regex = new Regex(entityTagPattern);
        Match matchResult = regex.Match(fetchXml);
        if (matchResult.Success)
        {
            entityTag = matchResult.Value;
        }

        int entityTagEndIndex = fetchXml.IndexOf(entityTag) + entityTag.Length;
        return fetchXml.Substring(0, entityTagEndIndex) + selectAttributes + fetchXml.Substring(entityTagEndIndex);
    }

}`

Message: Test method Xrm.Tools.WebAPI.Test.GetListTest.GetListTest threw exception: System.AggregateException: One or more errors occurred. ---> Xrm.Tools.WebAPI.Results.CRMWebAPIException: HTML Error Content:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

404 - File or directory not found.

404 - File or directory not found.

The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.

davidyack commented 6 years ago

So if you just specify 1 column it works, but as you get larger numbers it fails? Are you able to get an http trace to see what your get looks like and if it is truncated?

crmdev1 commented 6 years ago

No truncation, it was going over the 2,083 character limit for http requests. I ended up adding the all-attributes tag to the fetchXML rather than specifying many columns.

GET /api/data/v8.2/accounts?fetchXml=%253Cfetch+version%253D%25271.0%2527+output-format%253D%2527xml-platform%2527+mapping%253D%2527logical%2527+distinct%253D%2527true%2527%253E%250D%250A++++++++++++++++++++++++++++++++++++++++++++++%253Centity+name%253D%2527account%2527%253E%253Cattribute+name%253D%2527accountid%2527%252F%253E%253Cattribute+name%253D%2527name%2527%252F%253E%253Cattribute+name%253D%2527emailaddress1%2527%252F%253E%253Cattribute+name%253D%2527websiteurl%2527%252F%253E%253Cattribute+name%253D%2527telephone1%2527%252F%253E%253Cattribute+name%253D%2527fax%2527%252F%253E%253Cattribute+name%253D%2527accountnumber%2527%252F%253E%253Cattribute+name%253D%2527address1_line1%2527%252F%253E%253Cattribute+name%253D%2527address1_line2%2527%252F%253E%253Cattribute+name%253D%2527address1_line3%2527%252F%253E%253Cattribute+name%253D%2527address1_city%2527%252F%253E%253Cattribute+name%253D%2527address1_postalcode%2527%252F%253E%253Cattribute+name%253D%2527address1_country%2527%252F%253E%253Cattribute+name%253D%2527address2_line1%2527%252F%253E%253Cattribute+name%253D%2527address2_line2%2527%252F%253E%253Cattribute+name%253D%2527address2_city%2527%252F%253E%253Cattribute+name%253D%2527address2_postalcode%2527%252F%253E%253Cattribute+name%253D%2527address2_country%2527%252F%253E%253Cattribute+name%253D%2527parentaccountid%2527%252F%253E%253Cattribute+name%253D%2527primarycontactid%2527%252F%253E%253Cattribute+name%253D%2527statecode%2527%252F%253E%253Cattribute+name%253D%2527statuscode%2527%252F%253E%253Cattribute+name%253D%2527telephone2%2527%252F%253E%250D%250A++++++++++++++++++++++++++++++++++++++++++++++++%253Cfilter+type%253D%2527and%2527%253E%250D%250A++++++++++++++++++++++++++++++++++++++++++++++++++%253Ccondition+attribute%253D%2527primarycontactid%2527+operator%253D%2527eq%2527+value%253D%252774eaeb77-e23b-e811-a9a7-000d3af3c361%2527+%252F%253E%250D%250A++++++++++++++++++++++++++++++++++++++++++++++++%253C%252Ffilter%253E%250D%250A++++++++++++++++++++++++++++++++++++++++++++++%253C%252Fentity%253E%250D%250A++++++++++++++++++++++++++++++++++++++++++++%253C%252Ffetch%253E HTTP/1.1