locked
Get all Guid in javascript crm 2011 RRS feed

  • Question

  • I need to get all guids in an Entity by using Javascript crm 2011?for Ex:I have the Entity called Contact.In that contact Entity ther is an 5000 records.I need to get all 5000 guid in a Javacript .

    How to do this?

    Tuesday, November 18, 2014 7:20 AM

All replies

  • you can use fetchxml in javascript to generate all the guid's of an entity.

    function guid(){
    var xml = "<?xml version='1.0' encoding='utf-8'?>" + 
        "<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\">" + 
        GenerateAuthenticationHeader() +
        "<soap:Body>" + 
        "<RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
        "<query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + 
        "<q1:EntityName>contact</q1:EntityName>" + 
        "<q1:ColumnSet xsi:type='q1:ColumnSet'>" + 
        "<q1:Attributes>" + 
        "<q1:Attribute>contactid</q1:Attribute>" +
        "</q1:Attributes>" + 
        "</q1:ColumnSet>" + 
        "<q1:Distinct>false</q1:Distinct>" + 
    
        "</query></RetrieveMultiple>" +
        "</soap:Body></soap:Envelope>";
    
    
        var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    
        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
        xmlHttpRequest.send(xml);
    
        var result = xmlHttpRequest.responseXML.xml;
        var doc = new ActiveXObject("MSXML2.DOMDocument");
        doc.async = false;
        doc.loadXML(result);
    
    }
    result varaiable will have all the records guid.


    Regards, Saad

    Tuesday, November 18, 2014 8:23 AM
  • saad,

    I need to form an Array with the contactid values from the result xml.How can i do that.

    Please refer the Link for more Details

    Wednesday, November 26, 2014 5:47 AM
  • Hi Vijay,

    Can you paste the result XML here?

    --

    Regards,

    Gopinath

    Wednesday, November 26, 2014 5:57 AM
  • Hi Gopi,

    This is my result xml

    I need to form the array with contains <q1:opportunityid> records
    Wednesday, November 26, 2014 6:01 AM
  • Please paste the text, so that I can try to parse it for you.

    --

    Regards,

    Gopinath

    Wednesday, November 26, 2014 6:03 AM
  • Hi Vijay,

    Please use this code 

    var arrayAnswers = [];
    var arr = doc.selectNodes("//q1:contactid"); 
        for(var i=0, len = arr.length; i < len; i++)
        {
    		
           arrayAnswers[i] = arr.nextNode.text;
    
        }
    	
    	
    	alert(arrayAnswers);


    Regards, Saad

    Thursday, November 27, 2014 5:53 AM
  • Saad,The alert shows as empty
    Thursday, November 27, 2014 7:09 AM
  • function guid(){

    var xml = "<?xml version='1.0' encoding='utf-8'?>" + 
        "<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\">" + 
        GenerateAuthenticationHeader() +
        "<soap:Body>" + 
        "<RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
        "<query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + 
        "<q1:EntityName>contact</q1:EntityName>" + 
        "<q1:ColumnSet xsi:type='q1:ColumnSet'>" + 
        "<q1:Attributes>" + 
        "<q1:Attribute>contactid</q1:Attribute>" +
        "</q1:Attributes>" + 
        "</q1:ColumnSet>" + 
        "<q1:Distinct>false</q1:Distinct>" + 

        "</query></RetrieveMultiple>" +
        "</soap:Body></soap:Envelope>";


        var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
        xmlHttpRequest.send(xml);

        var result = xmlHttpRequest.responseXML.xml;
        var doc = new ActiveXObject("MSXML2.DOMDocument");
        doc.async = false;
        doc.loadXML(result);

    var arrayAnswers = [];
    var arr = doc.selectNodes("//q1:contactid");
        for(var i=0, len = arr.length; i < len; i++)
        {

           arrayAnswers[i] = arr.nextNode.text;

        }


    alert(arrayAnswers);

    }


    Use the code as shown above. Please share your code so i can see what's wrong


    Regards, Saad


    • Edited by Mohd Saad Thursday, November 27, 2014 7:14 AM
    Thursday, November 27, 2014 7:12 AM
  • My code for select opportunity Id from Opportunity Entity like as contact

    function guid(){

    var xml = "<?xml version='1.0' encoding='utf-8'?>" + 
        "<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\">" + 
        GenerateAuthenticationHeader() +
        "<soap:Body>" + 
        "<RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
        "<query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + 
        "<q1:EntityName>opportunity</q1:EntityName>" + 
        "<q1:ColumnSet xsi:type='q1:ColumnSet'>" + 
        "<q1:Attributes>" + 
        "<q1:Attribute>opportunityid</q1:Attribute>" +
        "</q1:Attributes>" + 
        "</q1:ColumnSet>" + 
        "<q1:Distinct>false</q1:Distinct>" + 
        "</query></RetrieveMultiple>" +
        "</soap:Body></soap:Envelope>";


        var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
       // xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
        xmlHttpRequest.send(xml);

        var result = xmlHttpRequest.responseXML.xml;
        var doc = new ActiveXObject("MSXML2.DOMDocument");
        doc.async = false;
        doc.loadXML(result);

    var arrayAnswers = [];
    var arr = doc.selectNodes("//q1:contactid"); 
        for(var i=0, len = arr.length; i < len; i++)
        {

           arrayAnswers[i] = arr.nextNode.text;

        }


       }

    Thursday, November 27, 2014 7:21 AM
  • Got it Saad...Thanks Much For your Help..
    Thursday, November 27, 2014 7:24 AM
  • Hi,

    Change the tag in this line to the tag you are using in your xml.

    var arr = doc.selectNodes("//q1:contactid"); 
    
    change to 
    
    var arr = doc.selectNodes("//q1:opportunityid"); 



    Regards, Saad

    Thursday, November 27, 2014 7:25 AM
  • Mark it as answered so others can refer it too.

    Regards, Saad

    Thursday, November 27, 2014 7:28 AM
  • Try this code:

    var contacts=[];
    function GetContactIds() {
        //debugger;
        var odatapath = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/";
        var odataQuery = odatapath + "ContactSet?$select=ContactId";
        GetContacts(odataQuery, SetContactIds)
    }
    
    
    function GetContacts(odataQuery, successCallback) {
        //Asynchronous XMLHttpRequest to retrieve account record
        debugger;
        var req = new XMLHttpRequest();
        req.open("GET", encodeURI(odataQuery), true);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function () {
            //debugger;
            if (this.readyState == 4 /* complete */) {
                req.onreadystatechange = null; //avoids memory leaks
                if (this.status == 200) {
                    //parse the response string as a JSON object into the successCallback method.
                    successCallback(JSON.parse(this.responseText).d);
                }
                else {                
                }
            }
        };
        req.send();
    }
    
    function SetContactIds(contactset)
    {    
        //debugger;
        for (var i = 0; i < contactset.results.length; i++) {
            contacts[i] = contactset.results[i].ContactId;
        }    
        
    }

    Thanks


    Sachith Chandrasiri

    Friday, November 28, 2014 1:05 AM