-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWebResourceQueries.cs
More file actions
84 lines (77 loc) · 3.11 KB
/
Copy pathWebResourceQueries.cs
File metadata and controls
84 lines (77 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Generator.DTO;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace Generator.Queries;
public static class WebResourceQueries
{
public static async Task<IEnumerable<WebResource>> GetWebResourcesAsync(this ServiceClient service, List<Guid>? solutionIds = null)
{
var query = new QueryExpression("solutioncomponent")
{
ColumnSet = new ColumnSet("objectid"),
Criteria = new FilterExpression(LogicalOperator.And)
{
Conditions =
{
new ConditionExpression("solutionid", ConditionOperator.In, solutionIds),
new ConditionExpression("componenttype", ConditionOperator.Equal, 61) // 61 = Web Resource
}
},
LinkEntities =
{
new LinkEntity(
"solutioncomponent",
"webresource",
"objectid",
"webresourceid",
JoinOperator.Inner)
{
Columns = new ColumnSet("webresourceid", "name", "content", "webresourcetype", "description"),
EntityAlias = "webresource",
LinkCriteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("webresourcetype", ConditionOperator.Equal, 3) // JS Resources
}
}
}
}
};
var results = (await service.RetrieveMultipleAsync(query)).Entities;
var webResources = results.Select(e =>
{
var content = "";
var contentValue = e.GetAttributeValue<AliasedValue>("webresource.content")?.Value;
var webresourceId = e.GetAttributeValue<AliasedValue>("webresource.webresourceid").Value?.ToString() ?? "";
var webresourceName = e.GetAttributeValue<AliasedValue>("webresource.name").Value?.ToString();
if (contentValue != null)
{
// Content is base64 encoded, decode it
var base64Content = contentValue.ToString();
if (!string.IsNullOrEmpty(base64Content))
{
try
{
var bytes = Convert.FromBase64String(base64Content);
content = System.Text.Encoding.UTF8.GetString(bytes);
}
catch
{
// If decoding fails, keep the base64 content
content = base64Content;
}
}
}
return new WebResource(
webresourceId,
webresourceName,
content,
(OptionSetValue)e.GetAttributeValue<AliasedValue>("webresource.webresourcetype").Value,
e.GetAttributeValue<AliasedValue>("webresource.description")?.Value?.ToString()
);
});
return webResources;
}
}