locked
MS CRM 2011 - Plugin for advanced find RRS feed

  • Question

  • Dear All,

    We want to implement a plugin for advanced find to identify who have downloaded records, how many records and for which entity. I have implemented the below code in order to achieve this and it is working perfectly except that I cannot retrieve how many records.

    Please let me know how to achieve this. I want to know many records the user downloaded using advanced find.

    ParameterCollection collection = context.InputParameters;
    if (current.Request.Path.Contains("/_grid/print/") && context.MessageName == "RetrieveMultiple" && collection["Query"] is QueryExpression)
                            {

    QueryExpression expression = (QueryExpression)collection["Query"];

    DateTime datetime = DateTime.Now.AddDays(0);
    Entity entity = new Entity("new_exportlog");
    entity.Attributes["new_name"] = expression.EntityName + " exported on " + datetime;
    entity.Attributes["new_entityexported"] = expression.EntityName;
    entity.Attributes["new_dateexported"] = datetime;

    entity.Attributes["new_username"] = new EntityReference("systemuser", context.InitiatingUserId);

     entity.Attributes["new_noofrecordsexported"] =???

    }

    Thanks in advance.

    Ravi


    Ravikumar

    Thursday, July 4, 2013 2:10 AM

All replies

  • Hi Ravi,

    If you want to actually record the number of records, you'll need to register on the Post RetrieveMultiple step.

    IPluginExecutionContext context = localContext.PluginExecutionContext;
    EntityCollection collection = (EntityCollection) localContext.PluginExecutionContext.OutputParameters["BusinessEntityCollection"];
    
    int recordCount = collection.Entities.Count;

    Be aware that this will create a performance hit since every query will cause a transaction lock to be taken.

    hth


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Thursday, July 4, 2013 7:40 AM
    Answerer
  • Another option would be to use the EntityCollection.TotalRecordCount property. However, this depends on setting the PagingInfo.ReturnTotalRecordCount property on the query, which you could do on a pre-event.

    The difference between the two approaches is that the Entities.Count property will return the number of records per page, whereas TotalRecordCount should be the total for the query


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Thursday, July 4, 2013 8:46 AM
    Moderator
  • Thanks Scott and David. I tried your code but I am getting error. I am not sure what mistake i am doing. Could you please help me in it? Thanks a lot.

    public void Execute(IServiceProvider serviceProvider)
            {

                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));


                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                               

                HttpContext current = HttpContext.Current;
                try
                {
                    if (context.Depth <= 1)
                    {
                        if (current != null)
                        {
                            string path = current.Request.Path;
                            ParameterCollection collection = context.InputParameters;

                            if (current.Request.Path.Contains("/_grid/print/") && context.MessageName == "RetrieveMultiple" && collection["Query"] is QueryExpression)
                            {

     

                                QueryExpression expression = (QueryExpression)collection["Query"];


                                LocalPluginContext localContext = new LocalPluginContext(serviceProvider);
                                EntityCollection collection1 = (EntityCollection)localContext.PluginExecutionContext.OutputParameters["BusinessEntityCollection"];

                                int recordCount = collection1.Entities.Count;


                                QueryExpression oThreshold = new QueryExpression();
                                oThreshold.EntityName = "new_customconfigurationparameters";
                                oThreshold.ColumnSet = new ColumnSet("new_exportlogthreshold");
                                EntityCollection oThresholdList = service.RetrieveMultiple(oThreshold);

     

                                    DateTime datetime = DateTime.Now.AddDays(0);

                                    Entity entity = new Entity("new_exportlog");
                                    entity.Attributes["new_name"] = expression.EntityName + " exported on " + datetime;
                                    entity.Attributes["new_entityexported"] = expression.EntityName;
                                    entity.Attributes["new_dateexported"] = datetime;
                                    entity.Attributes["new_noofrecordsexported"] = recordCount;
                                    entity.Attributes["new_username"] = new EntityReference("systemuser", context.InitiatingUserId);
                                    service.Create(entity);
                            }

                        }


    Ravikumar

    Monday, July 15, 2013 1:42 AM
  • Hi,

    What error are you getting - could you post the exception and stack trace?



    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Monday, July 15, 2013 8:02 AM
    Answerer