Advertisement
Guest User

Untitled

a guest
Mar 28th, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. using Microsoft.Crm.Sdk.Messages;
  2. using Microsoft.Extensions.Configuration;
  3. using Microsoft.Xrm.Sdk;
  4. using Microsoft.Xrm.Sdk.Query;
  5. using Microsoft.Xrm.Tooling.Connector;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11.  
  12. namespace student5.CRM
  13. {
  14.  
  15. public class CrmService
  16. {
  17.  
  18. private IOrganizationService _orgService;
  19. public CrmServiceClient conn;
  20. public static IConfigurationRoot Configuration { get; set; }
  21.  
  22. public CrmService()
  23. {
  24. // Connect to the CRM web service using a connection string.
  25.  
  26. var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("secrets.json");
  27.  
  28. Configuration = builder.Build();
  29.  
  30. //var credentials = new System.Net.NetworkCredential(Configuration["crmcreds:username"], Configuration["crmcreds:password"], Configuration["crmcreds:domain"]);
  31. //conn = new CrmServiceClient(credentials,
  32. // "workspace-justice-local-dev.crm.egcs.fmt-tgf.com",
  33. // "443",
  34. // "workspace-justice-local-dev", useUniqueInstance: true, useSsl: true);
  35. //CrmServiceClient conn = new CrmServiceClient("acad_instruct@eperformanceinc.com", CrmServiceClient.MakeSecureString("Mayu6150"), String.Empty, "eperf-acad.crm.dynamics.com", useSsl:true, useUniqueInstance:false, isOffice365:true);
  36. //CrmServiceClient conn = new CrmServiceClient("Url = https://eperf-acad.crm.dynamics.com/; Username=acad_instruct@eperformanceinc.com; Password=Mayu6150; authtype=Office365");
  37. CrmServiceClient conn = new CrmServiceClient("Url = https://eperf-acad.crm.dynamics.com/; Username= " + Configuration["crmcreds:username"] + "; Password=" + Configuration["crmcreds:password"] + "; authtype=Office365");
  38. Console.WriteLine("Connection is ready????? " + conn.IsReady.ToString());
  39. _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
  40.  
  41.  
  42. // _orgService = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
  43.  
  44. //Create any entity records this sample requires.
  45. }
  46.  
  47. public IOrganizationService GetService()
  48. {
  49. IOrganizationService service = _orgService;
  50. return service;
  51. }
  52.  
  53. public static IOrganizationService GetServiceProvider()
  54. {
  55. var crmService = new CrmService();
  56. IOrganizationService service = crmService._orgService;
  57. return service;
  58. }
  59.  
  60. public static string GetTestUserInfo()
  61. {
  62. var crmService = new CrmService();
  63. IOrganizationService service = crmService._orgService;
  64. //// Obtain information about the logged on user from the web service.
  65. Guid userid = ((WhoAmIResponse)service.Execute(new WhoAmIRequest())).UserId;
  66. // Entity account = _orgService.Retrieve("Account", Guid.NewGuid(), new ColumnSet(new String[] { "accountid", "accountname" }));
  67. Entity systemUser = service.Retrieve("systemuser", userid,
  68. new ColumnSet(new string[] { "firstname", "lastname" }));
  69. var user = String.Format("Logged on user is {0} {1}.", systemUser.GetAttributeValue<string>("firstname"), systemUser.GetAttributeValue<string>("lastname"));
  70. //return conn.IsReady.ToString();
  71. return user;
  72.  
  73. }
  74.  
  75. public string GetTestUser()
  76. {
  77. //// Obtain information about the logged on user from the web service.
  78. Guid userid = ((WhoAmIResponse)_orgService.Execute(new WhoAmIRequest())).UserId;
  79. // Entity account = _orgService.Retrieve("Account", Guid.NewGuid(), new ColumnSet(new String[] { "accountid", "accountname" }));
  80. Entity systemUser = _orgService.Retrieve("systemuser", userid,
  81. new ColumnSet(new string[] { "firstname", "lastname" }));
  82. var user = String.Format("Logged on user is {0} {1}.", systemUser.GetAttributeValue<string>("firstname"), systemUser.GetAttributeValue<string>("lastname"));
  83. //return conn.IsReady.ToString();
  84. return user;
  85. // Cast the proxy client to the IOrganizationService interface.
  86. }
  87.  
  88. internal object RetrieveMultiple(QueryExpression qe)
  89. {
  90. throw new NotImplementedException();
  91. }
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement