2012-08-29 191 views
0

如何在JavaScript中包含GenerateAuthenticationHeader()。我需要包含哪些必需的DLL?我使用這個函數來檢索使用JavaScript的MS CRM實體。我得到這個錯誤:CRM身份驗證問題

Microsoft JScript runtime error: 'GenerateAuthenticationHeader' is undefined

function AccessCRMWebServices() { 
     debugger; 
     var auth = GenerateAuthenticationHeader(); 
        var productCode = "ABCDE"; 
        var stock; 
        var x = Xrm.Page.getAuthenticationHeader(); 
        var fetchXml = '<fetch version="1.0" output-format="xml-platform" mapping="logical">'; 
        // Target Entity Name 
        fetchXml += '<entity name="new_aditya_products">'; 
        // Required Attribute 
        fetchXml += '<attribute name="new_productname"/>'; 
        fetchXml += '<attribute name="new_name"/>'; 
        // Condition 
        fetchXml += '<filter type="and">'; 
        fetchXml += '<condition attribute="new_productcode" operator="eq" value="' + productCode + '" />'; 
        fetchXml += '</filter>'; 

        fetchXml += '</entity>'; 
        fetchXml += '</fetch>'; 

        var Xml = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" 
        Xml += GenerateAuthenticationHeader() 
        Xml += "<soap:Body>"; 
        Xml += "<Fetch xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">"; 
        Xml += "<fetchXml>"; 
        //Xml += _HtmlEncode(fetchXml); 
        Xml += "</fetchXml>"; 
        Xml += "</Fetch>"; 
        Xml += "</soap:Body>"; 
        Xml += "</soap:Envelope>"; 

        var XmlHttp = CreateXmlHttp(); 

        XmlHttp.open("POST", 'http://rxdotnet:5555/MSCRMServices/2007/MetadataService.asmx', false); 
        XmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 
        XmlHttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch"); 
        XmlHttp.send(Xml); 
        var resultDoc = loadXmlDocument(XmlHttp.responseXML.text); 
        var resultRecords = resultDoc.selectNodes("//stockvolume"); 
        var resultnames = resultDoc.selectNodes("//new_name"); 
        if (resultRecords.length == 1) { 
         stock = resultRecords[0].text; 
         alert('Product ' + resultnames[0].text + ' Contains stock is -' + stock); 
        } 

回答

2

我假設你正在使用MS CRM 4.0版

GenerateAuthenticationHeader功能只能從表單代碼中本身(這是記錄在一個全局函數SDK)。我相信在任何其他環境中使用它都是不受支持的。這就是你得到錯誤的原因。

所有它是幹什麼的,按照該SDK是創建一個SOAP頭,看起來像這樣(所以也許你可以重新在你的代碼):

比這
<soap:Header> 
<CrmAuthenticationToken xmlns="http://schemas.microsoft.com/crm/2007/WebServices"> 
    <AuthenticationType xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes"> 
    0 
    </AuthenticationType> 
    <OrganizationName xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes"> 
    AdventureWorksCycle 
    </OrganizationName> 
    <CallerId xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes"> 
    00000000-0000-0000-0000-000000000000 
    </CallerId> 
</CrmAuthenticationToken> 
</soap:Header> 

其他有可能是另一種解決方案。你想做什麼,你的代碼在哪裏運行?

+0

我應該在哪裏包含上面的代碼.. ?? – Aditya

+0

我不可能在不知道「你在做什麼,你的代碼在哪裏運行?」的情況下回答這個問題。 ;-) –

+0

我的代碼在aspx頁面。其實我想通過JavaScript訪問所有的MS CRM實體,然後對來自javascript的實體執行CRUD操作。 – Aditya