private FetchQuerySettings Parse(string fetchXml, string layoutXml)
        {
            FetchQuerySettings querySettings = new FetchQuerySettings();


            jQueryObject fetchXmlDOM  = jQuery.FromHtml("<query>" + fetchXml.Replace("{0}", "#Query#") + "</query>");
            jQueryObject fetchElement = fetchXmlDOM.Find("fetch");

            querySettings.FetchXml = fetchXmlDOM;

            ParseFetchXml(querySettings);

            querySettings.Columns = ParseLayoutXml(querySettings.RootEntity, layoutXml);

            return(querySettings);
        }
        public static string GetFetchXmlParentFilter(FetchQuerySettings query, string parentAttribute)
        {
            jQueryObject fetchElement = query.FetchXml.Find("fetch");

            fetchElement.Attribute("count", "{0}");
            fetchElement.Attribute("paging-cookie", "{1}");
            fetchElement.Attribute("page", "{2}");
            fetchElement.Attribute("returntotalrecordcount", "true");
            fetchElement.Attribute("distinct", "true");
            fetchElement.Attribute("no-lock", "true");
            jQueryObject orderByElement = fetchElement.Find("order");

            // Get the default order by field - currently only supports a single sort by column
            query.OrderByAttribute = orderByElement.GetAttribute("attribute");
            query.OrderByDesending = orderByElement.GetAttribute("descending") == "true";

            orderByElement.Remove();

            // Get the root filter (if there is one)
            jQueryObject filter = fetchElement.Find("entity>filter");

            if (filter != null)
            {
                // Check that it is an 'and' filter
                string filterType = filter.GetAttribute("type");
                if (filterType == "or")
                {
                    // wrap up in an and filter
                    jQueryObject andFilter = jQuery.FromHtml("<filter type='and'>" + filter.GetHtml() + "</filter>");

                    // remove existing filter
                    filter.Remove();

                    filter = andFilter;
                    // Add in the existing filter
                    fetchElement.Find("entity").Append(andFilter);
                }
            }

            // Add in the parent query filter
            jQueryObject parentFilter = jQuery.FromHtml("<condition attribute='" + parentAttribute + "' operator='eq' value='" + ParentRecordPlaceholder + "'/>");

            filter.Append(parentFilter);

            // Add the order by placeholder for the EntityDataViewModel
            return(query.FetchXml.GetHtml().Replace("</entity>", "{3}</entity>"));
        }
Esempio n. 3
0
        public ConnectionsViewModel(EntityReference parentRecordId, string[] connectToTypes, int pageSize, FetchQuerySettings view)
        {
            Connections = new EntityDataViewModel(pageSize, typeof(Connection), true);
            if (view != null)
            {
                _viewFetchXml = QueryParser.GetFetchXmlParentFilter(view, "record1id");
                // Set initial sort
                _defaultSortCol=new SortCol(view.OrderByAttribute, !view.OrderByDesending);
            }
           
            ParentRecordId.SetValue(parentRecordId);
         
            ObservableConnection connection = new ObservableConnection(connectToTypes);
            connection.Record2Id.SetValue(parentRecordId);
            ConnectionEdit = (Observable<ObservableConnection>)ValidatedObservableFactory.ValidatedObservable(connection);

            Connections.OnDataLoaded.Subscribe(Connections_OnDataLoaded);
            ConnectionEdit.GetValue().OnSaveComplete += ConnectionsViewModel_OnSaveComplete;
            ObservableConnection.RegisterValidation(Connections.ValidationBinder);
            AllowAddNew = Knockout.DependentObservable<bool>(AllowAddNewComputed);
        }
        private FetchQuerySettings Parse(string fetchXml, string layoutXml)
        {
            FetchQuerySettings querySettings = new FetchQuerySettings();

            //Quick find view features placeholders from {0} up to {4} based on attribute type.
            jQueryObject fetchXmlDOM = jQuery.FromHtml("<query>" + fetchXml
                                                       .Replace("{0}", "#Query#")
                                                       .Replace("{1}", "#QueryInt#")
                                                       .Replace("{2}", "#QueryCurrency#")
                                                       .Replace("{3}", "#QueryDateTime#")
                                                       .Replace("{4}", "#QueryFloat#")
                                                       + "</query>");
            jQueryObject fetchElement = fetchXmlDOM.Find("fetch");

            querySettings.FetchXml = fetchXmlDOM;

            ParseFetchXml(querySettings);

            querySettings.Columns = ParseLayoutXml(querySettings.RootEntity, layoutXml);

            return(querySettings);
        }
Esempio n. 5
0
        public static string GetFetchXmlParentFilter(FetchQuerySettings query, string parentAttribute)
        {
            
            jQueryObject fetchElement = query.FetchXml.Find("fetch");
            fetchElement.Attribute("count", "{0}");
            fetchElement.Attribute("paging-cookie", "{1}");
            fetchElement.Attribute("page", "{2}");
            fetchElement.Attribute("returntotalrecordcount", "true");
            fetchElement.Attribute("distinct", "true");
            fetchElement.Attribute("no-lock", "true");
            jQueryObject orderByElement = fetchElement.Find("order");
            // Get the default order by field - currently only supports a single sort by column
            query.OrderByAttribute = orderByElement.GetAttribute("attribute");
            query.OrderByDesending = orderByElement.GetAttribute("descending") == "true";

            orderByElement.Remove();

            // Get the root filter (if there is one)
            jQueryObject filter = fetchElement.Find("entity>filter");
           
            if (filter != null )
            {
                // Check that it is an 'and' filter
                string filterType = filter.GetAttribute("type");
                if (filterType == "or")
                {
                    // wrap up in an and filter
                    jQueryObject andFilter = jQuery.FromHtml("<filter type='and'>" + filter.GetHtml() + "</filter>");

                    // remove existing filter
                    filter.Remove();

                    filter = andFilter;
                    // Add in the existing filter
                    fetchElement.Find("entity").Append(andFilter);

                }
            }

            // Add in the parent query filter
            jQueryObject parentFilter = jQuery.FromHtml("<condition attribute='" + parentAttribute + "' operator='eq' value='" + ParentRecordPlaceholder + "'/>");
            filter.Append(parentFilter);

            // Add the order by placeholder for the EntityDataViewModel
            return query.FetchXml.GetHtml().Replace("</entity>", "{3}</entity>");
        }
Esempio n. 6
0
        private void ParseFetchXml(FetchQuerySettings querySettings)
        {
            jQueryObject fetchElement = querySettings.FetchXml;

            // Get the entities and link entities - only support 1 level deep
            jQueryObject entityElement = fetchElement.Find("entity");
            string logicalName = entityElement.GetAttribute("name");

            EntityQuery rootEntity;
            // Get query from cache or create new
            if (!EntityLookup.ContainsKey(logicalName))
            {
                rootEntity = new EntityQuery();
                rootEntity.LogicalName = logicalName;
                rootEntity.Attributes = new Dictionary<string, AttributeQuery>();
                EntityLookup[rootEntity.LogicalName] = rootEntity;
            }
            else
            {
                rootEntity = EntityLookup[logicalName];
            }

            // Get Linked Entities(1 deep)
            jQueryObject linkEntities = entityElement.Find("link-entity");
            linkEntities.Each(delegate(int index, Element element)
            {
                EntityQuery link = new EntityQuery();
                link.Attributes = new Dictionary<string, AttributeQuery>();
                link.AliasName = element.GetAttribute("alias").ToString();
                link.LogicalName =  element.GetAttribute("name").ToString();

                if (!EntityLookup.ContainsKey(link.LogicalName))
                {
                    EntityLookup[link.LogicalName] = link;
                }
                else
                {
                    string alias = link.AliasName;
                    link = EntityLookup[link.LogicalName];
                    link.AliasName = alias;
                }

                if (!AliasEntityLookup.ContainsKey(link.AliasName))
                {
                    AliasEntityLookup[link.AliasName] = link;
                }
            });

            querySettings.RootEntity = rootEntity;

            // Issue #35 - Add any lookup/picklist quick find fields that are not included in results attributes will cause a format execption
            // because we don't have the metadata - this means that 'name' is not appended to the attribute

            // Add the search string and adjust any lookup columns
            jQueryObject conditions = fetchElement.Find("filter[isquickfindfields='1']");
            conditions.First().Children().Each(delegate(int index, Element element)
            {
                logicalName = element.GetAttribute("attribute").ToString();
                jQueryObject e = jQuery.FromElement(element);
                jQueryObject p =e.Parents("link-entity");
                if (!querySettings.RootEntity.Attributes.ContainsKey(logicalName))
                {
                    AttributeQuery attribute = new AttributeQuery();
                    attribute.LogicalName = logicalName;
                    attribute.Columns = new List<Column>();
                    querySettings.RootEntity.Attributes[logicalName] = attribute;
                }
            });

        }
Esempio n. 7
0
        private FetchQuerySettings Parse(string fetchXml, string layoutXml)
        {
           
            FetchQuerySettings querySettings = new FetchQuerySettings();
            
           
            jQueryObject fetchXmlDOM = jQuery.FromHtml("<query>" + fetchXml.Replace("{0}", "#Query#") + "</query>");
            jQueryObject fetchElement = fetchXmlDOM.Find("fetch");
            querySettings.FetchXml = fetchXmlDOM;

            ParseFetchXml(querySettings);

            querySettings.Columns = ParseLayoutXml(querySettings.RootEntity, layoutXml);

            return querySettings;

        }
        private Promise GetViewDefinition(bool isQuickFind, string viewName)
        {
            // Get the Quick Find View for the entity
            string getviewfetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                              <entity name='savedquery'>
                                <attribute name='name' />
                                <attribute name='fetchxml' />
                                <attribute name='layoutxml' />
                                <attribute name='returnedtypecode' />
                                <filter type='and'>
                                <filter type='or'>";

            List <Promise> entityMetadata = new List <Promise>();


            foreach (string entity in Entities)
            {
                entityMetadata.Add(Utility.GetEntityMetadata(entity).Then(delegate(object metadata) {
                    return(Promise.Resolve(metadata));
                }));
            }
            Script.Literal("debugger");
            return(Promise.All(entityMetadata).Then(delegate(List <EntityMetadata> result)
            {
                Script.Literal("debugger");
                foreach (EntityMetadata metadata in result)
                {
                    getviewfetchXml += @"<condition attribute='returnedtypecode' operator='eq' value='" + metadata.ObjectTypeCode.ToString() + @"'/>";
                }
                return Promise.Resolve(true);
            }).Then(delegate(bool ok)
            {
                getviewfetchXml += @"</filter>";

                if (isQuickFind)
                {
                    getviewfetchXml += @"<condition attribute='isquickfindquery' operator='eq' value='1'/>
                                    <condition attribute='isdefault' operator='eq' value='1'/>";
                }
                else if (viewName != null && viewName.Length > 0)
                {
                    getviewfetchXml += @"<condition attribute='name' operator='eq' value='" + XmlHelper.Encode(viewName) + @"'/>";
                }
                else
                {
                    // Get default associated view
                    getviewfetchXml += @"<condition attribute='querytype' operator='eq' value='2'/>
                                    <condition attribute='isdefault' operator='eq' value='1'/>";
                }

                getviewfetchXml += @"</filter>
                              </entity>
                            </fetch>";
                // Get the Quick Find View
                EntityCollection quickFindQuery = OrganizationServiceProxy.RetrieveMultiple(getviewfetchXml);
                Dictionary <string, Entity> entityLookup = new Dictionary <string, Entity>();

                // Preseve the requested view order
                foreach (Entity view in quickFindQuery.Entities)
                {
                    entityLookup[view.GetAttributeValueString("returnedtypecode")] = view;
                }

                foreach (string typeName in Entities)
                {
                    Entity view = entityLookup[typeName];
                    string fetchXml = view.GetAttributeValueString("fetchxml");
                    string layoutXml = view.GetAttributeValueString("layoutxml");
                    EntityQuery query;
                    if (EntityLookup.ContainsKey(typeName))
                    {
                        query = EntityLookup[typeName];
                    }
                    else
                    {
                        query = new EntityQuery();
                        query.LogicalName = typeName;
                        query.Views = new Dictionary <string, FetchQuerySettings>();
                        query.Attributes = new Dictionary <string, AttributeQuery>();
                        EntityLookup[typeName] = query;
                    }

                    // Parse the fetch and layout to get the attributes and columns
                    FetchQuerySettings config = Parse(fetchXml, layoutXml);


                    query.Views[view.GetAttributeValueString("name")] = config;
                    if (isQuickFind)
                    {
                        query.QuickFindQuery = config;
                    }
                }
                return Promise.Resolve(true);
            }));


            //foreach (string entity in Entities)
            //{

            //    int? typeCode = (int?)Script.Literal("Mscrm.EntityPropUtil.EntityTypeName2CodeMap[{0}]", entity);

            //}
        }
        private void ParseFetchXml(FetchQuerySettings querySettings)
        {
            jQueryObject fetchElement = querySettings.FetchXml;

            // Get the entities and link entities - only support 1 level deep
            jQueryObject entityElement = fetchElement.Find("entity");
            string       logicalName   = entityElement.GetAttribute("name");

            EntityQuery rootEntity;

            // Get query from cache or create new
            if (!EntityLookup.ContainsKey(logicalName))
            {
                rootEntity             = new EntityQuery();
                rootEntity.LogicalName = logicalName;
                rootEntity.Attributes  = new Dictionary <string, AttributeQuery>();
                EntityLookup[rootEntity.LogicalName] = rootEntity;
            }
            else
            {
                rootEntity = EntityLookup[logicalName];
            }

            // Get Linked Entities(1 deep)
            jQueryObject linkEntities = entityElement.Find("link-entity");

            linkEntities.Each(delegate(int index, Element element)
            {
                EntityQuery link = new EntityQuery();
                link.Attributes  = new Dictionary <string, AttributeQuery>();
                link.AliasName   = element.GetAttribute("alias").ToString();
                link.LogicalName = element.GetAttribute("name").ToString();
                link.Views       = new Dictionary <string, FetchQuerySettings>();

                if (!EntityLookup.ContainsKey(link.LogicalName))
                {
                    EntityLookup[link.LogicalName] = link;
                }
                else
                {
                    string alias   = link.AliasName;
                    link           = EntityLookup[link.LogicalName];
                    link.AliasName = alias;
                }

                if (!AliasEntityLookup.ContainsKey(link.AliasName))
                {
                    AliasEntityLookup[link.AliasName] = link;
                }
            });

            querySettings.RootEntity = rootEntity;

            // Issue #35 - Add any lookup/picklist quick find fields that are not included in results attributes will cause a format execption
            // because we don't have the metadata - this means that 'name' is not appended to the attribute

            // Add the search string and adjust any lookup columns
            jQueryObject conditions = fetchElement.Find("filter[isquickfindfields='1']");

            conditions.First().Children().Each(delegate(int index, Element element)
            {
                logicalName    = element.GetAttribute("attribute").ToString();
                jQueryObject e = jQuery.FromElement(element);
                jQueryObject p = e.Parents("link-entity");
                if (!querySettings.RootEntity.Attributes.ContainsKey(logicalName))
                {
                    AttributeQuery attribute = new AttributeQuery();
                    attribute.LogicalName    = logicalName;
                    attribute.Columns        = new List <Column>();
                    querySettings.RootEntity.Attributes[logicalName] = attribute;
                }
            });
        }
Esempio n. 10
0
        private FetchQuerySettings Parse(string fetchXml, string layoutXml)
        {
            FetchQuerySettings querySettings = new FetchQuerySettings();

            //Quick find view features placeholders from {0} up to {4} based on attribute type.
            jQueryObject fetchXmlDOM = jQuery.FromHtml("<query>" + fetchXml
                    .Replace("{0}", "#Query#")
                    .Replace("{1}", "#QueryInt#")
                    .Replace("{2}", "#QueryCurrency#")
                    .Replace("{3}", "#QueryDateTime#")
                    .Replace("{4}", "#QueryFloat#")
                + "</query>");
            jQueryObject fetchElement = fetchXmlDOM.Find("fetch");
            querySettings.FetchXml = fetchXmlDOM;

            ParseFetchXml(querySettings);

            querySettings.Columns = ParseLayoutXml(querySettings.RootEntity, layoutXml);

            return querySettings;
        }