Using system;
Using system. LINQ;
Using Microsoft. xrm. SDK;
Using Microsoft. xrm. SDK. query;
Using Microsoft. CRM. SDK. messages;
Using Microsoft. xrm. SDK. messages;
Using system. Collections. Generic;
/// <Summary>
/// Review history
/// </Summary>
Public class audithistory
{
/// <Summary>
/// Retrieve all changes made to a specific attribute
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "target"> record object </param>
/// <Param name = "attrname"> Field name </param>
/// Special note: The field must be reviewed
Public void retrieveattributehistory (iorganizationservice service, entityreference target, string attrname)
{
Retrieveattributechangehistoryrequest request = new retrieveattributechangehistoryrequest ();
Request. attributelogicalname = attrname;
Request. Target = target;
Request. paginginfo = new paginginfo () {COUNT = 1000, pagenumber = 1 };
Retrieveattributechangehistoryresponse response = (retrieveattributechangehistoryresponse) service. Execute (request );
// Result
Auditdetailcollection = response. auditdetailcollection;
}
/// <Summary>
/// All changes made to a specific object
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "target"> Target record </param>
Public void retrieveentityhistory (iorganizationservice service, entityreference target)
{
Retrieverecordchangehistoryrequest request = new retrieverecordchangehistoryrequest ();
Request. Target = target;
Request. paginginfo = new paginginfo () {COUNT = 1000, pagenumber = 1 };
Retrieverecordchangehistoryresponse response = (retrieverecordchangehistoryresponse) service. Execute (request );
// Result
Auditdetailcollection = response. auditdetailcollection;
}
/// <Summary>
/// Delete all partitions that contain audit data created before the specified end date
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "enddate"> end date </param>
Public void deleteaudit (iorganizationservice service, datetime enddate)
{
Deleteauditdatarequest request = new deleteauditdatarequest ();
Request. enddate = enddate;
Deleteauditdataresponse response = (deleteauditdataresponse) service. Execute (request );
}
/// <Summary>
/// Delete audit records
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "auditdetailcollection"> record set </param>
Public void deleteaudit (iorganizationservice service, auditdetailcollection)
{
If (auditdetailcollection. auditdetails! = NULL & auditdetailcollection. auditdetails. Count> 0)
{
Executemultiple multiple = new executemultiple ();
Multiple. adddelete (auditdetailcollection. auditdetails. Select (A => A. auditrecord ));
Multiple. Execute (service );
}
}
/// <Summary>
/// Batch operation
/// </Summary>
Public class executemultiple
{
Private int Len = 1500;
Private list <executemultiplerequest> List = new list <executemultiplerequest> ();
Public executemultiple ()
{
New ();
}
Public void new ()
{
Executemultiplerequest multiple = new executemultiplerequest ()
{
Settings = new executemultiplesettings ()
{
Continueonerror = false,
Returnresponses = true
},
Requests = new organizationrequestcollection ()
};
List. Add (multiple );
}
Public void add (organizationrequest request)
{
Executemultiplerequest multiple = List. lastordefault ();
If (Multiple. Requests. Count> = Len)
{
New ();
Multiple = List. lastordefault ();
Multiple. Requests. Add (request );
}
Else
{
Multiple. Requests. Add (request );
}
}
Public void addcreate (entity en)
{
Createrequest request = new createrequest () {target = EN };
Add (request );
}
Public void addupdate (entity en)
{
Updaterequest request = new updaterequest () {target = EN };
Add (request );
}
Public void adddelete (entityreference ER)
{
Deleterequest request = new deleterequest () {target = ER };
Add (request );
}
Public void adddelete (entity en)
{
Adddelete (New entityreference () {logicalname = en. logicalname, id = en. ID });
}
Public void adddelete (entitycollection EC)
{
If (EC! = NULL & EC. Entities. Count> 0)
{
Adddelete (EC. Entities );
}
}
Public void adddelete (datacollection <entity> List)
{
If (list! = NULL & list. Count> 0)
{
Foreach (entity en in List)
{
Adddelete (en );
}
}
}
Public void adddelete (ienumerable <entity> List)
{
If (list! = NULL & list. Count ()> 0)
{
Foreach (entity en in List)
{
Adddelete (en );
}
}
}
Public void execute (iorganizationservice Service)
{
Foreach (executemultiplerequest request in List)
{
Service. Execute (request );
}
}
}
}