Разработка
13
Окт
0

Запрос данных аудита

Пример C# запроса (в консольном приложении) данных аудита (в частности аудита входа пользователей):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Runtime.Serialization;

namespace Audit
{
    class Program
    {
        static void Main(string[] args)
        {
            // Подключаемся к CRM
            ClientCredentials credentials = new ClientCredentials();
            credentials.Windows.ClientCredential = new System.Net.NetworkCredential("Administrator", "1qaz@WSX", "D2011");
            Uri uri = new Uri("http://win-ebu0m4kvf06/superfirma/XRMServices/2011/Organization.svc");
            OrganizationServiceProxy proxy = new OrganizationServiceProxy(uri, null, credentials, null);
            proxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
            IOrganizationService service = (IOrganizationService)proxy;

            QueryExpression query = new QueryExpression()
            {
                EntityName = "audit",
                ColumnSet = new ColumnSet(true),
                Criteria = new FilterExpression
                {
                    FilterOperator = LogicalOperator.And,
                    Conditions = 
                    {
                        new ConditionExpression
                        {
                            AttributeName = "operation",
                            Operator = ConditionOperator.Equal,
                            Values = { 4 }  // Операция доступа
                        },
                        new ConditionExpression
                        {
                            AttributeName = "objectid",
                            Operator = ConditionOperator.Equal,
                            Values = { "4801FAB6-0BE7-E211-93EB-000C29461FAD" }  // GUID Пользователя
                        }
                    }
                },
                Orders =
                {
                    new OrderExpression
                    {
                        AttributeName = "createdon",
                        OrderType = OrderType.Descending
                    }
                }
            };

            RetrieveMultipleRequest rmr = new RetrieveMultipleRequest();
            rmr.Query = query;

            RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(rmr);

            foreach (Entity entity in response.EntityCollection.Entities)
            {
                Console.WriteLine("Дата: " + entity.Attributes["createdon"]);                
            }

            Console.WriteLine("Press Enter");
            Console.ReadLine();
        }
    }
}


Комментарии (0)

*

code