mscrm js webservic 常用功能

var ORG_NAME="test";
var SERVICE_PATH="/mscrmservices/2007/CrmService.asmx"
var authenticationHeader = GenerateAuthenticationHeader();

function getCurrentUserName(){
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\">" + 
    authenticationHeader 
+ 
"  <soap:Body>" + 
"    <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
"      <Request xsi:type=\"RetrieveMultipleRequest\" ReturnDynamicEntities=\"true\">" + 
"        <Query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + 
"          <q1:EntityName>systemuser</q1:EntityName>" + 
"          <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + 
"            <q1:Attributes>" + 
"              <q1:Attribute>fullname</q1:Attribute>" + 
"            </q1:Attributes>" + 
"          </q1:ColumnSet>" + 
"          <q1:Distinct>false</q1:Distinct>" + 
"          <q1:Criteria>" + 
"            <q1:FilterOperator>And</q1:FilterOperator>" + 
"            <q1:Conditions>" + 
"              <q1:Condition>" + 
"                <q1:AttributeName>systemuserid</q1:AttributeName>" + 
"                <q1:Operator>EqualUserId</q1:Operator>" + 
"              </q1:Condition>" +              
"            </q1:Conditions>" + 
"          </q1:Criteria>" + 
"        </Query>" + 
"      </Request>" + 
"    </Execute>" + 
"  </soap:Body>" + 
"</soap:Envelope>" + 
"";

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

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

var resultXml = xmlHttpRequest.responseXML;

var doc = xmlHttpRequest.responseXML;

return resultXml.selectSingleNode("//Property[@Name='fullname']/Value").nodeTypedValue;

}


function retrieve(entityName,entityId,attributeName){
    
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\">" + 
        authenticationHeader
+
    
"  <soap:Body>" + 
    
"    <Retrieve xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
    
"      <entityName>"+entityName+"</entityName>" + 
    
"      <id>"+entityId+"</id>" + 
    
"      <columnSet xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:ColumnSet\">" + 
    
"        <q1:Attributes>" + 
    
"          <q1:Attribute>"+attributeName+"</q1:Attribute>" + 
    
"        </q1:Attributes>" + 
    
"      </columnSet>" + 
    
"    </Retrieve>" + 
    
"  </soap:Body>" + 
    
"</soap:Envelope>" + 
    
"";
    
    
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    
    xmlHttpRequest.Open(
"POST", SERVICE_PATH, false);
    xmlHttpRequest.setRequestHeader(
"SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
    xmlHttpRequest.setRequestHeader(
"Content-Type""text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader(
"Content-Length", xml.length);
    xmlHttpRequest.send(xml);
    
var resultXml = xmlHttpRequest.responseXML;
    
return resultXml;
}

function getAttribute(entityName,entityId,attribute){
    
var resultXml= retrieve(entityName,entityId,attribute);
    
    
//Check for errors.
    var errorCount = resultXml.selectNodes('//error').length;
    
if (errorCount != 0)
    {
     
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
     alert(msg);
    }
    
//Display the retrieved value.
    else
    {
    
return resultXml.selectSingleNode("//q1:"+attribute).nodeTypedValue;
    }
}


function retrieveByCondition(entityName,attribute_get,attributeName_con1,attributeValue_con1,attributeName_con2,attributeValue_con2){
    
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\">" + 
    authenticationHeader 
+ 
"  <soap:Body>" + 
"    <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
"      <Request xsi:type=\"RetrieveMultipleRequest\" ReturnDynamicEntities=\"true\">" + 
"        <Query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + 
"          <q1:EntityName>"+entityName+"</q1:EntityName>" + 
"          <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + 
"            <q1:Attributes>" + 
"              <q1:Attribute>"+attribute_get+"</q1:Attribute>" + 
"            </q1:Attributes>" + 
"          </q1:ColumnSet>" + 
"          <q1:Distinct>false</q1:Distinct>" + 
"          <q1:Criteria>" + 
"            <q1:FilterOperator>And</q1:FilterOperator>" + 
"            <q1:Conditions>" + 
"              <q1:Condition>" + 
"                <q1:AttributeName>"+attributeName_con1+"</q1:AttributeName>" + 
"                <q1:Operator>Equal</q1:Operator>" + 
"                <q1:Values>" + 
"                  <q1:Value xsi:type=\"xsd:string\">"+attributeValue_con1+"</q1:Value>" + 
"                </q1:Values>" + 
"              </q1:Condition>" + 
"              <q1:Condition>" + 
"                <q1:AttributeName>"+attributeName_con2+"</q1:AttributeName>" + 
"                <q1:Operator>Equal</q1:Operator>" + 
"                <q1:Values>" + 
"                  <q1:Value xsi:type=\"xsd:string\">"+attributeValue_con2+"</q1:Value>" + 
"                </q1:Values>" + 
"              </q1:Condition>" + 
"            </q1:Conditions>" + 
"          </q1:Criteria>" + 
"        </Query>" + 
"      </Request>" + 
"    </Execute>" + 
"  </soap:Body>" + 
"</soap:Envelope>" + 
"";

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

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

var resultXml = xmlHttpRequest.responseXML;
return resultXml;
}
function getAttributeByCondition(entityName,attribute_get,attributeName_con1,attributeValue_con1,attributeName_con2,attributeValue_con2){
    
var resultXml= retrieveByCondition(entityName,attribute_get,attributeName_con1,attributeValue_con1,attributeName_con2,attributeValue_con2);
    
    
//Check for errors.
    var errorCount = resultXml.selectNodes('//error').length;
    
if (errorCount != 0)
    {
     
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
     alert(msg);
    }
    
//Display the retrieved value.
    else
    {
    
//return resultXml;
    return resultXml.selectSingleNode("//Property[@Name='"+attribute_get+"']/Value").nodeTypedValue;
    }
}


//工作周报
var lookupItem= crmForm.all.new_projectweekreportid.DataValue;
 
if (lookupItem[0!= null)
{
  
//alert(lookupItem[0].name);
  var currentUserName=getCurrentUserName();
   
var weekReportId=lookupItem[0].id;
   
var projectId= getAttribute("new_projectweekreport",weekReportId,"new_project2id");
   
var memberId=getAttributeByCondition("new_prejectmember","new_prejectmemberid","new_project2id",projectId,"new_name",currentUserName);
   
if(memberId != null){
         
//Create an array to set as the DataValue for the lookup control.
            var lookupData = new Array();
        
//Create an Object add to the array.
           var lookupItem= new Object();
        
//Set the id, typename, and name properties to the object.
           lookupItem.id = memberId;
           lookupItem.name
=currentUserName;
           lookupItem.typename 
= 'new_prejectmember';
           
        
// Add the object to the array.
           lookupData[0= lookupItem;
        
// Set the value of the lookup field to the value of the array.
           crmForm.all.new_prejectmemberid.DataValue= lookupData;
          
       }
   }
}
原文地址:https://www.cnblogs.com/seerlin/p/2003351.html