Exemplo n.º 1
0
        public async Task <RetrieveMultipleResponse> RetrieveMultipleAsync(FetchXmlExpression fetchXml)
        {
            string entityCollection = WebApiMetadata.GetEntitySetName(fetchXml.LogicalName);
            var    retrieveOptions  = new RetrieveOptions {
                FetchXml = fetchXml
            };

            return(await RetrieveMultipleAsync(entityCollection, retrieveOptions));
        }
Exemplo n.º 2
0
        public async Task <IReadOnlyList <Entity> > GetFilteredListAsync(string entityCollection, string filter, params string[] properties)
        {
            try
            {
                await LoadEntityDefinitions();

                var options = new RetrieveOptions {
                    Select = properties, Filter = filter
                };
                var response = await this.client.RetrieveMultipleAsync(entityCollection, options).ConfigureAwait(false);

                return(response.Entities);
            }
            catch (WebApiException e)
            {
                throw new XrmException(e);
            }
        }
Exemplo n.º 3
0
        public async Task <RetrieveMultipleResponse> RetrieveMultipleAsync(string entityCollection, RetrieveOptions options)
        {
            string fullUrl = ApiUrl + entityCollection;

            fullUrl = options.GetRetrieveUrl(new Uri(fullUrl));
            var request = new HttpRequestMessage(new HttpMethod("GET"), fullUrl);

            foreach (string header in options.GetPreferList())
            {
                request.Headers.Add("Prefer", header);
            }

            HttpResponseMessage response = await Authorization.GetHttpClient().SendAsync(request);

            ResponseValidator.EnsureSuccessStatusCode(response);

            string data = await response.Content.ReadAsStringAsync();

            JObject result = JObject.Parse(data);
            var     retrieveMultipleResponse = new RetrieveMultipleResponse(result);

            while (!string.IsNullOrWhiteSpace(retrieveMultipleResponse.NextLink))
            {
                HttpResponseMessage nextResults = await Authorization.GetHttpClient().GetAsync(retrieveMultipleResponse.NextLink);

                ResponseValidator.EnsureSuccessStatusCode(nextResults);
                string nextData = await nextResults.Content.ReadAsStringAsync();

                JObject nextValues = JObject.Parse(nextData);
                retrieveMultipleResponse.AddResult(nextValues);
            }

            string           logicalName      = WebApiMetadata.GetLogicalName(entityCollection);
            EntityDefinition entityDefinition = WebApiMetadata.EntityDefinitions.FirstOrDefault(e => e.LogicalName == logicalName);
            string           primaryKey       = entityDefinition?.PrimaryIdAttribute;

            foreach (Entity entity in retrieveMultipleResponse.Entities)
            {
                if (entity.Contains(primaryKey))
                {
                    entity.Id = Guid.Parse(entity.GetAttributeValue <string>(primaryKey));
                }

                entity.LogicalName = logicalName;
            }

            return(retrieveMultipleResponse);
        }
Exemplo n.º 4
0
 public RetrieveMultipleResponse RetrieveMultiple(string entityCollection, RetrieveOptions options)
 {
     return(RetrieveMultipleAsync(entityCollection, options).GetAwaiter().GetResult());
 }
        public IReadOnlyCollection <string> GetVirtualMachineNames()
        {
            EnsureConnected();

            ServiceContent         serviceContent    = _connection.ServiceContent;
            ManagedObjectReference viewManager       = serviceContent.viewManager;
            ManagedObjectReference propertyCollector = serviceContent.propertyCollector;

            ManagedObjectReference containerView = _connection.Service.CreateContainerView(viewManager,
                                                                                           serviceContent.rootFolder, new[] { "VirtualMachine" }, true);

            TraversalSpec traversalSpec = new TraversalSpec()
            {
                name = "traverseEntities",
                type = "ContainerView",
                path = "view",
                skip = false
            };

            ObjectSpec objectSpec = new ObjectSpec
            {
                obj  = containerView,
                skip = true
            };

            objectSpec.selectSet = new SelectionSpec[] { traversalSpec };

            PropertySpec propertySpec = new PropertySpec()
            {
                type    = "VirtualMachine",
                pathSet = new [] { "name" }
            };

            PropertyFilterSpec filter = new PropertyFilterSpec()
            {
                objectSet = new[] { objectSpec },
                propSet   = new[] { propertySpec }
            };

            RetrieveOptions retrieveOptions = new RetrieveOptions();
            RetrieveResult  properties      = _connection.Service.RetrievePropertiesEx(propertyCollector, new[] { filter },
                                                                                       retrieveOptions);


            var virtualMachineNames = new List <string>();

            if (properties != null)
            {
                foreach (ObjectContent objectContent in properties.objects)
                {
                    DynamicProperty[] dynamicProperties = objectContent.propSet;
                    if (dynamicProperties != null)
                    {
                        foreach (DynamicProperty dynamicProperty in dynamicProperties)
                        {
                            string virtualMachineName = (string)dynamicProperty.val;
                            string path = dynamicProperty.name;
                            virtualMachineNames.Add(virtualMachineName);
                        }
                    }
                }
            }

            return(virtualMachineNames);
        }