I have a table of project entities in CRM, each project entity has a text field called "project number". I want to query out a list of all the project numbers available in the table.
All of the sources that I have looked at, mention that I need to use a ServiceContext
or XrmServiceContext()
but it seems that those are generated using the CrmSvcUtil tool. The tutorial I used for this portion is found here.
From my past experience with CRM Plugin development, I have found that I am not allowed to do any local tasks within the plugin execution, therefore using the CrmSvcUtil tool conflicts with this.
Am I approaching this situation all wrong? I have access to OrganizationServiceContext
but I am not sure if this will give me access to query my project entities.
EDIT:
My references listed below but LocalPluginContext cannot be found. Quick google search suggested I just add the items from the sdk but I have added everything.
There are 2 Ways you could achieve This. 1. Console Applicaiton where you do not need context rather you sign in and then get IOrganizationService
static void Main(string[] args)
{
IOrganizationService organizationService = null;
try
{
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "AdminCRM@dabc.onmicrosoft.com";
clientCredentials.UserName.Password = "pwd";
//For Dynamics 365 Customer Engagement V9.X, set Security Protocol as TLS12
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
//Get the URL from CRM, Navigate to Settings -> Customizations -> Developer Resources
//Copy and Paste Organization Service Endpoint Address URL
organizationService = (IOrganizationService)new OrganizationServiceProxy(new Uri("https:/[OrgUrl]/XRMServices/2011/Organization.svc"),
null, clientCredentials, null);
if (organizationService != null)
{
Guid userid = ((WhoAmIResponse)organizationService.Execute(new WhoAmIRequest())).UserId;
if (userid != Guid.Empty)
{
Console.WriteLine("Connection Established Successfully...");
FetchXmlTestQuery(organizationService);
queryExpressionTest(organizationService);
}
}
else
{
Console.WriteLine("Failed to Established Connection!!!");
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught - " + ex.Message);
}
Console.ReadKey();
}
private static void queryExpressionTest(IOrganizationService organizationService)
{
QueryExpression qe = new QueryExpression();
qe.EntityName = "account";
qe.ColumnSet= new ColumnSet("name", "accountnumber");
EntityCollection coll = organizationService.RetrieveMultiple(qe);
foreach (Entity acunt in coll.Entities)
{
Console.WriteLine("Name of Account: " + acunt.GetAttributeValue<string>("name"));
Console.WriteLine("Number of Account: " + acunt.GetAttributeValue<string>("accountnumber"));
}
}
private static void FetchXmlTestQuery(IOrganizationService CrmConn)
{
// Retrieve all accounts owned by the user with read access rights to the accounts and
// where the last name of the user is not Cannon.
string fetch = @"
<fetch>
<entity name='account' >
<attribute name='name' />
<attribute name='accountnumber' />
<link-entity name='contact' from='parentcustomerid' to='accountid' link-type='inner' alias='Contact' >
<attribute name='fullname' alias = 'Contact.Fullname' />
</link-entity>
</entity>
</fetch> ";
EntityCollection Coll = CrmConn.RetrieveMultiple(new FetchExpression(fetch));
foreach (Entity acunt in Coll.Entities)
{
Console.WriteLine("Name of Account: " + acunt.GetAttributeValue<string>("name"));
Console.WriteLine("Name of Contact: " + acunt.GetAttributeValue<AliasedValue>("Contact.Fullname").Value);
Console.WriteLine("Number of Account: " + acunt.GetAttributeValue<string>("accountnumber"));
}
}
Now you could also use Plugin Context
protected override void ExecuteCrmPlugin(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
// TODO: Implement your custom plug-in business logic.
IPluginExecutionContext context = localContext.PluginExecutionContext;
ITracingService tracingService = localContext.TracingService;
IOrganizationService orgService = localContext.OrganizationService;
FetchXmlTestQuery(orgService);
queryExpressionTest(orgService);
}
private void FetchXmlTestQuery(IOrganizationService orgService)
{
// Retrieve all accounts owned by the user with read access rights to the accounts and
// where the last name of the user is not Cannon.
string fetch = @"
<fetch>
<entity name='account' >
<attribute name='name' />
<attribute name='accountnumber' />
<link-entity name='contact' from='parentcustomerid' to='accountid' link-type='inner' alias='Contact' >
<attribute name='fullname' alias = 'Contact.Fullname' />
</link-entity>
</entity>
</fetch> ";
EntityCollection Coll = orgService.RetrieveMultiple(new FetchExpression(fetch));
foreach (Entity acunt in Coll.Entities)
{
string accountname= acunt.GetAttributeValue<string>("name");
string accountnr= acunt.GetAttributeValue<string>("accountnumber");
}
}
private static void queryExpressionTest(IOrganizationService organizationService)
{
QueryExpression qe = new QueryExpression();
qe.EntityName = "account";
qe.ColumnSet = new ColumnSet("name", "accountnumber");
EntityCollection coll = organizationService.RetrieveMultiple(qe);
foreach (Entity acunt in coll.Entities)
{
string accountname = acunt.GetAttributeValue<string>("name");
string accountnr = acunt.GetAttributeValue<string>("accountnumber");
}
}