zoukankan      html  css  js  c++  java
  • mscrm WebService开发 之 metadataService

    代码
    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;
            } 
        }
    }
  • 相关阅读:
    Android 自定义Dialog中加EditText弹不出键盘跟Dialog遮挡键盘的问题
    上周热点回顾(8.28-9.3)团队
    云计算之路-阿里云上-新车限行:新购服务器无法访问任何远程25端口团队
    上周热点回顾(8.21-8.27)团队
    云计算之路-阿里云上-容器难容:自建docker swarm集群遭遇无法解决的问题团队
    上周热点回顾(8.14-8.20)团队
    上周热点回顾(8.7-8.13)团队
    上周热点回顾(7.31-8.6)团队
    上周热点回顾(7.24-7.30)团队
    故障公告:docker swarm集群“群龙无首”造成部分站点无法访问团队
  • 原文地址:https://www.cnblogs.com/seerlin/p/1756459.html
Copyright © 2011-2022 走看看