mscrm WebService开发 之 metadataService

 

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  MetadataServiceTest.MetadataService;


namespace  MetadataServiceTest
{
    
///   <summary>
    
///  author:seer lin
    
///  description:use MetadataService to create entity schema and attribute
    
///   </summary>
     public   partial   class  Form1 : Form
    {
        
public  Form1()
        {
            InitializeComponent();
            ServiceSetup();
        }
        MetadataService.MetadataService metadataService 
=   null ;
        
private   void  button1_Click( object  sender, EventArgs e)
        {
            
// a new entity schema
            EntityMetadata entity  =   new  EntityMetadata();
            entity.SchemaName 
=   " new_testentity " ;
            
// entity.LogicalName = "new_testentity";
            entity.DisplayName  =  CreateCrmLabel( " Test Entity " , 2052 );
            entity.DisplayCollectionName 
=  CreateCrmLabel( " Test Entities " , 2052 );
            entity.Description 
=  CreateCrmLabel( " demo entity " , 2052 );
            entity.OwnershipType 
=   new  CrmOwnershipTypes();
            entity.OwnershipType.Value 
=  OwnershipTypes.UserOwned;
            
// entity.IsCustomEntity = new CrmBoolean();
            
// entity.IsCustomEntity.Value = true;
            entity.IsAvailableOffline  =   new  CrmBoolean();
            entity.IsAvailableOffline.Value 
=   true ;
            
// entity.IsActivity = new CrmBoolean();
            
// entity.IsActivity.Value = true;

           


            
// define attribute schema
            StringAttributeMetadata newName  =   new  StringAttributeMetadata();
            newName.SchemaName 
=   " new_name " ;
            newName.DisplayName 
=  CreateCrmLabel( " Name " , 2052 );
            newName.EntityLogicalName 
=   " new_testentity " ;
            newName.AttributeType 
=   new  CrmAttributeType();
            newName.AttributeType.Value 
=  AttributeType.String;
            newName.MaxLength 
=   new  CrmNumber();
            newName.MaxLength.Value 
=   15 ;
            newName.RequiredLevel 
=   new  CrmAttributeRequiredLevel();
            newName.RequiredLevel.Value 
=  AttributeRequiredLevel.Required;


            PicklistAttributeMetadata newAction 
=   new  PicklistAttributeMetadata();
            newAction.SchemaName 
=   " new_action " ;
            newAction.DisplayName 
=  CreateCrmLabel( " Action " 2052 );
            newAction.EntityLogicalName 
=   " new_testentity " ;
            newAction.AttributeType 
=   new  CrmAttributeType();
            newAction.AttributeType.Value 
=  AttributeType.Picklist;
            newAction.RequiredLevel 
=   new  CrmAttributeRequiredLevel();
            newAction.RequiredLevel.Value 
=  AttributeRequiredLevel.None;
            newAction.Options
=  CreateOptions( " create " , " select " , " modify " );

             
// create entity
            CreateEntityRequest createEntityRequest  =   new  CreateEntityRequest();
            createEntityRequest.Entity 
=  entity;
            createEntityRequest.HasActivities 
=   false ;
            createEntityRequest.HasNotes 
=   true ;
            createEntityRequest.PrimaryAttribute 
=  newName; // to create a new entity ,the primary attribute is required
            CreateEntityResponse response  =  (CreateEntityResponse)metadataService.Execute(createEntityRequest);
            
string  guid =  response.EntityId.Value.ToString();
            
// 6003cf19-2175-df11-9b67-0003ff779ab5,

           
            

            
// create attribute
            CreateAttributeRequest attributeRequest  =   new  CreateAttributeRequest();
            
// atrributeRequest.EntityName = "new_testentity";
            
// atrributeRequest.Attribute = newName;
            
// metadataService.Execute(atrributeRequest);

            
// attributeRequest = new CreateAttributeRequest();
            attributeRequest.EntityName  =   " new_testentity " ;
            attributeRequest.Attribute 
=  newAction;
            metadataService.Execute(attributeRequest);


        }
//
         ///   <summary>
        
///  setup service 
        
///   </summary>
         private   void  ServiceSetup()
        {
            
            CrmAuthenticationToken token 
=   new  CrmAuthenticationToken();
            token.OrganizationName 
=   " crmdev2 " ;

            metadataService 
=   new  MetadataService.MetadataService();
            metadataService.Url 
=   " http://localhost:5555/mscrmservices/2007/MetadataService.asmx " ;
            metadataService.Credentials 
=   new  System.Net.NetworkCredential( " administrator " "" " crm " );
            metadataService.CrmAuthenticationTokenValue 
=  token;
        }

        
///   <summary>
        
///  create a CrmLabel
        
///   </summary>
        
///   <param name="name"> label name </param>
        
///   <param name="langCode"> language code </param>
         private  CrmLabel CreateCrmLabel( string  labelText,  int  langCode) {
            CrmLabel crmLabel 
=   new  CrmLabel();

            LocLabel locLabel 
=   new  LocLabel();
            locLabel.Label 
=  labelText;
            locLabel.LanguageCode 
=   new  CrmNumber();
            locLabel.LanguageCode.Value 
=  langCode;

            crmLabel.LocLabels 
=   new  LocLabel[] { locLabel };
            
return  crmLabel;
        }

        
///   <summary>
        
///  create options for picklist etc.
        
///   </summary>
        
///   <param name="labels"> display labels </param>
        
///   <returns> options </returns>
         private  Option[] CreateOptions( params   object [] labels) {
            Option[] options
= new  Option[labels.Length];
            
for  ( int  i  =   0 ; i  <  options.Length;i ++  )
            {
                options[i] 
=   new  Option();
                options[i].Label 
=  CreateCrmLabel(labels[i].ToString(),  2052 );
                options[i].Value 
=   new  CrmNumber();
                options[i].Value.Value 
=  i + 1 ; // the value must  large than 0

            }
            
return  options;
        } 
    }
}

 

 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值