-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.asmx.cs
executable file
·430 lines (378 loc) · 16.6 KB
/
service.asmx.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using Sitecore.Exceptions;
namespace SitecoreTestingService
{
/// <summary>
/// Summary description for service
/// </summary>
[WebService(Namespace = "/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class service : System.Web.Services.WebService
{
Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master");
HttpRequest req = HttpContext.Current.Request;
[WebMethod]
public List<string> media_item_info()
{
List<string> outlist = new List<string>();
string id = req.Params["id"];
outlist.Add(id);
Sitecore.Data.Items.Item item = master.GetItem(new Sitecore.Data.ID(id));
Sitecore.Data.Items.MediaItem media_item = new Sitecore.Data.Items.MediaItem(item);
Sitecore.Resources.Media.Media media = Sitecore.Resources.Media.MediaManager.GetMedia(media_item);
Sitecore.Resources.Media.MediaData media_data = media.MediaData;
return outlist;
}
[WebMethod]
//[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public CreatedMediaItem create_media_item()
{
CreatedMediaItem output = new CreatedMediaItem();
List<HttpPostedFile> files = new List<HttpPostedFile>();
if (req.Files.Count > 0)
{
HttpPostedFile media_file = req.Files["media_file"];
output.fileName = media_file.FileName;
var stream = media_file.InputStream;
using (new Sitecore.SecurityModel.SecurityDisabler())
{
//using (new Sitecore.Data.DatabaseCacheDisabler())
//{
Sitecore.Resources.Media.MediaCreatorOptions options = new Sitecore.Resources.Media.MediaCreatorOptions();
options.Database = master;
string itemName = req.Params["item_name"];
//string itemName = media_file.FileName.Replace(".jpg", "").Replace(".", "_").Replace("&", "_and_");
output.itemName = itemName;
options.Destination = req.Params["destination"] + "/" + itemName;
//create the item
Sitecore.Data.Items.Item media_item = Sitecore.Resources.Media.MediaManager.Creator.CreateFromStream(stream, media_file.FileName, options);
output.ID = media_item.ID.ToString();
// change the template
//string TemplateID = "{16692733-9A61-45E6-B0D4-4C0C06F8DD3C}";
//Sitecore.Data.Items.TemplateItem template = master.GetTemplate(new Sitecore.Data.ID(TemplateID));
//media_item.ChangeTemplate(template);
// edit the fields
media_item.Editing.BeginEdit();
media_item.Fields["Title"].Value = req.Params["title"];
media_item.Fields["Alt"].Value = req.Params["alt"];
media_item.Editing.EndEdit();
//}
}
}
return output;
/*
using (new Sitecore.SecurityModel.SecurityDisabler())
{
string ParentID = "{C2902D58-D0D2-4B6C-B93E-87208CFC2C89}";
string TemplateID = "{962B53C4-F93B-4DF9-9821-415C867B8903}";
Sitecore.Data.Items.TemplateItem template = master.GetTemplate(new Sitecore.Data.ID(TemplateID));
Sitecore.Data.Items.Item parent = master.GetItem(new Sitecore.Data.ID(ParentID));
Sitecore.Data.Items.Item newitem = parent.Add("foo", template);
return newitem.ID.ToString();
}
*/
}
[WebMethod]
public List<string> upload()
{
List<string> item_id_list = new List<string>();
if (req.Files.Count == 1)
{
HttpPostedFile file = req.Files[0];
var stream = file.InputStream;
var xml = XDocument.Load(stream);
foreach (XElement tag in xml.Descendants())
{
if (tag.Name == "Items")
{
string TemplateID = tag.Attribute("TemplateID").Value;
string ParentID = tag.Attribute("ParentID").Value;
string domainUser = tag.Attribute("User").Value;
foreach (XElement item in tag.Descendants())
{
if (item.Name == "Item")
{
if (Sitecore.Security.Accounts.User.Exists(domainUser))
{
Sitecore.Security.Accounts.User user = Sitecore.Security.Accounts.User.FromName(domainUser, false);
using (new Sitecore.Security.Accounts.UserSwitcher(user))
{
Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master");
Sitecore.Data.Items.Item parent = master.GetItem(new Sitecore.Data.ID(ParentID));
Sitecore.Data.Items.TemplateItem template = master.GetTemplate(new Sitecore.Data.ID(TemplateID));
string ItemName = item.Attribute("Name").Value;
Sitecore.Data.Items.Item newItem;
ItemName = Regex.Replace(ItemName, "[^a-zA-Z0-9]+", " ");
newItem = parent.Add(ItemName.Trim(), template);
try
{
}
catch (AccessDeniedException e)
{
//return "Failure"; // item_id_list;
}
newItem.Editing.BeginEdit();
try
{
foreach (XElement field in item.Descendants())
{
if (field.Name == "Field")
{
string name = field.Attribute("Name").Value;
string value = field.Attribute("Value").Value;
if (field.Attributes().Any(p => p.Name == "Type"))
{
string type = field.Attribute("Type").Value;
if (type == "Date")
{
value = Sitecore.DateUtil.ToIsoDate(DateTime.Parse(value));
}
}
newItem.Fields[name].Value = value;
}
}
}
finally
{
newItem.Editing.EndEdit();
}
item_id_list.Add(newItem.ID.ToString());
}
}
}
}
}
}
}
return item_id_list;
}
[WebMethod]
public Collection<Item> get_child_items()
{
Collection<Item> items = new Collection<Item>();
string id = req.Params["id"];
var item = master.GetItem(new Sitecore.Data.ID(id));
Sitecore.Collections.ChildList children = item.GetChildren();
foreach (Sitecore.Data.Items.Item kid in children)
{
var i = new Item();
i.id = kid.ID.ToString();
i.load();
items.Add(i);
}
return items;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Collection<Item> get_items()
{
Collection<Item> items = new Collection<Item>();
string list = req.Params["list"];
string[] id_list = list.Split(':');
foreach (string id in id_list)
{
var i = new Item();
i.id = id;
i.load();
items.Add(i);
}
return items;
}
[WebMethod]
public bool edit_item()
{
using (new Sitecore.SecurityModel.SecurityDisabler())
{
string id = req.Params["id"];
Sitecore.Data.Items.Item item = master.GetItem(new Sitecore.Data.ID(id));
item.Editing.BeginEdit();
try
{
foreach (string name in req.Params)
{
if (name != "id" && name.ToUpper() != name)
{
var value = req.Params[name];
item.Fields[name].Value = value;
}
}
}
finally
{
}
item.Editing.EndEdit();
}
return true;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Item get_item()
{
string id = req.Params["id"];
Item item = new Item();
item.id = id;
item.load();
return item;
}
[WebMethod]
public int spiderman()
{
string list = req.Params["list"];
string[] id_list = list.Split(':');
//return id_list.Count();
int count = 0;
using (new Sitecore.SecurityModel.SecurityDisabler())
{
foreach (string id in id_list)
{
var item = master.GetItem(new Sitecore.Data.ID(id));
item.Delete();
count++;
}
}
return count;
}
[WebMethod]
public bool delete_item()
{
string id = req.Params["id"];
using (new Sitecore.SecurityModel.SecurityDisabler())
{
var item = master.GetItem(new Sitecore.Data.ID(id));
item.Delete();
}
return true;
}
[WebMethod]
public List<string> delete()
{
List<string> item_id_list = new List<string>();
if (req.Files.Count == 1)
{
HttpPostedFile file = HttpContext.Current.Request.Files[0];
var stream = file.InputStream;
var xml = XDocument.Load(stream);
using (new Sitecore.SecurityModel.SecurityDisabler())
{
Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master");
foreach (XElement tag in xml.Descendants())
{
if (tag.Name == "{/}string")
{
Sitecore.Data.ID itemID = Sitecore.Data.ID.Parse(tag.Value);
try
{
Sitecore.Data.Items.Item item = master.GetItem(itemID);
item.Delete();
item_id_list.Add(item.ID.ToString());
}
catch (Exception e)
{
//swallow
}
}
}
}
}
return item_id_list;
}
[WebMethod]
//[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string publish()
{
using (new Sitecore.SecurityModel.SecurityDisabler())
{
Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master");
Sitecore.Data.Database web = Sitecore.Data.Database.GetDatabase("web");
string mode = req.Params["mode"];
if (mode == "")
{
mode = "site";
}
Sitecore.Publishing.PublishMode publishMode = Sitecore.Publishing.PublishMode.Incremental;
if (mode == "item")
{
publishMode = Sitecore.Publishing.PublishMode.SingleItem;
}
var options = new Sitecore.Publishing.PublishOptions(master, web, publishMode, Sitecore.Globalization.Language.Parse("en"), System.DateTime.Now);
string id = req.Params["id"];
//return id;
if (mode == "item" && id != "")
{
//return "I have an item and an ID of " + id;
Sitecore.Data.ID itemID = Sitecore.Data.ID.Parse(id);
options.RootItem = master.GetItem(itemID);
}
//return options.RootItem.Name;
var publisher = new Sitecore.Publishing.Publisher(options);
//publisher.Options.RootItem = master.GetItem("/sitecore/content/employee/home");
publisher.Options.Deep = true;
publisher.PublishAsync();
}
return "yo!";
}
}
public class Item
{
public string id { get; set; }
public string name { get; set; }
public Collection<Field> fields = new Collection<Field>();
public void load()
{
Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master");
Sitecore.Data.Items.Item item = master.GetItem(new Sitecore.Data.ID(this.id));
this.name = item.Name;
foreach (Sitecore.Data.Fields.Field field in item.Fields)
{
Field ff = new Field(field.Name, field.Value, field.Type);
this.fields.Add(ff);
}
}
}
public class CreatedMediaItem
{
public string fileName { get; set; }
public string itemName { get; set; }
public string ID { get; set; }
}
[Serializable]
public class CreatedItem
{
public List<string> ID { get; set; }
}
public class Field
{
public string name { get; set; }
public string value { get; set; }
public string type { get; set; }
public Field(string name, string value, string type)
{
this.name = name;
this.value = value;
this.type = type;
}
public Field(string name, string value)
{
this.name = name;
this.value = value;
}
public Field(string name)
{
this.name = name;
}
public Field()
{
}
}
}