Search This Blog

Monday, February 20, 2012

Ax2012 Calling Query Service by using MetaDataService


Using MetaDataService and QueryService

Call the query from metadataService and Define the Range  by using MetaDataQuery  and define the ranges and assign to the queryMetaData and call ExecuteQuery by using QueryService client.

Before doing this sample change the table property of ValidaTimeStateFieldType to yes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using krishh_cosumeQueryService.QueryService;
using System.Data;
using krishh_cosumeQueryService.MetaDataService;
using System.Runtime.Serialization;
using System.IO;

namespace krishh_cosumeQueryService
{
   public class TestQueryMetaDataService
    {
        static void Main(string[] args)
        {
            DateTime startDate = new DateTime(2011, 01, 01);
            // converts the DateTime to UTC date Time
            startDate = startDate.ToUniversalTime();
            DateTime endDate = new DateTime(2012, 3, 31);
            // converts the DateTime to UTC date Time
            endDate = endDate.ToUniversalTime();
           
            DataSet dataSet;
            Paging paging = null;

            QueryServiceClient client = new QueryServiceClient();
            AxMetadataServiceClient metadataClient = new AxMetadataServiceClient();

            // create query object for QueryService
            QueryService.QueryMetadata queryServiceMetaData = new QueryService.QueryMetadata();
           
            // Create the query object for metadata
            MetaDataService.QueryMetadata queryMetadata = new MetaDataService.QueryMetadata();

            // Get the query from the AOT using the metadata service.
            queryMetadata = metadataClient.GetQueryMetadataByName(new[] { "Cust" })[0];

            // Set the date range on the query.
            queryMetadata.ValidTimeStateQueryType =
            MetaDataService.ValidTimeStateQueryType.Range;
            queryMetadata.ValidTimeStateValidFromDateTime =startDate;
            queryMetadata.ValidTimeStateValidToDateTime = endDate;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                DataContractSerializer mdsSerializer = new
                 DataContractSerializer(typeof(MetaDataService.QueryMetadata));
                DataContractSerializer qsSerializer = new
                  DataContractSerializer(typeof(QueryService.QueryMetadata));
                mdsSerializer.WriteObject(memoryStream, queryMetadata);
                memoryStream.Seek(0, SeekOrigin.Begin);
                queryServiceMetaData =  
               (QueryService.QueryMetadata)qsSerializer.ReadObject(memoryStream);

            }
            // Call the query service with the query retrieved
            // from the metadata service.
            dataSet = client.ExecuteQuery(queryServiceMetaData, ref paging);

            DataTable CustTable = new DataTable();
            CustTable = dataSet.Tables["CustTable"];

            // Loop through the TestEmployee table and display the results.
            if (CustTable != null && CustTable.Rows.Count > 0)
            {
                foreach (DataRow row in CustTable.Rows)
                {
                    Console.WriteLine(row["AccountNum"].ToString() +
                        "\t" + row["CustGroup"].ToString() + "\t" +
                        row["ValidFrom"].ToString() + "\t" +
                        row["ValidTo"].ToString());
                }
                Console.ReadLine();
            }

        }
    }
}

2 comments:

  1. Dear krishna,

    what is the security context when you call a service?

    By default the user calling is the user executing the process, right? How may I change it?

    Thanks,
    Ivan

    ReplyDelete
    Replies
    1. Hi Ivan,
      We can pass by assigning username,password,domain for client object, sample as follows
      client.ClientCredentials.Windows.ClientCredential.UserName = "test1";
      client.ClientCredentials.Windows.ClientCredential.Password = "ja2R8piPHZbF87n";
      client.ClientCredentials.Windows.ClientCredential.Domain = "domainName";
      client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

      I think this will help you to change the user.

      Delete

Thanks for visiting my blog,
I will reply for your comment within 48 hours.

Thanks,
krishna.