-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnector.cs
136 lines (121 loc) · 5.13 KB
/
Connector.cs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.IO;
using System.Linq;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
namespace AWRP {
public class Connector {
private ServiceClient client;
private bool _IsReady;
public bool IsReady {
get {
return _IsReady;
}
private set {
_IsReady = value;
}
}
public Connector(string domain, string username, string secret) {
string connectionString = $@"
AuthType = OAuth;
Url = {domain};
UserName = {username};
Password = {secret};
LoginPrompt=Auto;
RequireNewInstance = True";
try {
client = new ServiceClient(connectionString);
IsReady = client.IsReady;
} catch {
IsReady = false;
}
}
private string EncodeFile(string file) {
FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[fs.Length];
long count = fs.Read(buffer, 0, (int)fs.Length);
fs.Close();
return Convert.ToBase64String(buffer);
}
public bool UploadFile(string file, string resourcePath, string description, string solutionUniqueName) {
if (!File.Exists(file)) {
Console.WriteLine($"File '{file}' dont exists");
return false;
}
if (!SolutionExists(solutionUniqueName)) {
Console.WriteLine($"A solution with the unique id '{solutionUniqueName}' does not exists");
return false;
}
string resourceName = resourcePath + Path.GetFileName(file);
Entity entity = new Entity("webresource");
entity["content"] = EncodeFile(file);
entity["displayname"] = Path.GetFileName(file);
entity["description"] = description;
entity["name"] = resourceName;
switch(Path.GetExtension(file)) {
case ".html":
case ".htm":
entity["webresourcetype"] = new OptionSetValue((int)WebResourceType.Html);
break;
case ".css":
entity["webresourcetype"] = new OptionSetValue((int)WebResourceType.Style);
break;
case ".js":
entity["webresourcetype"] = new OptionSetValue((int)WebResourceType.Script);
break;
case ".png":
entity["webresourcetype"] = new OptionSetValue((int)WebResourceType.Png);
break;
case ".jpg":
entity["webresourcetype"] = new OptionSetValue((int)WebResourceType.Jpg);
break;
case ".gif":
entity["webresourcetype"] = new OptionSetValue((int)WebResourceType.Gif);
break;
case ".xap":
entity["webresourcetype"] = new OptionSetValue((int)WebResourceType.Silverlight);
break;
case ".xsl":
case ".xslt":
entity["webresourcetype"] = new OptionSetValue((int)WebResourceType.StyleSheet);
break;
case ".ico":
entity["webresourcetype"] = new OptionSetValue((int)WebResourceType.Ico);
break;
case ".svg":
entity["webresourcetype"] = new OptionSetValue((int)WebResourceType.Vector);
break;
case ".resx":
entity["webresourcetype"] = new OptionSetValue((int)WebResourceType.String);
break;
default:
return false;
}
QueryByAttribute qba = new QueryByAttribute("webresource");
qba.ColumnSet = new ColumnSet(true);
qba.AddAttributeValue("name", resourceName);
Entity res;
if ((res = client.RetrieveMultiple(qba).Entities.FirstOrDefault()) == null) {
CreateRequest req = new CreateRequest() {
Target = entity
};
req.Parameters.Add("SolutionUniqueName", solutionUniqueName);
Console.WriteLine("WebResource dont exists... Automatically create one");
client.Execute(req);
} else {
Console.WriteLine("WebResource exists... Try to update");
entity.Id = res.Id;
client.Update(entity);
}
return true;
}
private bool SolutionExists(string solutionUniqueName) {
QueryByAttribute qba = new QueryByAttribute("solution");
qba.AddAttributeValue("uniquename", solutionUniqueName);
EntityCollection res = client.RetrieveMultiple(qba);
return res.Entities.Count != 0;
}
}
}