登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

忆风的博客

http://www.cnblogs.com/dhjdhja

 
 
 

日志

 
 
 
 

获得CRMService  

2009-09-21 17:12:11|  分类: CRM学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Win32;
using Microsoft.Crm.Sdk.Query;

public CrmService getService()
    {
        string orgname;
        string crmurl;
        string metaurl;
        bool offline;

        #region CRM URLs and Organization Name
        //Determine Offline State from Host Name
        //Response.Write(Request.Url.Host.ToString());
        if (Request.Url.Host.ToString() == "127.0.0.1")
        {
            offline = true;

            //Retrieve the Port and OrgName from the Registry
            RegistryKey regkey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\MSCRMClient");
            orgname = regkey.GetValue("ClientAuthOrganizationName").ToString();
            string portnumber = regkey.GetValue("CassiniPort").ToString();

            //Construct the URLs
            string baseurl = "http://localhost:" + portnumber + "/mscrmservices/2007/";
            crmurl = baseurl + "crmservice.asmx";
            metaurl = baseurl + "metadataservice.asmx";
        }
        else
        {
            offline = false;

            //Retrieve the URLs from the Registry
            RegistryKey regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM");
            string ServerUrl = regkey.GetValue("ServerUrl").ToString();
            crmurl = ServerUrl + "/2007/crmservice.asmx";
            metaurl = ServerUrl + "/2007/metadataservice.asmx";

            //Retrieve the Query String from the current URL
            if (Request.QueryString["orgname"] == null)
            {
                orgname = string.Empty;
            }
            else
            {
                //Query String
                string orgquerystring = Request.QueryString["orgname"].ToString();
                if (string.IsNullOrEmpty(orgquerystring))
                {
                    orgname = string.Empty;
                }
                else
                {
                    orgname = orgquerystring;
                }
            }

            if (string.IsNullOrEmpty(orgname))
            {
                //Windows Auth URL
                if (Request.Url.Segments[2].TrimEnd('/').ToLower() == "isv")
                {
                    orgname = Request.Url.Segments[1].TrimEnd('/').ToLower();
                }

                //IFD URL
                if (string.IsNullOrEmpty(orgname))
                {
                    string url = Request.Url.ToString().ToLower();
                    int start = url.IndexOf("://") + 3;
                    orgname = url.Substring(start, url.IndexOf(".") - start);
                }
            }
        }

        #endregion

        //using (new CrmImpersonator())
        //{
        CrmAuthenticationToken token;
        if (offline == true)
        {
            token = new CrmAuthenticationToken();
        }
        else
        {
            // Notice that the Context parameter value is Page.Context.
            token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgname);
        }
        token.OrganizationName = orgname;
        token.AuthenticationType = 0;

        //Create the Service
        CrmService service = new CrmService();
        service.Credentials = System.Net.CredentialCache.DefaultCredentials;
        service.CrmAuthenticationTokenValue = token;
        service.Url = crmurl;

        //}
        // This code shows how to create the metadata service.
        // It is not used in this sample.
        // MetadataService meta = new MetadataService();
        // meta.CrmAuthenticationTokenValue = token;
        // meta.Credentials = CredentialCache.DefaultCredentials;
        // meta.Url = "http://localhost/mscrmservices/2007/MetadataService.asmx";

        return service;

    }

 

添加web引用

private localhost.CrmService GetCrmService(string organizationName)
{
            // Setup the Authentication Token
            localhost.CrmAuthenticationToken token = new localhost.CrmAuthenticationToken();
            token.OrganizationName = organizationName;


            localhost.CrmService crmService = new localhost.CrmService();
            crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
            crmService.CrmAuthenticationTokenValue = token;


            return crmService;
}

 public void Execute(IPluginExecutionContext context)
{
            localhost.CrmService crmService = GetCrmService(context.OrganizationName);
}

  评论这张
 
阅读(744)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018