using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using System;
namespace CRM.Plugins.Helpers {
/// <summary>
/// Class that holds helper methods for microsoft dynamics CRM.
/// </summary>
public class CrmHelper {
/// <summary>
/// Function to share a record with a team.
/// </summary>
/// <param name="orgService">The organization service.</param>
/// <param name="recordId">The record id to share.</param>
/// <param name="recordEntityName">The record entity name.</param>
/// <param name="teamId">The team id.</param>
public static void SetTeamShareWithReadAccess(IOrganizationService orgService, Guid recordId, string recordEntityName, Guid teamId) {
var grantAccessRequest = new GrantAccessRequest {
Target = new EntityReference(recordEntityName, recordId),
PrincipalAccess = new PrincipalAccess {
Principal = new EntityReference("team", teamId),
// AccessMask = AccessRights.ReadAccess | AccessRights.WriteAccess | AccessRights.AppendAccess | AccessRights.AppendToAccess | AccessRights.DeleteAccess | AccessRights.ShareAccess
AccessMask = AccessRights.ReadAccess
}
};
orgService.Execute(grantAccessRequest);
}
/// <summary>
/// Function to revoke a team access.
/// </summary>
/// <param name="orgService">The organization service.</param>
/// <param name="recordId">The record id to share.</param>
/// <param name="recordEntityName">The record entity name.</param>
/// <param name="teamId">The team id.</param>
public static void RevokeTeamShare(IOrganizationService orgService, Guid recordId, string recordEntityName, Guid teamId) {
var revokeAccessRequest = new RevokeAccessRequest {
Target = new EntityReference(recordEntityName, recordId),
Revokee = new EntityReference("team", teamId)
};
orgService.Execute(revokeAccessRequest);
}
/// <summary>
/// Function to list the record shared team.
/// </summary>
/// <param name="orgService">The organization service.</param>
/// <param name="entityName">The entity name.</param>
/// <param name="recordId">The record id.</param>
/// <returns>A list of shared team.</returns>
public static PrincipalAccess[] GetSharedTeam(IOrganizationService orgService, string entityName, Guid recordId) {
var retrieveSharedRequest = new RetrieveSharedPrincipalsAndAccessRequest {
Target = new EntityReference(entityName, recordId)
};
var retrieveSharedResponse = (RetrieveSharedPrincipalsAndAccessResponse) orgService.Execute(retrieveSharedRequest);
return retrieveSharedResponse.PrincipalAccesses;
}
}
}
//Set the Web Resource properties
var wr = new WebResource
{
Content = getEncodedFileContents(@"../../" + webResource.path),
DisplayName = webResource.displayName,
Description = webResource.description,
Name = _customizationPrefix + webResource.name,
LogicalName = WebResource.EntityLogicalName,
WebResourceType = new OptionSetValue(Int32.Parse(webResource.type))
};
// Using CreateRequest because we want to add an optional parameter
var cr = new CreateRequest
{
Target = wr
};
//Set the SolutionUniqueName optional parameter so the Web Resources will be
// created in the context of a specific solution.
cr.Parameters.Add("SolutionUniqueName", _ImportWebResourcesSolutionUniqueName);
var cresp = (CreateResponse)_serviceProxy.Execute(cr);
string token;
// Initialize page number.
int pageNumber = 1;
List<Entity> initialrecords = new List<Entity>();
// Retrieve records by using Change Tracking feature.
var request = new RetrieveEntityChangesRequest();
request.EntityName = "account";
request.Columns = new ColumnSet("name");
request.PageInfo = new PagingInfo() { Count = 5000, PageNumber = 1, ReturnTotalRecordCount = false };
// Initial Synchronization. Retrieves all records as well as token value.
Console.WriteLine("Initial synchronization....retrieving all records.");
while (true)
{
var response = (RetrieveEntityChangesResponse)serviceProxy.Execute(request);
initialrecords.AddRange(response.EntityChanges.Changes.Select(x => (x as NewOrUpdatedItem).NewOrUpdatedEntity).ToArray());
initialrecords.ForEach(x => Console.WriteLine("initial record id:{0}", x.Id));
if (!response.EntityChanges.MoreRecords)
{
// Store token for later query
token = response.EntityChanges.DataToken;
break;
}
// Increment the page number to retrieve the next page.
request.PageInfo.PageNumber++;
// Set the paging cookie to the paging cookie returned from current results.
request.PageInfo.PagingCookie = response.EntityChanges.PagingCookie;
}