Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\CrmCodeGenerator.sln",
"PreviewInSolutionExplorer": false
}
Binary file added .vs/slnx.sqlite
Binary file not shown.
3 changes: 1 addition & 2 deletions CrmCodeGenerator.VSPackage/CrmCodeGenerator.VSPackage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Microsoft.VisualStudio.OLE.Interop" />
<Reference Include="Microsoft.VisualStudio.Shell.12.0, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.Shell.12.0, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.Shell.Framework, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.Shell.Immutable.10.0, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.Shell.Interop" />
Expand Down Expand Up @@ -325,5 +325,4 @@
<Target Name="AfterBuild">
</Target>
-->

</Project>
56 changes: 49 additions & 7 deletions CrmCodeGenerator.VSPackage/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ protected void OnMessage(string message, string extendedMessage = "")

public Context MapContext()
{
var connection = GetConnection();
var context = new Context();
context.Entities = GetEntities(GetConnection());
context.Entities = GetEntities(connection);
SortEntities(context);
context.Enums = GetGlobalEnums(connection);
return context;
}

Expand Down Expand Up @@ -93,7 +95,7 @@ public void SortEntities(Context context)

internal MappingEntity[] GetEntities(IOrganizationService service)
{
var entities = GetMetadataFromServer(service);
var entities = GetEntitiesMetadata(service);

var selectedEntities = entities
.Where(r => this.Settings.EntitiesSelected.Contains(r.LogicalName))
Expand Down Expand Up @@ -135,21 +137,30 @@ internal MappingEntity[] GetEntities(IOrganizationService service)
return mappedEntities.ToArray();
}

private EntityMetadata[] GetMetadataFromServer(IOrganizationService service)
internal MappingEnum[] GetGlobalEnums(IOrganizationService service)
{
var enums = RetrieveGlobalOptionSetsMetadataFromServer(service);

OnMessage(string.Format("Found {0} option sets", enums.Count()));

var mappedEnums = enums.Select(e => MappingEnum.Parse(e)).OrderBy(e => e.DisplayName).ToList();

return mappedEnums.ToArray();
}

private EntityMetadata[] GetEntitiesMetadata(IOrganizationService service)
{
OnMessage("Gathering metadata, this may take a few minutes...");
if (this.Settings.EntitiesSelected.Count > 20)
{
return GetAllMetadataFromServer(service);
return RetrieveEntityMetadataFromServer(service);
}

var entitiesToRetreive = this.Settings.EntitiesSelected.Select(x => x).ToList();
if (!entitiesToRetreive.Any(x => x.Equals("activityparty")))
{
entitiesToRetreive.Add("activityparty");
}



var results = new List<EntityMetadata>();
foreach (var entity in entitiesToRetreive)
Expand All @@ -164,7 +175,7 @@ private EntityMetadata[] GetMetadataFromServer(IOrganizationService service)
return results.ToArray();
}

private EntityMetadata[] GetAllMetadataFromServer(IOrganizationService service)
private EntityMetadata[] RetrieveEntityMetadataFromServer(IOrganizationService service)
{
//TODO should change this to early binding RetrieveAllEntitiesRequest
OrganizationRequest request = new OrganizationRequest("RetrieveAllEntities");
Expand All @@ -176,6 +187,37 @@ private EntityMetadata[] GetAllMetadataFromServer(IOrganizationService service)
var entities = results["EntityMetadata"] as EntityMetadata[];
return entities;
}
private OptionSetMetadata[] RetrieveGlobalOptionSetsMetadataFromServer(IOrganizationService service)
{
var results = new List<OptionSetMetadata>();
// Use RetrieveAllOptionSetsRequest to retrieve all global option sets.
// Create the request.
RetrieveAllOptionSetsRequest retrieveAllOptionSetsRequest = new RetrieveAllOptionSetsRequest();

// Execute the request
RetrieveAllOptionSetsResponse retrieveAllOptionSetsResponse = (RetrieveAllOptionSetsResponse)service.Execute(retrieveAllOptionSetsRequest);

//return retrieveAllOptionSetsResponse.OptionSetMetadata;
// Now you can use RetrieveAllOptionSetsResponse.OptionSetMetadata property to
// work with all retrieved option sets.
if (retrieveAllOptionSetsResponse.OptionSetMetadata.Count() > 0)
{
Console.WriteLine("All the global option sets retrieved as below:");
foreach (OptionSetMetadataBase optionSetMetadata in retrieveAllOptionSetsResponse.OptionSetMetadata)
{
if (!optionSetMetadata.IsManaged.Value && optionSetMetadata.IsGlobal.Value)
{
RetrieveOptionSetRequest retrieveOptionSetRequest = new RetrieveOptionSetRequest { Name = optionSetMetadata.Name };
// Execute the request.
RetrieveOptionSetResponse retrieveOptionSetResponse = (RetrieveOptionSetResponse)service.Execute(retrieveOptionSetRequest);
results.Add((OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata);
}
}
}

return results.ToArray();
}

private static void ExcludeRelationshipsNotIncluded(List<MappingEntity> mappedEntities)
{
foreach (var ent in mappedEntities)
Expand Down
2 changes: 2 additions & 0 deletions CrmCodeGenerator.VSPackage/Model/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public class Context
public string Namespace { get; set; }

public MappingEntity[] Entities { get; set; }

public MappingEnum[] Enums { get; set; }
}
}
26 changes: 26 additions & 0 deletions CrmCodeGenerator.VSPackage/Model/MappingEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public static MappingEnum Parse(object attribute )
if (attribute is BooleanAttributeMetadata)
return Parse(attribute as BooleanAttributeMetadata);

if (attribute is OptionSetMetadata)
return Parse(attribute as OptionSetMetadata);

return null;
}

Expand Down Expand Up @@ -58,6 +61,29 @@ public static MappingEnum Parse(BooleanAttributeMetadata twoOption)

return enm;
}
public static MappingEnum Parse(OptionSetMetadata optionSet)
{
var enm = new MappingEnum
{
DisplayName = Naming.GetProperVariableName(Naming.GetProperVariableName(optionSet.Name)),
Items =
optionSet.Options.Select(
o => new MapperEnumItem
{
Attribute = new CrmPicklistAttribute
{
DisplayName = o.Label.UserLocalizedLabel.Label,
Value = o.Value ?? 1
},
Name = Naming.GetProperVariableName(o.Label.UserLocalizedLabel.Label)
}
).ToArray()
};

RenameDuplicates(enm);

return enm;
}
private static void RenameDuplicates(MappingEnum enm)
{
Dictionary<string, int> duplicates = new Dictionary<string, int>();
Expand Down
13 changes: 13 additions & 0 deletions CrmCodeGenerator.VSPackage/Resources/Templates/CSharp.tt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ using System.ComponentModel.DataAnnotations;

namespace <#= Context.Namespace #>
{
public static class Enums
{
<#foreach (var enm in Context.Enums) {#>
[DataContractAttribute]
public enum <#=enm.DisplayName#>
{
<#foreach(var item in enm.Items){#>
[EnumMemberAttribute]
<#= item.Name #> = <#= item.Value #>,
<#}#>
}
<#}#>
}
<# foreach (var entity in Context.Entities)
{
#>
Expand Down
13 changes: 13 additions & 0 deletions CrmCodeGenerator.VSPackage/Resources/Templates/CrmSvcUtil.tt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ using Microsoft.Xrm.Sdk.Client;
[assembly: Microsoft.Xrm.Sdk.Client.ProxyTypesAssemblyAttribute()]
namespace <#= Context.Namespace #>
{
public static class Enums
{
<#foreach (var enm in Context.Enums) {#>
[DataContractAttribute]
public enum <#=enm.DisplayName#>
{
<#foreach(var item in enm.Items){#>
[EnumMemberAttribute]
<#= item.Name #> = <#= item.Value #>,
<#}#>
}
<#}#>
}
<# foreach (var entity in Context.Entities) { #>
<# if (entity.States != null) { #>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ using Microsoft.Xrm.Sdk.Client;
[assembly: Microsoft.Xrm.Sdk.Client.ProxyTypesAssemblyAttribute()]
namespace <#= Context.Namespace #>
{
public static class Enums
{
<#foreach (var enm in Context.Enums) {#>
[DataContractAttribute]
public enum <#=enm.DisplayName#>
{
<#foreach(var item in enm.Items){#>
[EnumMemberAttribute]
<#= item.Name #> = <#= item.Value #>,
<#}#>
}
<#}#>
}
<# foreach (var entity in Context.Entities) { #>
<# if (entity.States != null) { #>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ using Microsoft.Xrm.Sdk.Client;
[assembly: Microsoft.Xrm.Sdk.Client.ProxyTypesAssemblyAttribute()]
namespace <#= Context.Namespace #>
{
public static class Enums
{
<#foreach (var enm in Context.Enums) {#>
[DataContractAttribute]
public enum <#=enm.DisplayName#>
{
<#foreach(var item in enm.Items){#>
[EnumMemberAttribute]
<#= item.Name #> = <#= item.Value #>,
<#}#>
}
<#}#>
}
<# foreach (var entity in Context.Entities) { #>
<# if (entity.States != null) { #>

Expand Down
Binary file modified CrmCodeGenerator.VSPackage/bin/Debug/CrmCodeGenerator.vsix
Binary file not shown.