Exemple #1
0
        internal string GetOData(int version)
        {
            try
            {
                if (Service == null || ConnectionDetail == null || ConnectionDetail.OrganizationDataServiceUrl == null)
                {
                    throw new Exception("Must have an active connection to CRM to compose OData query.");
                }
                var odata = string.Empty;
                switch (version)
                {
                case 2:
                    odata = ODataCodeGenerator.GetODataQuery(dockControlBuilder.GetFetchType(), ConnectionDetail.OrganizationDataServiceUrl, this);
                    break;

                case 4:
                    // Find correct WebAPI base url
                    var baseUrl = ConnectionDetail.WebApplicationUrl;
                    if (!baseUrl.EndsWith("/"))
                    {
                        baseUrl += "/";
                    }
                    var url       = new Uri(new Uri(baseUrl), $"api/data/v{ConnectionDetail.OrganizationMajorVersion}.{ConnectionDetail.OrganizationMinorVersion}");
                    var converter = new FetchXmlToWebAPIConverter(new WebAPIMetadataProvider(this), url.ToString());
                    odata = converter.ConvertFetchXmlToWebAPI(dockControlBuilder.GetFetchString(false, false));
                    break;
                }
                return(odata);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
        public static string Process(IOrganizationService serviceAdmin, IOrganizationService service, ITracingService tracing, string json)
        {
            var input  = SimpleJson.DeserializeObject <InputConvertFetchXmlToWebAPI>(json);
            var output = new OutputConvertFetchXmlToWebAPI {
                Select    = string.Empty,
                Expand    = string.Empty,
                Filter    = string.Empty,
                OrderBy   = string.Empty,
                FetchData = string.Empty
            };

            try
            {
                var converter = new FetchXmlToWebAPIConverter(new MetadataProvider(serviceAdmin), input.Url);
                //output.WebApiJs = converter.ConvertFetchXmlToWebAPI(input.FetchXml);
                var odata = converter.ConvertFetchXmlToWebAPI(input.FetchXml);
                if (odata.Select.Count > 0)
                {
                    output.Select = "$select=" + String.Join(",", odata.Select);
                }
                if (odata.Expand.Count > 0)
                {
                    output.Expand = "$expand=" + String.Join(",", odata.Expand.Select(e => $"{e.PropertyName}({e})"));
                }
                if (odata.Filter.Count > 0)
                {
                    output.Filter = "$filter=" + String.Join(" and ", odata.Filter);
                }
                if (odata.OrderBy.Count > 0)
                {
                    output.OrderBy = "$orderby=" + String.Join(",", odata.OrderBy);
                }

                output.FetchData = SimpleJson.SerializeObject(odata.FetchData);
            }
            catch (NotSupportedException e)
            {
            }
            catch (Exception e2)
            {
            }
            return(SimpleJson.SerializeObject(output));
        }
Exemple #3
0
        public static string GetWebApiUrl(ServiceClient DataverseClient, string query)
        {
            var url            = $"{DataverseClient.ConnectedOrgPublishedEndpoints[EndpointType.WebApplication]}api/data/v{DataverseClient.ConnectedOrgVersion.Major}.{DataverseClient.ConnectedOrgVersion.Minor}";
            var fetchXml       = XElement.Parse(query);
            var entityElement  = fetchXml.Element(FetchAttributes.Entity);
            var entityMetadata = new List <EntityMetadata>();
            var mainEntity     = DataverseClient.GetEntityMetadata(entityElement.Attribute(FetchAttributes.Name).Value, EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships);

            entityMetadata.Add(mainEntity);

            if (fetchXml.Descendants(FetchAttributes.LinkEntity).Any())
            {
                entityMetadata.AddRange(fetchXml.Descendants(FetchAttributes.LinkEntity).Select(x => DataverseClient.GetEntityMetadata(x.Attribute(FetchAttributes.Name).Value, EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships)).ToList());
            }

            var converter = new FetchXmlToWebAPIConverter(new LINQPadMetadataProvider(entityMetadata), url);
            var webApiUrl = converter.ConvertFetchXmlToWebAPI(query);

            new Hyperlinq(webApiUrl).Dump("WebAPI URL");
            return(webApiUrl);
        }
        private string ConvertFetchToOData(string fetch)
        {
            var context = new XrmFakedContext();

            // Add basic metadata
            var relationships = new[]
            {
                new OneToManyRelationshipMetadata
                {
                    SchemaName           = "contact_customer_accounts",
                    ReferencedEntity     = "account",
                    ReferencedAttribute  = "accountid",
                    ReferencingEntity    = "contact",
                    ReferencingAttribute = "parentcustomerid"
                },
                new OneToManyRelationshipMetadata
                {
                    SchemaName           = "account_primarycontact",
                    ReferencedEntity     = "contact",
                    ReferencedAttribute  = "contactid",
                    ReferencingEntity    = "account",
                    ReferencingAttribute = "primarycontactid"
                }
            };

            var entities = new[]
            {
                new EntityMetadata
                {
                    LogicalName   = "account",
                    EntitySetName = "accounts"
                },
                new EntityMetadata
                {
                    LogicalName   = "contact",
                    EntitySetName = "contacts"
                },
                new EntityMetadata
                {
                    LogicalName   = "connection",
                    EntitySetName = "connections"
                },
                new EntityMetadata
                {
                    LogicalName   = "webresource",
                    EntitySetName = "webresourceset"
                },
                new EntityMetadata
                {
                    LogicalName   = "stringmap",
                    EntitySetName = "stringmaps"
                },
                new EntityMetadata
                {
                    LogicalName   = "incident",
                    EntitySetName = "incidents"
                }
            };

            var attributes = new Dictionary <string, AttributeMetadata[]>
            {
                ["account"] = new AttributeMetadata[]
                {
                    new UniqueIdentifierAttributeMetadata
                    {
                        LogicalName = "accountid"
                    },
                    new StringAttributeMetadata
                    {
                        LogicalName = "name"
                    },
                    new StringAttributeMetadata
                    {
                        LogicalName = "websiteurl"
                    },
                    new LookupAttributeMetadata
                    {
                        LogicalName = "primarycontactid",
                        Targets     = new[] { "contact" }
                    }
                },
                ["contact"] = new AttributeMetadata[]
                {
                    new UniqueIdentifierAttributeMetadata
                    {
                        LogicalName = "contactid"
                    },
                    new StringAttributeMetadata
                    {
                        LogicalName = "firstname"
                    },
                    new LookupAttributeMetadata
                    {
                        LogicalName = "parentcustomerid",
                        Targets     = new[] { "account", "contact" }
                    },
                    new DateTimeAttributeMetadata
                    {
                        LogicalName = "createdon"
                    }
                },
                ["connection"] = new AttributeMetadata[]
                {
                    new UniqueIdentifierAttributeMetadata
                    {
                        LogicalName = "connectionid"
                    },
                    new PicklistAttributeMetadata
                    {
                        LogicalName = "record1objecttypecode"
                    }
                },
                ["incident"] = new AttributeMetadata[]
                {
                    new UniqueIdentifierAttributeMetadata
                    {
                        LogicalName = "incidentid"
                    }
                },
                ["stringmap"] = new AttributeMetadata[]
                {
                    new UniqueIdentifierAttributeMetadata
                    {
                        LogicalName = "stringmapid"
                    },
                    new EntityNameAttributeMetadata
                    {
                        LogicalName = "objecttypecode"
                    },
                    new StringAttributeMetadata
                    {
                        LogicalName = "attributename"
                    },
                    new IntegerAttributeMetadata
                    {
                        LogicalName = "attributevalue"
                    },
                    new StringAttributeMetadata
                    {
                        LogicalName = "value"
                    }
                },
                ["webresource"] = new AttributeMetadata[]
                {
                    new UniqueIdentifierAttributeMetadata
                    {
                        LogicalName = "webresourceid"
                    },
                    new StringAttributeMetadata
                    {
                        LogicalName = "name"
                    },
                    new ManagedPropertyAttributeMetadata
                    {
                        LogicalName = "iscustomizable"
                    }
                }
            };

            SetSealedProperty(attributes["webresource"].Single(a => a.LogicalName == "iscustomizable"), nameof(ManagedPropertyAttributeMetadata.ValueAttributeTypeCode), AttributeTypeCode.Boolean);
            SetRelationships(entities, relationships);
            SetAttributes(entities, attributes);
            SetSealedProperty(entities.Single(e => e.LogicalName == "incident"), nameof(EntityMetadata.ObjectTypeCode), 112);

            foreach (var entity in entities)
            {
                context.SetEntityMetadata(entity);
            }

            context.AddFakeMessageExecutor <RetrieveAllEntitiesRequest>(new RetrieveAllEntitiesRequestExecutor(entities));
            var org       = context.GetOrganizationService();
            var converter = new FetchXmlToWebAPIConverter(new MetadataProvider(org), $"https://example.crm.dynamics.com/api/data/v9.0");

            return(converter.ConvertFetchXmlToWebAPI(fetch));
        }