mscrm WebService开发 之 Crmservice

ExpandedBlockStart.gif 代码
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);
            
            
        }
    }
}

 

转载于:https://www.cnblogs.com/seerlin/archive/2010/06/11/1756476.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值