Friday, March 16, 2012

Basic connection for CRM2011 sdk on premise

Private Function getCRM(ByVal userauth as Boolean, Optional ByVal userid as String = nothing) As IOrganizationService
        Dim cred As New ClientCredentials()
   if userauth then
     cred.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials
   else
     cred.Windows.ClientCredential = New System.Net.NetworkCredential("username", "password", "domain")
   end if
     Dim org As New Uri("http://crm/orgname/xrmservices/2011/organization.svc")
     Dim proxy As New OrganizationServiceProxy(org, Nothing, cred, Nothing)
     proxy.EnableProxyTypes()
     if not userid is nothing then proxy.callerid = new guid(userid)
     Dim orgservice As IOrganizationService = proxy
     Return orgservice
    End Function

An important note about this approach is that if userauth is false, and a userid is not supplied or the userid is not actually present in the crm system thetyped credentials will be used.  In other words even an incorrect userid will succeed.  The call will just not be made as the intended user.

No comments:

Post a Comment