Exemplo n.º 1
0
        public void Execute(IServiceProvider serviceProvider)
        {
            //Get the trace provider
            ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            //get the execution context;
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            //Get the service agent
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService        service = factory.CreateOrganizationService(context.UserId);

            try
            {
                tracer.Trace("Retrieve is called");
                tracer.Trace("Get retrieve Service");
                IEntityDataSourceRetrieverService retrieveService = (IEntityDataSourceRetrieverService)serviceProvider.Get <IEntityDataSourceRetrieverService>();
                Entity sourceEntity = retrieveService.RetrieveEntityDataSource();

                EntityReference target = (EntityReference)context.InputParameters["Target"];
                tracer.Trace("Target ID is : " + target.Id);

                string externalwebapiurl = sourceEntity.GetAttributeValue <string>("new_externalwebapiurl") + "/Students(" + target.Id + ")";
                tracer.Trace("Query URL: " + externalwebapiurl);

                //Getting Data from ODATA
                tracer.Trace("Begin to get data");
                var getAPIDataTask = Task.Run(async() => await HttpHelper.GetAPIdatabyID(externalwebapiurl));
                Task.WaitAll(getAPIDataTask);


                QueryResult row = getAPIDataTask.Result;

                Entity newrecord = new Entity("new_student");
                newrecord.Attributes.Add("new_studentid", row.StudentId);
                newrecord.Attributes.Add("new_name", row.name);
                newrecord.Attributes.Add("new_familyaddress", row.familyaddress);
                //newrecord.Attributes.Add("new_age", row.age);
                //newrecord.Attributes.Add("new_grade", row.grade);
                newrecord.Attributes.Add("new_phoneno", row.phoneno);
                context.OutputParameters["BusinessEntity"] = newrecord;
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
Exemplo n.º 2
0
        public void Execute(IServiceProvider serviceProvider)
        {
            //Get the trace provider
            ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            //get the execution context;
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));


            //Get the service agent
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService        service = factory.CreateOrganizationService(context.UserId);

            try
            {
                tracer.Trace("RetrieveMultiple is called");
                QueryExpression query   = context.InputParameterOrDefault <QueryExpression>("Query");
                SearchVisitor   visitor = new SearchVisitor();
                query.Accept(visitor);


                IEntityDataSourceRetrieverService retrieveService = (IEntityDataSourceRetrieverService)serviceProvider.Get <IEntityDataSourceRetrieverService>();

                Entity sourceEntity = retrieveService.RetrieveEntityDataSource();

                string externalwebapiurl;

                if (string.IsNullOrEmpty(visitor.SearchKeyWords))
                {
                    //Get the settings of Data source
                    externalwebapiurl = sourceEntity.GetAttributeValue <string>("new_externalwebapiurl") + "/Students";
                }
                else
                {
                    tracer.Trace("Search keywords is " + visitor.SearchKeyWords);
                    //The search URL wil be like this: http://winstonodata4.azurewebsites.net/Students?$filter=contains(name,'D')
                    externalwebapiurl = sourceEntity.GetAttributeValue <string>("new_externalwebapiurl") + "/Students?$filter=contains(name,'" + visitor.SearchKeyWords + "')";
                }

                tracer.Trace("external web api url: " + externalwebapiurl);

                EntityCollection results = new EntityCollection();
                results.EntityName = "new_student";

                //Getting Data from ODATA
                tracer.Trace("Begin to get data");
                var getAPIDataTask = Task.Run(async() => await HttpHelper.GetAPIdata(externalwebapiurl));
                Task.WaitAll(getAPIDataTask);

                OData odata = getAPIDataTask.Result;


                foreach (QueryResult row in odata.Value)
                {
                    Entity newrecord = new Entity("new_student");

                    newrecord.Attributes.Add("new_studentid", row.StudentId);
                    newrecord.Attributes.Add("new_name", row.name);
                    newrecord.Attributes.Add("new_familyaddress", row.familyaddress);
                    //newrecord.Attributes.Add("new_age", row.age);
                    //newrecord.Attributes.Add("new_grade", row.grade);
                    newrecord.Attributes.Add("new_phoneno", row.phoneno);

                    results.Entities.Add(newrecord);
                }

                context.OutputParameters["BusinessEntityCollection"] = results;
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }