Dynamics CRM 2011编程系列(57):使用Business Connectivity Services 集成Sharepoint 2010 (中)

  接着上文,我们来看看步骤2),步骤3)和步骤4)的实现。

BCS项目的创建

  Dynamics CRM 2011编程系列(57):使用Business Connectivity Services 集成Sharepoint 2010 (中)_第1张图片

图1

Dynamics CRM 2011编程系列(57):使用Business Connectivity Services 集成Sharepoint 2010 (中)_第2张图片

图2

Dynamics CRM 2011编程系列(57):使用Business Connectivity Services 集成Sharepoint 2010 (中)_第3张图片

图3

Dynamics CRM 2011编程系列(57):使用Business Connectivity Services 集成Sharepoint 2010 (中)_第4张图片

图4

Dynamics CRM 2011编程系列(57):使用Business Connectivity Services 集成Sharepoint 2010 (中)_第5张图片

图5

Dynamics CRM 2011编程系列(57):使用Business Connectivity Services 集成Sharepoint 2010 (中)_第6张图片

图6

 

BCS项目代码

    public partial class Entity1
    {
        //TODO: Implement additional properties here. The property Message is just a sample how a property could look like.
        public string Number { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Address { get; set; }
    }
}

 /// <summary>
    /// All the methods for retrieving, updating and deleting data are implemented in this class file.
    /// The samples below show the finder and specific finder method for Entity1.
    /// </summary>
    public class Entity1Service
    {
        private static CRMSVC.ICURD svc = CreateSVC();

        /// <summary>
        /// This is a sample specific finder method for Entity1.
        /// If you want to delete or rename the method think about changing the xml in the BDC model file as well.
        /// </summary>
        /// <param name="id"></param>
        /// <returns>Entity1</returns>
        public static Entity1 ReadItem(string id)
        {
            //TODO: This is just a sample. Replace this simple sample with valid code.
            CRMSVC.Account tmp = svc.GetAccount(id);
            return new Entity1() { Number=tmp.Number, Name=tmp.Name, Phone=tmp.PhoneNumber, Email=tmp.Email, Address=tmp.Address};
        }
        /// <summary>
        /// This is a sample finder method for Entity1.
        /// If you want to delete or rename the method think about changing the xml in the BDC model file as well.
        /// </summary>
        /// <returns>IEnumerable of Entities</returns>
        public static IEnumerable<Entity1> ReadList()
        {
            //TODO: This is just a sample. Replace this simple sample with valid code.
            List<Entity1> tmp=svc.GetAllAccount().Select<CRMSVC.Account, Entity1>(item => new Entity1() { Number = item.Number, Name = item.Name, Email=item.Email, Phone=item.PhoneNumber, Address=item.Address}).ToList();
           
            return tmp.ToArray();
           
        }

        private static CRMSVC.ICURD CreateSVC()
        {
            EndpointAddress endpoint = new EndpointAddress("http://your server name: your port number/CURD.svc");
            BasicHttpBinding binding = new BasicHttpBinding();
            ChannelFactory<CRMSVC.ICURD> svcFactory = new ChannelFactory<CRMSVC.ICURD>(binding, endpoint);
            return svcFactory.CreateChannel();
        }

        public static Entity1 CreateAccount(Entity1 InputAccount)
        {
            svc.CreateAccount(new CRMSVC.Account() { Number=InputAccount.Number, Name=InputAccount.Name, Address=InputAccount.Address, Email=InputAccount.Email, PhoneNumber=InputAccount.Phone});
            return InputAccount;
        }

        public static void Update(Entity1 InputAccount)
        {
            svc.UpdateAccount(new CRMSVC.Account() { Number = InputAccount.Number, Name = InputAccount.Name, Address = InputAccount.Address, Email = InputAccount.Email, PhoneNumber = InputAccount.Phone });
        }

        public static void Delete(Entity1 InputAccount)
        {
            svc.DeleteAccount(new CRMSVC.Account() { Number = InputAccount.Number, Name = InputAccount.Name, Address = InputAccount.Address, Email = InputAccount.Email, PhoneNumber = InputAccount.Phone });
        }

    }


小结

 1. BCS模型扩展的方法需要加上方法绑定(instances)。

 2. 输入参数与输出参数的typedescriptor需要与BCS实体的属性一致,否则同步后的数据将会丢失该属性的数据。

 

源码下载

 

你可能感兴趣的:(Dynamics CRM 2011编程系列(57):使用Business Connectivity Services 集成Sharepoint 2010 (中))