Показать сообщение отдельно
Старый 19.04.2011, 16:31   #12  
Skyway is offline
Skyway
Участник
 
78 / 10 (1) +
Регистрация: 07.08.2009
Ну да, приложение, это была описка)
Пытаюсь тут разобраться с плагином. Хочу чтобы выполнялся хотябы простой запрос. Создал новое представление, в описании написал new_business_trip.all, поменял плагин следующим образом:
X++:
using System;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using System.Data.SqlClient;
using Microsoft.Crm.Sdk.Query;

namespace CustomQueryAPI
{
    public class QueryFetchPlugin : IPlugin
    {
        private string connectionString;
        IPluginExecutionContext pluginContext;

        public QueryFetchPlugin(string unsecure, string secure)
        {
            //"Data Source=CRMDEV;Initial Catalog=ICS_MSCRM;Integrated Security=SSPI"
            connectionString = secure;
        }

        public void Execute(IPluginExecutionContext context)
        {
            this.pluginContext = context;

            DynamicEntity query = (DynamicEntity)pluginContext.OutputParameters[ParameterName.BusinessEntity];
            string api = (string)query.Properties["description"];
            if (String.IsNullOrEmpty(api)) return;

            // Для систематизации и удобства наращивания api группируются в наборы ClassAPI.MetodAPI
            string[] paths = api.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            if (paths.Length < 2) return;



            string className = paths[0];
            string methodName = paths[1];

            string myFetch = String.Empty;

            switch (className)
            {
                case "New_business_trip":
                    myFetch = retrieveBusinessTrip(methodName);
                    break;
            }

            if (myFetch != null) query.Properties["fetchxml"] = myFetch;
        }

        private string queryExpressionToFetch(QueryBase query)
        {
            QueryExpressionToFetchXmlRequest request = new QueryExpressionToFetchXmlRequest();
            request.Query = query;

            ICrmService service = this.pluginContext.CreateCrmService(true);
            QueryExpressionToFetchXmlResponse responce = (QueryExpressionToFetchXmlResponse)service.Execute(request);

            return responce.FetchXml;
        }


        private string sqlToFetch(string entity, string primarykey, string sql)
        {
            string fetchPattern = @"<fetch version=""1.0"" output-format=""xml-platform"" mapping=""logical"" distinct=""false"">
                                            <entity name=""{0}"">
                                                <attribute name=""{1}""/>
                                                <filter type=""and"">
                                                    <condition attribute=""{1}"" operator=""in"">
                                                            {2}
                                                    </condition>
                                                </filter>
                                            </entity>
                                    </fetch>";

            string filterValues = String.Empty;

            using (SqlConnection sqlConnection = new SqlConnection(this.connectionString))
            {
                sqlConnection.Open();
                SqlCommand cmd = new SqlCommand(sql, sqlConnection);

                SqlDataReader reader = cmd.ExecuteReader();
                try
                {
                    while (reader.Read())
                    {
                        string id = ((Guid)reader[0]).ToString();
                        filterValues += "<value>" + id + "</value>\n";
                    }
                }
                finally
                {
                    reader.Close();
                }

            }
            return String.Format(fetchPattern, entity, primarykey, filterValues);
        }

 
        private string retrieveBusinessTrip(string methodName)
        {
            string fetch = String.Empty;
            switch (methodName)
            {
                case "all":
                    string sqlQuery = "SELECT new_business_tripid FROM new_business_trip Where new_name like 'г%'";
                    fetch = sqlToFetch("new_business_trip", "new_business_tripid", sqlQuery);
                    break;
            }
            return fetch;
        }

    }
}
Зарегистрировал Pre Stage, событие Retrive, сущность UserQuery.
Но почему-то ничего не происходит, такое впечатление что плагин не работает.