Microsoft Dynamics CRM 2011 2013 外部网页对 CRM 的 增删改查 CRUD

【声明】本博文的参照资料:

1.Dynamics CRM 2011 编程系列(29):自定义页面(http://blog.csdn.net/ghostbear/article/details/7676222)

2.MS CRM 2011 如何从外部连接CRM - JF Zhu - 博客园 (http://www.cnblogs.com/jfzhu/archive/2012/11/02/2752006.html)


【需求描述】

客户需要在外部的网页上输入一些数据,然后把这些数据导入到CRM系统中。

【实现】

1.首先用做一个简易的ASP.NET页面


【代码如下】

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="iphonesales.aspx.cs" Inherits="iphonesales" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
    <table>
        <tr>
            <td><label>名前</label></td>
            <td>
                <asp:TextBox ID="inputname" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td><label>プロダクト名</label></td>
            <td>
                <asp:TextBox ID="inputproductname" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td><label>当日売上</label></td>
            <td>
                <asp:TextBox ID="inputtodaysalesvolume" runat="server"></asp:TextBox>
            </td>
        </tr>
    </table>
    
    <p>
        <asp:Button runat="server" id="commitbtn" onclick="commitbtn_Click" Width="100px" Text="button" />
    </p>
</form>
</body>
</html>

2.后台处理

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Description;

using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Crm.Sdk.Messages;

public partial class iphonesales : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }


    #region Class Level Members

    private OrganizationService _orgService;

    #endregion Class Level Members        

    #region getConnection String

    /// <summary>
    /// 連携テクストを取得
    /// </summary>
    /// <returns>連携テクスト</returns>
    private static String GetServiceConfiguration()
    {
        var connection = ConfigurationManager.ConnectionStrings["CrmConnection"];
        return connection.ConnectionString;
    }

    #endregion getConnection String

    #region public Method

    /// <summary>
    /// CRMにレコードを追加
    /// </summary>
    /// <param name="connectionString">連携テキスト</param>
    /// <param name="name">名前</param>
    /// <param name="productname">プロダクト名</param>
    /// <param name="salesvolume">当日売上</param>
    public void Run(String connectionString, string name, string productname, string salesvolume)
    {
        try
        {
            // CrmConnection CRMに連接する。
            Microsoft.Xrm.Client.CrmConnection connection = CrmConnection.Parse(connectionString);


            using (_orgService = new OrganizationService(connection))
            {
                Entity etnIphone = new Entity("new_iphone");

                etnIphone["new_name"] = name;

                // レコード追加
                Guid guidIphone = _orgService.Create(etnIphone);

                ColumnSet attributes = new ColumnSet(new string[] { "new_name", "ownerid", "new_productname", "new_todaysalesvolume" });

                etnIphone = _orgService.Retrieve("new_iphone", guidIphone, attributes);
                etnIphone["new_productname"] = productname;
                etnIphone["new_todaysalesvolume"] = salesvolume;
                // 追加したレコードを値修正
                _orgService.Update(etnIphone);

                MessageBox.Show("CRMに追加完了しました。");

            }
        }
        catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
        {
            throw;
        }
    }
    #endregion public Method



    protected void commitbtn_Click(object sender, EventArgs e)
    {
        // 画面上のデータを取得
        string name = this.inputname.Text;
        string productname = this.inputproductname.Text;
        string salesvolume = this.inputtodaysalesvolume.Text;

        try
        {
            // 連携テクストを取得
            String connectionString = GetServiceConfiguration();

            if (connectionString != null)
            {
                iphonesales app = new iphonesales();
                // CRMにレコードを追加
                app.Run(connectionString,name,productname,salesvolume);
            }
        }
        catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
        {
            Response.Write("Error!!");
            Response.Write("Timestamp:" + ex.Detail.Timestamp);
            Response.Write("Code:" + ex.Detail.ErrorCode);
            Response.Write("Message:" + ex.Detail.Message);
            Response.Write("Trace:" + ex.Detail.TraceText);
            if (ex.Detail.InnerFault == null)
            {
                Response.Write("Inner Fault: Has Inner Fault");
            }
            else
            {
                Response.Write("Inner Fault: No Inner Fault");
            }
        }
        catch (System.TimeoutException ex)
        {
            Response.Write("Error!!");
            Response.Write("Message:" + ex.Message);
            Response.Write("Stack Trace:" + ex.StackTrace);
            if (ex.InnerException.Message == null)
            {
                Response.Write("Inner Fault: Has Inner Fault");
            }
            else
            {
                Response.Write("Inner Fault: No Inner Fault");
            }
        }
        catch (System.Exception ex)
        {
            Response.Write("error");
            Response.Write(ex.Message);

            if (ex.InnerException != null)
            {
                Response.Write(ex.InnerException.Message);
                FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
                    as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>;
                if (fe != null)
                {
                    Response.Write("Timestamp:" + fe.Detail.Timestamp);
                    Response.Write("Code:" + fe.Detail.ErrorCode);
                    Response.Write("Message:" + fe.Detail.Message);
                    Response.Write("Trace:" + fe.Detail.TraceText);
                    if (fe.Detail.InnerFault == null)
                    {
                        Response.Write("Inner Fault: Has Inner Fault");
                    }
                    else
                    {
                        Response.Write("Inner Fault: No Inner Fault");
                    }
                }
            }
        }
    }

}

【wib.config】

<connectionStrings>
    <!--<add name="ApplicationServices"
         connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
         providerName="System.Data.SqlClient" /> -->
    <add name="CrmConnection" connectionString="Url=https://wicresoft670.crm5.dynamics.com; Username=365loginuser; Password=Password"/>
  </connectionStrings>



Microsoft Dynamics CRM 2011 2013 外部网页对 CRM 的 增删改查 CRUD,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。