none
CRM2011怎么调用WebService创建记录? RRS feed

  • 问题

  • 我添加啦http://119.161.160.135:5555/crm/XRMServices/2011/Organization.svc引用,怎么初始化service 呢?然后调用呢
    CRM----BEST
    2011年5月4日 3:20

答案

  •       Uri orgServiceUri = new Uri("http://servername/OrgnizationName/XRMServices/2011/Organization.svc");
          ClientCredentials credentials = new ClientCredentials();
          credentials.Windows.ClientCredential = new System.Net.NetworkCredential("UserName", "******", "DomainName");
    
          OrganizationServiceProxy service = new OrganizationServiceProxy(orgServiceUri, null,credentials, null);
    
          
          WhoAmIRequest request = new WhoAmIRequest();
          WhoAmIResponse response = (WhoAmIResponse )servie.Execute(request);
    
    • 已标记为答案 王红福 2011年5月12日 1:54
    2011年5月10日 14:49

全部回复

  • 你是要在前段用javascript调用还是后端.net调用?
    2011年5月4日 17:31
  • 这个SDK上有。

    以silverlight application为例。

    首先需要一个helper class : ServerUtility.cs

     

     
     public static class ServerUtility
     {
     /// <summary>
     /// Returns the ServerUrl from Microsoft Dynamics CRM
     /// </summary>
     /// <returns>String representing the ServerUrl or String.Empty if not found.</returns>
     public static String GetServerUrl()
     {
      String serverUrl = String.Empty;
    
      //Try to get the ServerUrl from the Xrm.Page object
      serverUrl = GetServerUrlFromContext();
    
      //The trailing forward slash character from CRM Online needs to be removed.
      if (serverUrl.EndsWith("/"))
      {
      serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
      }
    
      return serverUrl;
     }
    
      /// <summary>
      /// Attempts to retrieve the ServerUrl from the Xrm.Page object
      /// </summary>
      /// <returns></returns>
      private static String GetServerUrlFromContext()
      {
       try
       {
        // If the Silverlight is in a form, this will get the server url
        ScriptObject xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
        ScriptObject page = (ScriptObject)xrm.GetProperty("Page");
        ScriptObject pageContext = (ScriptObject)page.GetProperty("context");
    
        String serverUrl = (String)pageContext.Invoke("getServerUrl");
    
        //The trailing forward slash character from CRM Online needs to be removed.
        if (serverUrl.EndsWith("/"))
        {
         serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
        }
    
        return serverUrl;
       }
       catch
       {
        return String.Empty;
       }
      }
    }
    

     

    然后在你的主程序里调用这个方法

     

    private SynchronizationContext _syncContext;
    private DataContext _context; //The organization context of your service
    private String _serverURL;
    
    public void MainMethod()
    {
     //Keep a reference to the UI thread
     _syncContext = SynchronizationContext.Current;
    
     _serverURL = ServerUtility.GetServerUrl();
    
      if (!String.IsNullOrEmpty(_serverURL))
      {
    
      //Setup Context
      _context = new PlaygroupVICContext(
       new Uri(String.Format("{0}/xrmservices/2011/organizationdata.svc/",
       _serverURL), UriKind.Absolute));
    
      //This is important because if the entity has new 
      //attributes added the code will fail.
      _context.IgnoreMissingProperties = true;
    
      //Do something here  }
      else
      {
      String errorMessage = "Unable to access server url. Launch this Silverlight Web Resource from a CRM Form OR host it in a valid HTML Web Resource with a <script src='../ClientGlobalContext.js.aspx' type='text/javascript'></script>";
      showErrorDetails(new Exception(errorMessage));
      }
    }
    

     



    • 已建议为答案 Jaimie_J 2011年5月5日 1:44
    2011年5月5日 1:43
  • 前台可以用JS静态调用,我不太熟悉用.net如何掉用,因为之前4.0的掉用需要先初始化一下,CRM2011中有点不一样

    // Set up the CRM Service. CRM4.0

    CrmAuthenticationToken token = new CrmAuthenticationToken();

    token.AuthenticationType = 0; token.OrganizationName = "AdventureWorksCycle";

    CrmService service = new CrmService();

    service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";

    service.CrmAuthenticationTokenValue = token;

    service.Credentials = System.Net.CredentialCache.DefaultCredentials;

    CRM2011中也需要这样初始化吗?


    CRM----BEST
    2011年5月5日 1:56
  • 你上面的代码 只能在silverlight application 上那样,我把代码粘贴到 .aspx页后有几个 类没有定义,

     ScriptObject xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");

      private DataContext _context; //The organization context of your service

    是需要引用什么吗?

    我想弄明白如果在普通的aspx页上怎么调用CRMService和用像CRM4.0那样掉吗,具体怎么掉呢应该,上代码吧呵呵


    CRM----BEST
    2011年5月5日 2:02
  •       Uri orgServiceUri = new Uri("http://servername/OrgnizationName/XRMServices/2011/Organization.svc");
          ClientCredentials credentials = new ClientCredentials();
          credentials.Windows.ClientCredential = new System.Net.NetworkCredential("UserName", "******", "DomainName");
    
          OrganizationServiceProxy service = new OrganizationServiceProxy(orgServiceUri, null,credentials, null);
    
          
          WhoAmIRequest request = new WhoAmIRequest();
          WhoAmIResponse response = (WhoAmIResponse )servie.Execute(request);
    
    • 已标记为答案 王红福 2011年5月12日 1:54
    2011年5月10日 14:49
  •   OrganizationServiceProxy service = new OrganizationServiceProxy(orgServiceUri, null,credentials, null);

    你写的两个NULL的参数在什么时候用啊?


    CRM----BEST
    2011年5月12日 1:55