• mscrm WebService开发 之 Crmservice


    代码
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    using CrmWebServiceTest.CrmService;


    namespace CrmWebServiceTest
    {
        
    /// <summary>
        
    /// author:Seer lin
        
    /// description:demo of using CrmService to create,delete,update,retreive entity record
        
    /// </summary>
        public partial class Form1 : Form
        {
            
    public Form1()
            {
                InitializeComponent();
                ServiceSetUp();
            }

            
    private const string serviceUrl = "http://localhost:5555/MSCRMServices/2007/CrmService.asmx";
            CrmService.CrmService cs 
    = new CrmWebServiceTest.CrmService.CrmService();

            
    //create a new contact
            private void btnCreate_Click(object sender, EventArgs e)
            {
                contact contact 
    = new contact();
                contact.firstname 
    = "seer2";
                contact.lastname 
    = "lin";
                contact.address1_city 
    = "shan tou";
                contact.donotbulkemail 
    = new CrmBoolean();
                contact.donotbulkemail.Value 
    = true;

                Guid guid
    = cs.Create(contact);
                MessageBox.Show(
    "新增成功 "+guid.ToString());
            }

            
    // 设置webservice,例如验证凭证等
            private void ServiceSetUp()
            {
                
                CrmAuthenticationToken token 
    = new CrmAuthenticationToken();
                token.AuthenticationType 
    = 0;
                token.OrganizationName 
    = "crmdev2";

               
                cs.Url 
    = serviceUrl;
                cs.CrmAuthenticationTokenValue 
    = token;
                
    //cs.Credentials = System.Net.CredentialCache.DefaultCredentials;
                cs.Credentials = new System.Net.NetworkCredential("administrator","","crm");
     

            }
            
    //delete a contact by contactid
            private void btnDelete_Click(object sender, EventArgs e)
            {
                cs.Delete(
    "contact"new Guid("f08853d3-7673-df11-b781-0003ff779ab5"));
            }

            
    //retrieve some contacts by condition
            private void btnRetrieve_Click(object sender, EventArgs e)
            {
                ColumnSet cols 
    = new ColumnSet();
                cols.Attributes 
    = new string[] {"contactid","fullname","contactid"};

                ConditionExpression con1 
    = new ConditionExpression();
                con1.AttributeName 
    = "firstname";
                con1.Values 
    = new string[] { "seer2" };
                con1.Operator 
    = ConditionOperator.Like;

                ConditionExpression con2 
    = new ConditionExpression();
                con2.AttributeName 
    = "lastname";
                con2.Values 
    = new string[] { "lin"};
                con2.Operator 
    = ConditionOperator.Equal;

                FilterExpression filter 
    = new FilterExpression();
                filter.Conditions 
    = new ConditionExpression[] { con1,con2 };
                filter.FilterOperator 
    = LogicalOperator.And;
                
    //filter.Filters=...如果有有一些条件跟前面的查询逻辑不一样,
                
    //例如前面是and,如果其他条件是or,那就必须创建一个新的filter,再加到这个属性,相当于两个filter合再一起


                QueryExpression query 
    = new QueryExpression();
                query.EntityName 
    = EntityName.contact.ToString();
                query.ColumnSet 
    = cols;
                query.Criteria 
    = filter;
                

                query.PageInfo 
    = new PagingInfo();
                query.PageInfo.Count 
    = 2;//counts per page
                query.PageInfo.PageNumber = 1;//current page

                BusinessEntityCollection contacts
    = cs.RetrieveMultiple(query);

                
    if(contacts.BusinessEntities.Length>0){
                    
    foreach(BusinessEntity be in contacts.BusinessEntities ){
                         
    string guid = ((contact)be).contactid.Value.ToString();
                         MessageBox.Show(guid);
                    }
                }
            }

            
    private void btnDynamicEntity_Click(object sender, EventArgs e)
            {
                
    //create a string property for firstname field
                StringProperty spFirstName=new StringProperty();
                spFirstName.Name
    ="firstname";
                spFirstName.Value
    ="seer3";

                
    //create a key property for contactid,witch is a guid
                KeyProperty guid = new KeyProperty();
                guid.Name 
    = "contactid";
                guid.Value 
    = new Key();
                guid.Value.Value
    =new Guid("20124c71-7773-df11-b781-0003ff779ab5") ; 

                
    //create a new dynamicEntity
                DynamicEntity de = new DynamicEntity();
                de.Name 
    = EntityName.contact.ToString();
                de.Properties 
    = new Property[] { spFirstName, guid };

                
    //update a contact entity
                TargetUpdateDynamic updateTarget= new TargetUpdateDynamic();
                updateTarget.Entity 
    = de;

                UpdateRequest updateRequest 
    = new UpdateRequest();
                updateRequest.Target 
    = updateTarget;

                
    //Create a new contact
                
    //TargetCreateDynamic createTarget = new TargetCreateDynamic();
                
    //createTarget.Entity = de;

                
    //CreateRequest createRequest = new CreateRequest();
                
    //createRequest.Target = createTarget;

                
    //configure some option for create or update
              
    //  OptionalParameter op = new PersistInSyncOptionalParameter();            
                
    //request.OptionalParameters = new OptionalParameter[] {op };

                Response response 
    = cs.Execute(updateRequest);
                
                
            }
        }
    }
  • 相关阅读:
    apply()和call()的区别
    强制类型转换
    浮动理解
    清除浮动的方式
    五大主流浏览器及四大内核
    CSS引入方式
    js构建类的方法
    web前端与后端的理解区分
    Java的API及Object
    面向对象之this关键字
  • 原文地址:https://www.cnblogs.com/seerlin/p/1756476.html
Copyright © 2020-2023  润新知