-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
164 lines (135 loc) · 5.5 KB
/
Program.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using Microsoft.SharePoint.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk.Query;
using PowerApps.Samples;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace PnP.Wiki
{
class Program
{
[STAThread]
static void Main(string[] args)
{
/*
string siteUrl = "";
var authManager = new OfficeDevPnP.Core.AuthenticationManager();
// This method calls a pop up window with the login page and it also prompts
// for the multi factor authentication code.
ClientContext ctx = authManager.GetWebLoginClientContext(siteUrl);
// The obtained ClientContext object can be used to connect to the SharePoint site.
Web web = ctx.Web;
ctx.Load(web, w => w.Title);
ctx.ExecuteQuery();
Console.WriteLine("You have connected to {0} site, with Multi Factor Authentication enabled!!", web.Title);
Console.ReadLine();
*/
List<string> kbIds = new List<string>() { "437", "677", "1899", "2423", "2585" };
List<KBModel> KBList = new List<KBModel>();
KBList = System.IO.File.ReadAllLines(@"C:\Jithesh\Customer\AT\SPO\Wiki\list.csv")
.Skip(1)
.Select(v => KBModel.FromCsv(v))
.ToList<KBModel>();
List<KBModel> migrationList = new List<KBModel>();
foreach (KBModel item in KBList)
{
if (kbIds.Contains(item.Id.ToString()))
{
migrationList.Add(item);
}
}
CrmServiceClient service = null;
try
{
service = SampleHelpers.Connect("Connect");
if (service != null)
{
// Service implements IOrganizationService interface
if (service.IsReady)
{
#region Sample Code
//////////////////////////////////////////////
#region Demonstrate
// Instantiate an account object.
foreach (KBModel item in migrationList)
{
Entity newKb = new Entity("knowledgearticle");
newKb["title"] = item.Title.ToString();
newKb["content"] = GetHtml(item.Id.ToString());
newKb["keywords"] = item.Keywords.ToString();
newKb["description"] = item.FileLeafRef.ToString();
Guid kbId = service.Create(newKb);
Console.WriteLine(kbId);
}
#endregion Demonstrate
//////////////////////////////////////////////
#endregion Sample Code
Console.WriteLine("The sample completed successfully");
return;
}
else
{
const string UNABLE_TO_LOGIN_ERROR = "Unable to Login to Microsoft Dataverse";
if (service.LastCrmError.Equals(UNABLE_TO_LOGIN_ERROR))
{
Console.WriteLine("Check the connection string values in cds/App.config.");
throw new Exception(service.LastCrmError);
}
else
{
throw service.LastCrmException;
}
}
}
}
catch (Exception ex)
{
SampleHelpers.HandleException(ex);
}
finally
{
if (service != null)
service.Dispose();
Console.WriteLine("Press <Enter> to exit.");
Console.ReadLine();
}
}
private static string GetHtml(string docId)
{
StringBuilder contents = new StringBuilder();
contents.Append(System.IO.File.ReadAllText("C:\\Jithesh\\Customer\\AT\\SPO\\Wiki\\Files\\" + docId + ".html"));
contents.Replace("<br>", "<br />");
XElement wikiField = XElement.Parse(contents.ToString());
XElement div = wikiField.Descendants("div").Where(e =>
(string)e.Attribute("class") == "ms-rte-layoutszone-inner")
.FirstOrDefault();
return div.ToString();
}
}
class KBModel
{
public int Id { get; set; }
public string Title { get; set; }
public string Categ { get; set; }
public string FileLeafRef { get; set; }
public string Keywords { get; set; }
public static KBModel FromCsv(string csvLine)
{
string[] values = csvLine.Split(',');
KBModel kb = new KBModel();
int i;
int.TryParse(values[0].Replace("\"",""), out i);
kb.Id = i;
kb.Title = values[1].Replace("\"", "");
kb.Categ = values[2].Replace("\"", "");
kb.FileLeafRef = values[3].Replace("\"", "");
kb.Keywords = values[4].Replace("\"", "");
return kb;
}
}
}