Thanks for this but we don't want to add any new tool we try to achieve this using plugin we writtena a code please review our code and help us to resolve this error.
=====================================
using Microsoft.Xrm.Sdk;
using System;
using ClosedXML.Excel;
using Microsoft.Xrm.Sdk.Query;
using System.Security;
using System.Security.Permissions;
namespace Test
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context and necessary services
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
// Retrieve the relevant audit log data using the CRM API or SDK queries
// Customize the query to retrieve the audit logs for the desired entity and any necessary filters
// Set the entity name for which you want to retrieve audit logs
string entityName = "opportunity";
// Set the GUID of the specific record for which you want to retrieve audit logs
Guid recordId = new Guid("344c4fbf-463a-4016-9eda-753fea8013a0");
// Create the query to retrieve audit logs
QueryExpression query = new QueryExpression("audit")
{
ColumnSet = new ColumnSet(true),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("objecttypecode", ConditionOperator.Equal, entityName),
new ConditionExpression("objectid", ConditionOperator.Equal, recordId)
}
},
Orders =
{
new OrderExpression("createdon", OrderType.Descending)
}
};
// Retrieve the audit logs
EntityCollection result = service.RetrieveMultiple(query);
// Process the retrieved audit logs as needed
foreach (Entity auditLog in result.Entities)
{
// Access the audit log attributes and perform any required actions
Guid auditLogId = auditLog.Id;
DateTime createdOn = (DateTime)auditLog["createdon"];
string action = auditLog.GetAttributeValue<OptionSetValue>("action").Value.ToString();
// Access more attributes as needed
}
// Continue with the rest of your export logic
// Your code to retrieve the audit logs goes here
// Create a new workbook and worksheet using ClosedXML
XLWorkbook workbook = new XLWorkbook();
{
PermissionSet permissions = new PermissionSet(PermissionState.Unrestricted);
var worksheet = workbook.Worksheets.Add("Audit Logs");
// Add headers
worksheet.Cell(1, 1).Value = "Audit Log ID";
worksheet.Cell(1, 2).Value = "Action";
worksheet.Cell(1, 3).Value = "Attribute";
// Add more headers as needed
// Add data to the worksheet
int row = 2;
foreach (var auditLog in result.Entities)
{
worksheet.Cell(row, 1).Value = auditLog.Id.ToString();
worksheet.Cell(row, 2).Value = auditLog.Attributes["ig1_projectnumber"].ToString();
worksheet.Cell(row, 3).Value = auditLog.Attributes["name"].ToString();
// Add more data fields as needed
row++;
}
// Save the workbook to a file
string filePath = "E:/Excel File/Test2.Xlsx";
workbook.SaveAs(filePath);
}
// Handle the exported Excel file as desired (e.g., store it, send it as an attachment, etc.)
// Optionally, perform any additional actions or logic based on the exported audit log data
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("Erron In Test Plugin " + ex);
}
}
}
}
==============================we debug this code and we get the error how we solve this error :
Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed