-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharePointHelper.cs.ps1
More file actions
1118 lines (945 loc) · 48.4 KB
/
Copy pathSharePointHelper.cs.ps1
File metadata and controls
1118 lines (945 loc) · 48.4 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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Linq;
using System.Text;
using System.Xml;
using SPAttachmentTab.SPCopy;
using SPAttachmentTab.SPDws;
using SPAttachmentTab.SPLists;
using SPAttachmentTab.SPMeetings;
using SPAttachmentTab.SPObjects;
using SPAttachmentTab.SPSiteData;
using SPAttachmentTab.SPWebs;
using System.Web;
using System.Web.UI;
using System.Net;
namespace SPAttachmentTab
{
internal class SharePointHelper
{
public Page _mypage = null;
#region private member variables
private readonly String IndexListName = "Entity Library Index";
private const String IndexContentTypeName = "Entity Library Location";
private const String CustomDocContentTypeName = "Entity Attachment";
private readonly Int32 CustomListTemplateId = 100;
private readonly Int32 DocLibListTemplateId = 101;
private readonly String ItemContentTypeCode = "0x01";
private readonly String DocumentContentTypeCode = "0x0101";
private readonly String DefaultSiteTemplateIdentifier = "STS#1";
private readonly UInt32 _locId = 1033;
private readonly String ListsServiceEndpoint = "/_vti_bin/Lists.asmx";
private readonly String MeetingsServiceEndpoint = "/_vti_bin/Meetings.asmx";
private readonly String WebsServiceEndpoint = "/_vti_bin/Webs.asmx";
private readonly String SiteDataServiceEndpoint = "/_vti_bin/SiteData.asmx";
private readonly String DWSServiceEndpoint = "/_vti_bin/DWS.asmx";
private readonly String CopyServiceEndpoint = "/_vti_bin/Copy.asmx";
#endregion
#region Web Service Endpoint manipulation methods
private String PrepareSiteUrl(String url)
{
if (url == null) { return String.Empty; }
if (url.EndsWith(@"/")) { return url.Substring(0, url.Length - 1); }
if (!url.StartsWith("http://") && !url.StartsWith("https://"))
{
url = "http://" + url;
}
return url;
}
private String GetListsServiceUrl(String siteUrl)
{
return PrepareSiteUrl(siteUrl) + ListsServiceEndpoint;
}
private String GetMeetingsServiceUrl(String siteUrl)
{
return PrepareSiteUrl(siteUrl) + MeetingsServiceEndpoint;
}
private String GetWebsServiceUrl(String siteUrl)
{
return PrepareSiteUrl(siteUrl) + WebsServiceEndpoint;
}
private String GetSiteDataServiceUrl(String siteUrl)
{
return PrepareSiteUrl(siteUrl) + SiteDataServiceEndpoint;
}
private String GetDWSServiceUrl(String siteUrl)
{
return PrepareSiteUrl(siteUrl) + DWSServiceEndpoint;
}
private String GetCopyServiceUrl(String siteUrl)
{
return PrepareSiteUrl(siteUrl) + CopyServiceEndpoint;
}
#endregion
#region Start Here!!!!!: Create Library For Entity Method, Starting Point
internal void CreateLibraryForEntity(SPSettings settings, String listName)
{
if (!String.IsNullOrEmpty(settings.AliasPrefix))
{
listName = settings.AliasPrefix + listName;
}
LoggingWrapper.DebugMessage("ListName: " + listName);
LoggingWrapper.DebugMessage("PrepSites");
PrepareSiteForEntityLibraries(settings);
LoggingWrapper.DebugMessage("PrepSites - Done");
String destSiteLoc = DetermineListDestination(settings);
CreateIndexRecordForThisEntity(settings.SiteUrl, destSiteLoc, listName);
CreateDocLibraryOnSite(settings, destSiteLoc, listName);
}
#endregion
#region Get Folders, Files, Details about Library Items
internal void GetFilesInFolder(string siteUrl, string folderName, ref DocLibItemCollection items)
{
if (!siteUrl.EndsWith("/")) { siteUrl += "/"; }
string libraryName = folderName;
if (folderName.Contains("/"))
{
// we are looking at a library sub-folder. library name is the first segment of this path
libraryName = libraryName.Substring(0, libraryName.IndexOf("/"));
}
//List WebService
SiteData sitedataWs = new SiteData();
string status = string.Empty;
sitedataWs.Url = GetSiteDataServiceUrl(siteUrl);
sitedataWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
_sFPUrl[] docLibItems;
//call method
sitedataWs.EnumerateFolder(folderName, out docLibItems);
//check for data
if (docLibItems == null) { return; }
var filteredItems = from libItem in docLibItems
where !libItem.IsFolder && !libItem.Url.Contains("Forms")
select libItem;
foreach (var listItem in filteredItems)
{
string docPath = listItem.Url;
string docName = docPath.Substring(docPath.LastIndexOf("/") + 1);
DocLibItem item = new DocLibItem();
//store the virtual path (***used for delete operation***)
item.VirtualPath = docPath;
//store the actual file path
item.AbsolutePath = siteUrl + docPath;
item.ItemType = DocLibItemTypes.Document;
PopulateMetadataForDocument(siteUrl, libraryName, ref item);
items.Add(item);
//recursive call
}
items.Sort();
}
private void PopulateMetadataForDocument(string siteUrl, string libraryName, ref DocLibItem item)
{
// set defaults
item.Title = item.VirtualPath;
item.DocumentType = String.Empty;
string filePath = item.AbsolutePath;
try
{
using (Lists listsWs = new Lists())
{
listsWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
listsWs.Url = GetListsServiceUrl(siteUrl);
string rootWebUrl = GetRootWebUrl(siteUrl);
string virtualPath = filePath.Replace(rootWebUrl, String.Empty);
XmlDocument xmlDoc = new XmlDocument();
XmlNode query = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
query.InnerXml = @"<Where><Eq><FieldRef Name='FileRef' /><Value Type='Text'>" + virtualPath + @"</Value></Eq></Where>";
XmlNode queryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
queryOptions.InnerXml = @"<IncludeMandatoryColumns>True</IncludeMandatoryColumns><ViewAttributes Scope='Recursive' />";
//get ListItem ID
XmlNode viewFieldsNode = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
viewFieldsNode.InnerXml = "<FieldRef Name='ID' /><FieldRef Name='Title' /><FieldRef Name='DocumentCategory' />";
XmlNode itemsNode = listsWs.GetListItems(libraryName, null, query,
viewFieldsNode, int.MaxValue.ToString(), queryOptions, null);
string listItemId = String.Empty;
foreach (XmlNode itemNode in itemsNode.ChildNodes)
{
foreach (XmlNode itemNode2 in itemNode.ChildNodes)
{
if (itemNode2.Attributes != null)
{
XmlAttribute titleAttr = itemNode2.Attributes["ows_Title"];
XmlAttribute docCategoryAttr = itemNode2.Attributes["ows_DocumentCategory"];
if (titleAttr != null)
{
item.Title = titleAttr.InnerXml;
}
if (docCategoryAttr != null)
{
item.DocumentType = docCategoryAttr.InnerXml;
}
}
}
}
}
}
catch (Exception ex)
{
string status = ex.StackTrace.ToString();
LoggingWrapper.ReportError(ex);
}
}
internal void GetFolders(string siteUrl, string folderUrl, ref DocLibItemCollection items)
{
if (!siteUrl.EndsWith("/")) { siteUrl += "/"; }
//List WebService
SiteData sitedataWs = new SiteData();
string status = string.Empty;
sitedataWs.Url = GetSiteDataServiceUrl(siteUrl);
sitedataWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
_sFPUrl[] docLibItems;
//call method
sitedataWs.EnumerateFolder(folderUrl, out docLibItems);
//check for data
if (docLibItems == null) { return; }
var filteredItems = from libItem in docLibItems
where libItem.IsFolder && !libItem.Url.Contains("Forms")
select libItem;
foreach (var listItem in filteredItems)
{
string folderPath = listItem.Url;
string folderName = folderPath.Substring(folderPath.LastIndexOf("/") + 1);
DocLibItem item = new DocLibItem();
item.Title = folderName;
//store the virtual path (***used for delete operation***)
item.VirtualPath = folderPath;
//store the actual file path
item.AbsolutePath = siteUrl + folderPath;
item.ItemType = DocLibItemTypes.Folder;
string[] pathFragments = folderPath.Split('/');
item.ContainerName = (pathFragments.Length >= 2) ? pathFragments[pathFragments.Length - 2] : String.Empty;
//add to list
items.Add(item);
//recursive call
GetFolders(siteUrl, folderPath, ref items);
}
items.Sort();
}
#endregion
#region Site Existence and Creation Methods
private String CreateSubSite(SPSettings settings, String newSubSiteName)
{
using (Meetings mtgs = new Meetings())
{
mtgs.Url = GetMeetingsServiceUrl(settings.SiteUrl);
mtgs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
XmlNode node = mtgs.CreateWorkspace(newSubSiteName, DefaultSiteTemplateIdentifier, _locId, new TimeZoneInf());
}
String baseUrl = settings.SiteUrl;
if (!baseUrl.EndsWith("/")) { baseUrl += "/"; }
baseUrl += newSubSiteName;
return baseUrl;
}
internal Boolean SiteExists(String url)
{
try
{
using (Lists listWs = new Lists())
{
listWs.Url = GetListsServiceUrl(url);
listWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
listWs.Discover();
}
return true;
}
catch (Exception ex)
{
LoggingWrapper.ReportError(ex);
return false;
}
}
#endregion
#region Site Content Limiting and Provisioning methods (and Helpers)
// from a given url, determine the url of the site collections root.
private string GetRootWebUrl(string siteUrl)
{
String rootUrl = siteUrl;
using (SiteData sdWs = new SiteData())
{
sdWs.Url = GetSiteDataServiceUrl(siteUrl);
sdWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
_sSiteMetadata siteMetadata;
_sWebWithTime[] webInfo;
String users;
String sGroups;
String[] vGroups;
uint response = sdWs.GetSite(out siteMetadata, out webInfo, out users, out sGroups, out vGroups);
rootUrl = webInfo[0].Url;
if (rootUrl.EndsWith("/")) { rootUrl = rootUrl.Substring(0, rootUrl.Length - 1); }
string[] urlParts = rootUrl.Split(new char[] { '/' }, StringSplitOptions.None);
if (urlParts.Length > 3)
{
string[] shortUrlParts = new string[3];
for (int x = 0; x < 3; x++) { shortUrlParts[x] = urlParts[x]; }
rootUrl = String.Join("/", shortUrlParts);
}
if (!rootUrl.EndsWith("/")) { rootUrl += "/"; }
}
return rootUrl;
}
// determine which provisioned subsite should contain our new List
private String DetermineListDestination(SPSettings settings)
{
String topUrl = settings.SiteUrl;
LoggingWrapper.DebugMessage("TopUrl: " + topUrl);
String groupUrl = String.Empty;
using (SiteData sdWs = new SiteData())
{
sdWs.Url = GetSiteDataServiceUrl(topUrl);
LoggingWrapper.DebugMessage("ServiceUrl: " + sdWs.Url);
var cred = (NetworkCredential)SPSettingsManager.GetCredentialsFromConfig();
sdWs.Credentials = cred;
LoggingWrapper.DebugMessage("User: " + cred.UserName);
_sSiteMetadata siteMetadata;
_sWebWithTime[] webInfo;
String users;
String sGroups;
String[] vGroups;
uint response = sdWs.GetSite(out siteMetadata, out webInfo, out users, out sGroups, out vGroups);
String latestGroupUrl = String.Empty;
foreach (var webInfoItem in webInfo)
{
if (webInfoItem.Url.StartsWith(topUrl) && !webInfoItem.Url.Equals(topUrl))
{
latestGroupUrl = webInfoItem.Url;
}
}
groupUrl = latestGroupUrl;
if (String.IsNullOrEmpty(latestGroupUrl))
{
// create group site
String groupalias = GenerateNextGroupSiteAlias(String.Empty);
groupUrl = CreateSubSite(settings, groupalias);
}
else
{
// check if this group is beyond its limit;
if (GroupExceedsListLimit(settings, latestGroupUrl))
{
String groupalias = latestGroupUrl.Substring(latestGroupUrl.LastIndexOf("/")+1);
String newgroupalias = GenerateNextGroupSiteAlias(groupalias);
groupUrl = CreateSubSite(settings, newgroupalias);
}
}
}
return groupUrl;
}
// generate next alias for a "group site" which is site that holds a limited number of
// libraries. Alias are generated: A1, A2, A3, A4, ... A(n)
private String GenerateNextGroupSiteAlias(String groupalias)
{
if (String.IsNullOrEmpty(groupalias))
{
return "A1";
}
if (groupalias.Length == 2)
{
char letter = groupalias[0];
char index = groupalias[1];
int v;
if (int.TryParse(index.ToString(), out v))
{
v++;
}
else
{
throw new InvalidOperationException("Bad Group Site Alias Sequence: " + groupalias);
}
if (v == 10)
{
v = 1;
byte b = (byte)letter;
b++;
letter = (char)b;
if (letter == 'Z') { throw new OverflowException("SharePoint Group Site Alias Sequence overflow: " + groupalias); }
return String.Format("{0}{1}", letter, v);
}
else
{
return String.Format("{0}{1}", letter, v);
}
}
throw new OverflowException("SharePoint Group Site Alias Sequence overflow: " + groupalias);
}
// determines if the current Group site has reached the limit and we need to create a new one
private bool GroupExceedsListLimit(SPSettings settings, String latestGroupUrl)
{
int limit = SPSettingsManager.GetItemLimitForEntity(settings.EntityName);
int actual = 0;
using (Lists listWs = new Lists())
{
listWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
listWs.Url = GetListsServiceUrl(settings.SiteUrl);
XmlNode itemsNode = listWs.GetListItems(IndexListName, null, null, null, null, null, null);
foreach (XmlNode itemNode in itemsNode.ChildNodes)
{
foreach (XmlNode itemNode2 in itemNode.ChildNodes)
{
if (itemNode2.Attributes != null)
{
XmlAttribute locAttr = itemNode2.Attributes["ows_LibraryLocation"];
if (locAttr != null)
{
string path = locAttr.InnerXml;
if (path.Equals(latestGroupUrl)) { actual++; }
}
}
}
}
}
return actual >= limit;
}
// creates the Library
private void CreateDocLibraryOnSite(SPSettings settings, string destSiteLoc, string listName)
{
using (Lists listsWs = new Lists())
{
listsWs.Url = GetListsServiceUrl(destSiteLoc);
listsWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
string desc = String.Format("{0} library: {1}", settings.EntityName, listName);
XmlNode outputNode = listsWs.AddList(listName, desc, DocLibListTemplateId);
String contentTypeId = GetContentTypeByName(SPSettingsManager.GetContentTypeLocationForEntity(settings.EntityName), CustomDocContentTypeName);
listsWs.ApplyContentTypeToList(settings.SiteUrl, contentTypeId, listName);
}
}
// creates the list on our top level site to track where each entity's library is.
private void CreateIndexRecordForThisEntity(String rootSiteUrl, String destinationSiteUrl, String listName)
{
using (Lists listWs = new Lists())
{
listWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
listWs.Url = GetListsServiceUrl(rootSiteUrl);
XmlDocument doc = new XmlDocument();
XmlNode batchNode = doc.CreateNode(XmlNodeType.Element, "Batch", "");
XmlNode methodNode = doc.CreateNode(XmlNodeType.Element, "Method", "");
XmlNode idFieldNode = doc.CreateNode(XmlNodeType.Element, "Field", "");
XmlNode titleFieldNode = doc.CreateNode(XmlNodeType.Element, "Field", "");
XmlNode locationFieldNode = doc.CreateNode(XmlNodeType.Element, "Field", "");
XmlAttribute onErrorAttr = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, "OnError", "");
onErrorAttr.InnerXml = "Continue";
XmlAttribute listVerAttr = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, "ListVersion", "");
listVerAttr.InnerXml = "1";
XmlAttribute methodIDAttr = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, "ID", "");
methodIDAttr.InnerXml = "1";
XmlAttribute methodCmdAttr = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, "Cmd", "");
methodCmdAttr.InnerXml = "New";
XmlAttribute fldIDNameAttr = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, "Name", "");
fldIDNameAttr.InnerXml = "ID";
XmlAttribute fldTitleNameAttr = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, "Name", "");
fldTitleNameAttr.InnerXml = "Title";
XmlAttribute fldLocNameAttr = (XmlAttribute)doc.CreateNode(XmlNodeType.Attribute, "Name", "");
fldLocNameAttr.InnerXml = "LibraryLocation";
batchNode.Attributes.Append(onErrorAttr);
batchNode.Attributes.Append(listVerAttr);
methodNode.Attributes.Append(methodIDAttr);
methodNode.Attributes.Append(methodCmdAttr);
idFieldNode.Attributes.Append(fldIDNameAttr);
titleFieldNode.Attributes.Append(fldTitleNameAttr);
locationFieldNode.Attributes.Append(fldLocNameAttr);
idFieldNode.InnerXml = "New";
titleFieldNode.InnerXml = listName;
locationFieldNode.InnerXml = destinationSiteUrl;
methodNode.AppendChild(idFieldNode);
methodNode.AppendChild(titleFieldNode);
methodNode.AppendChild(locationFieldNode);
batchNode.AppendChild(methodNode);
XmlNode resultNode = listWs.UpdateListItems(IndexListName, batchNode);
}
}
// determines whether the "index list" exists for a given site.
// this needs to be created once per entity
private void PrepareSiteForEntityLibraries(SPSettings settings)
{
bool indexListExists = false;
using (Lists listsWs = new Lists())
{
listsWs.Url = GetListsServiceUrl(settings.SiteUrl);
listsWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
foreach (XmlNode listNode in listsWs.GetListCollection().ChildNodes)
{
XmlAttribute titleAttribute = listNode.Attributes["Title"];
if (titleAttribute != null)
{
if (IndexListName.Equals(titleAttribute.InnerXml))
{
indexListExists = true;
break;
}
}
}
LoggingWrapper.DebugMessage("Index List Exists: " + indexListExists.ToString());
if (!indexListExists)
{
listsWs.AddList(IndexListName, String.Empty, CustomListTemplateId);
String contentTypeId = GetContentTypeByName(SPSettingsManager.GetContentTypeLocationForEntity(settings.EntityName), IndexContentTypeName);
listsWs.ApplyContentTypeToList(settings.SiteUrl, contentTypeId, IndexListName);
}
}
}
// looks for Entity reference in index list.
internal String FindEntityLibraryLocation(SPSettings settings, string id)
{
string url = String.Empty;
using (Lists listWs = new Lists())
{
listWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
listWs.Url = GetListsServiceUrl(settings.SiteUrl);
XmlDocument xmlDoc = new XmlDocument();
XmlNode viewFieldsNode = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
viewFieldsNode.InnerXml = "<FieldRef Name=\"ID\" /><FieldRef Name=\"Title\" /><FieldRef Name=\"LibraryLocation\" />";
XmlNode query = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
query.InnerXml = @"<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + id + @"</Value></Eq></Where>";
XmlNode itemsNode = listWs.GetListItems(IndexListName, null, query, viewFieldsNode, null, null, null);
foreach (XmlNode itemNode in itemsNode.ChildNodes)
{
foreach (XmlNode itemNode2 in itemNode.ChildNodes)
{
if (itemNode2.Attributes != null)
{
//url += ", item has has attributes!" + (x++).ToString() ;
XmlAttribute titleAttr = itemNode2.Attributes["ows_Title"];
XmlAttribute locAttr = itemNode2.Attributes["ows_LibraryLocation"];
string path = String.Empty;
string title = String.Empty;
if (titleAttr != null) { title = titleAttr.InnerXml; }
if (locAttr != null) { path = locAttr.InnerXml; }
if (_mypage is Page) _mypage.Trace.Write("Title > " + title + ", Path > " + path);
if (title.Equals(id))
{
if (_mypage is Page) _mypage.Trace.Write("Url Found!!!: " + path);
url = path;
}
}
}
}
}
return url;
}
#endregion
#region Working with Content Types
private String GetContentTypeByName(String contentLocation, String contentTypeName)
{
String contentTypeId = String.Empty;
using (Webs websWs = new Webs())
{
websWs.Url = GetWebsServiceUrl(contentLocation);
websWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
bool contentTypeFound = false;
XmlNode contentTypesNode = websWs.GetContentTypes();
foreach (XmlNode contentTypeNode in contentTypesNode.ChildNodes)
{
XmlAttribute nameAttrib = contentTypeNode.Attributes["Name"];
XmlAttribute idAttrib = contentTypeNode.Attributes["ID"];
if (nameAttrib != null)
{
if (contentTypeName.Equals(nameAttrib.InnerXml))
{
contentTypeId = idAttrib.InnerXml;
XmlNode contentTypeDescNode = websWs.GetContentType(contentTypeId);
contentTypeFound = true;
break;
}
}
}
if (!contentTypeFound)
{
switch (contentTypeName)
{
case IndexContentTypeName:
contentTypeId = CreateEntityLibraryIndexContentType(websWs);
break;
case CustomDocContentTypeName:
contentTypeId = CreateCustomDocLibContentType(websWs);
break;
default:
throw new ArgumentException("GetContentTypeByName:Unknown ContentType, Cannot Create.");
}
}
}
return contentTypeId;
}
private String CreateCustomDocLibContentType(Webs websWs)
{
String docTypeColumn = "DocumentCategory";
String docTypeColumnDesc = "Document Category";
if (!SiteColumnExists(docTypeColumn, websWs))
{
CreateSiteColumn(docTypeColumn, docTypeColumnDesc, "Text", websWs);
}
XmlDocument contentTypeDoc = new XmlDocument();
XmlNode fieldsNode = contentTypeDoc.CreateNode(XmlNodeType.Element, "Fields", "");
XmlNode methodNode = contentTypeDoc.CreateNode(XmlNodeType.Element, "Method", "");
XmlAttribute methodidAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "ID", "");
methodidAttr.InnerXml = "1";
methodNode.Attributes.Append(methodidAttr);
fieldsNode.AppendChild(methodNode);
XmlNode field1Node = contentTypeDoc.CreateNode(XmlNodeType.Element, "Field", "");
XmlAttribute nameAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "Name", "");
nameAttr.InnerXml = docTypeColumn;
XmlAttribute dispAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "DisplayName", "");
dispAttr.InnerXml = docTypeColumnDesc;
XmlAttribute typeAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "Type", "");
typeAttr.InnerXml = "Text";
field1Node.Attributes.Append(nameAttr);
field1Node.Attributes.Append(dispAttr);
field1Node.Attributes.Append(typeAttr);
methodNode.AppendChild(field1Node);
return CreateContentType(websWs, CustomDocContentTypeName, GetBaseContentTypeID(websWs, "Item"), fieldsNode);
}
private String CreateEntityLibraryIndexContentType(Webs websWs)
{
String libLocColumn = "LibraryLocation";
String libLocColumnDesc = "Library Location";
if (!SiteColumnExists(libLocColumn, websWs))
{
CreateSiteColumn(libLocColumn, libLocColumnDesc, "Text", websWs);
}
XmlDocument contentTypeDoc = new XmlDocument();
XmlNode fieldsNode = contentTypeDoc.CreateNode(XmlNodeType.Element, "Fields", "");
XmlNode methodNode = contentTypeDoc.CreateNode(XmlNodeType.Element, "Method", "");
XmlAttribute methodidAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "ID", "");
methodidAttr.InnerXml = "1";
methodNode.Attributes.Append(methodidAttr);
fieldsNode.AppendChild(methodNode);
XmlNode field1Node = contentTypeDoc.CreateNode(XmlNodeType.Element, "Field", "");
XmlAttribute nameAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "Name", "");
nameAttr.InnerXml = libLocColumn;
XmlAttribute dispAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "DisplayName", "");
dispAttr.InnerXml = libLocColumnDesc;
XmlAttribute typeAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "Type", "");
typeAttr.InnerXml = "Text";
field1Node.Attributes.Append(nameAttr);
field1Node.Attributes.Append(dispAttr);
field1Node.Attributes.Append(typeAttr);
methodNode.AppendChild(field1Node);
return CreateContentType(websWs, IndexContentTypeName, GetBaseContentTypeID(websWs, "Item"), fieldsNode);
}
private string GetBaseContentTypeID(Webs websWs, String baseContentTypeName)
{
XmlNode contentTypesNode = websWs.GetContentTypes();
foreach (XmlNode contentTypeNode in contentTypesNode.ChildNodes)
{
XmlAttribute nameAttrib = contentTypeNode.Attributes["Name"];
XmlAttribute idAttrib = contentTypeNode.Attributes["ID"];
if (nameAttrib != null)
{
if (baseContentTypeName.Equals(nameAttrib.InnerXml))
{
string contentTypeId = idAttrib.InnerXml;
return contentTypeId;
}
}
}
return "0x01"; // This is the ID for the 'Item' ContentType
}
private string CreateContentType(Webs websWs, string name, string itemContentTypeCode, XmlNode fieldsNode)
{
XmlDocument contentTypeDoc = new XmlDocument();
XmlNode propsNode = contentTypeDoc.CreateNode(XmlNodeType.Element, "ContentType", "");
String id = websWs.CreateContentType(name, itemContentTypeCode, fieldsNode, propsNode);
return id;
}
#endregion
#region Working with Site Columns
private void CreateSiteColumn(String colName, String colDesc, String colType, Webs websWs)
{
XmlDocument contentTypeDoc = new XmlDocument();
XmlNode fieldsNode = contentTypeDoc.CreateNode(XmlNodeType.Element, "Fields", "");
XmlNode methodNode = contentTypeDoc.CreateNode(XmlNodeType.Element, "Method", "");
XmlAttribute methodidAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "ID", "");
methodidAttr.InnerXml = "1";
methodNode.Attributes.Append(methodidAttr);
fieldsNode.AppendChild(methodNode);
XmlNode field1Node = contentTypeDoc.CreateNode(XmlNodeType.Element, "Field", "");
XmlAttribute nameAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "Name", "");
nameAttr.InnerXml = colName;
XmlAttribute dispAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "DisplayName", "");
dispAttr.InnerXml = colDesc;
XmlAttribute typeAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "Type", "");
typeAttr.InnerXml = "Text";
XmlAttribute requiredAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "Required", "");
requiredAttr.InnerXml = "FALSE";
XmlAttribute groupAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "Group", "");
groupAttr.InnerXml = "Custom Columns";
XmlAttribute maxLenAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "MaxLength", "");
maxLenAttr.InnerXml = "255";
field1Node.Attributes.Append(typeAttr);
field1Node.Attributes.Append(dispAttr);
field1Node.Attributes.Append(requiredAttr);
field1Node.Attributes.Append(maxLenAttr);
field1Node.Attributes.Append(groupAttr);
field1Node.Attributes.Append(nameAttr);
methodNode.AppendChild(field1Node);
XmlNode resultsNode = websWs.UpdateColumns(fieldsNode, fieldsNode, null);
}
private void RemoveSiteColumn(String colName, Webs websWs)
{
XmlDocument contentTypeDoc = new XmlDocument();
XmlNode fieldsNode = contentTypeDoc.CreateNode(XmlNodeType.Element, "Fields", "");
XmlNode methodNode = contentTypeDoc.CreateNode(XmlNodeType.Element, "Method", "");
XmlAttribute methodidAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "ID", "");
methodidAttr.InnerXml = "1";
methodNode.Attributes.Append(methodidAttr);
fieldsNode.AppendChild(methodNode);
XmlNode field1Node = contentTypeDoc.CreateNode(XmlNodeType.Element, "Field", "");
XmlAttribute nameAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "Name", "");
nameAttr.InnerXml = colName;
XmlAttribute typeAttr = (XmlAttribute)contentTypeDoc.CreateNode(XmlNodeType.Attribute, "Type", "");
typeAttr.InnerXml = "Text";
field1Node.Attributes.Append(nameAttr);
field1Node.Attributes.Append(typeAttr);
methodNode.AppendChild(field1Node);
XmlNode resultsNode = websWs.UpdateColumns(null, null, fieldsNode);
}
private bool SiteColumnExists(String colName, Webs websWs)
{
XmlNode columnsNode = websWs.GetColumns();
foreach (XmlNode colNode in columnsNode.ChildNodes)
{
Console.WriteLine(colNode.OuterXml);
XmlAttribute nameAttr = colNode.Attributes["Name"];
if (nameAttr != null)
{
if (nameAttr.InnerXml.Equals(colName))
{
return true;
}
}
}
return false;
}
#endregion
#region Deleting Files From Library
internal string DeleteFilesFromDocumentLibrary(string siteUrl, string libraryName, string[] pathsToDelete)
{
using (Lists listsWs = new Lists())
{
string status = string.Empty;
listsWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
//location
listsWs.Url = GetListsServiceUrl(siteUrl);
try
{
//get Lists GUID
XmlNode ndListView = listsWs.GetListAndView(libraryName, "");
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;
//string builder
StringBuilder sbXML = new StringBuilder();
XmlNodeList xListItemID = null;
XmlNode xListItemsData = null;
string sListItemID = string.Empty;
//one time setup
XmlDocument xmlDoc = new XmlDocument();
XmlDocument xDocItemID = new XmlDocument();
//check if there is site nesting
string sPreFilePath = GetWebsPrefixForFilePath(siteUrl);
//sequence setup
foreach (string sFilePath in pathsToDelete)
{
string sFullFilePath = sPreFilePath + sFilePath;
//CAML query
XmlNode query = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
query.InnerXml = @"<Where><Eq><FieldRef Name='FileRef' /><Value Type='Text'>" + sFullFilePath + @"</Value></Eq></Where>";
XmlNode queryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
queryOptions.InnerXml = @"<IncludeMandatoryColumns>True</IncludeMandatoryColumns><ViewAttributes Scope='Recursive' />";
//get ListItem ID
xListItemsData = listsWs.GetListItems(libraryName, null, query,
null, int.MaxValue.ToString(), queryOptions, null);
xDocItemID.LoadXml(xListItemsData.OuterXml);
xListItemID = xDocItemID.GetElementsByTagName("z:row");
sListItemID = xListItemID[0].Attributes["ows_ID"].Value.ToString();
string sXML = @"<Method ID='1' Cmd='Delete'>
<Field Name='ID'>" + sListItemID + "</Field>" +
"<Field Name='FileRef'>" + sFullFilePath + @"</Field>" +
@"</Method>";
//add to string
sbXML.Append(sXML);
//clean
sXML = string.Empty;
}
//operation
XmlElement updateBatch = xmlDoc.CreateElement("Batch");
updateBatch.SetAttribute("OnError", "Continue");
updateBatch.SetAttribute("ListVersion", "1");
updateBatch.SetAttribute("ViewName", strViewID);
updateBatch.InnerXml = sbXML.ToString();
XmlNode xnode = listsWs.UpdateListItems(strListID, updateBatch);
status = xnode.OuterXml.ToString();
//clean
sbXML = null;
xmlDoc = null;
xDocItemID = null;
}
catch (Exception ex)
{
status = ex.StackTrace.ToString();
LoggingWrapper.ReportError(ex);
}
return status;
}
}
// When deleting a file, the path should contain not only its location
// in the current library, but also its current site's location within the site collection.
private string GetWebsPrefixForFilePath(string siteUrl)
{
string siteBaseUrl = GetRootWebUrl(siteUrl);
if (!siteBaseUrl.EndsWith("/")) { siteBaseUrl += "/"; }
string newPath = siteUrl.Replace(siteBaseUrl, String.Empty);
if (!newPath.EndsWith("/")) { newPath += "/"; }
return newPath;
}
#endregion
#region Working with Library Folders
internal string CreateFolder(string siteUrl, string folderUrl)
{
using (Dws dwsWs = new Dws())
{
string status = string.Empty;
dwsWs.Credentials = SPSettingsManager.GetCredentialsFromConfig();
//location
dwsWs.Url = GetDWSServiceUrl(siteUrl);
try
{
status = dwsWs.CreateFolder(folderUrl);
}
catch (Exception ex)
{
status = ex.StackTrace.ToString();
}
return status;
}
}
internal string DeleteFolder(string siteUrl, string folderUrl)
{
using (Dws objSvc = new Dws())
{
string status = string.Empty;
objSvc.Credentials = SPSettingsManager.GetCredentialsFromConfig();
//location
objSvc.Url = GetDWSServiceUrl(siteUrl);
try
{
string delURL = folderUrl.Replace(" ", "%20");
//check if the folder has files, folders with files are not allowed to be deleted
bool IsEmpty = IsFolderEmpty(siteUrl, folderUrl);
//check if there "/" after the folder to delete
if (folderUrl.Substring(folderUrl.Length - 1, 1) == "/")
{
delURL = folderUrl.Remove(folderUrl.Length - 1, 1);
}
if (IsEmpty == true)
{
//delete
status = objSvc.DeleteFolder(delURL);
}
else
{
status = " (Only empty folder can be deleted.)";
}
}
catch (Exception ex)
{
status = ex.StackTrace.ToString();
LoggingWrapper.ReportError(ex);
}