This repository has been archived by the owner on Feb 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPWeb.cs
999 lines (851 loc) · 39.9 KB
/
SPWeb.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
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
/* Copyright (C) 2014 NAVERTICA a.s. http://www.navertica.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Caching;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
namespace Navertica.SharePoint.Extensions
{
public static class SPWebExtensions
{
/// <summary>
/// Uploads new document
/// </summary>
/// <param name="web"></param>
/// <param name="fullPath"></param>
/// <param name="uploadStream"></param>
/// <returns></returns>
public static SPListItem CreateNewDocument(this SPWeb web, string fullPath, Stream uploadStream)
{
if (web == null) throw new ArgumentNullException("web");
if (fullPath == null) throw new ArgumentNullException("fullPath");
if (uploadStream == null) throw new ArgumentNullException("uploadStream");
WebRequest request = WebRequest.Create(fullPath);
request.Credentials = CredentialCache.DefaultCredentials; // User must have 'Contributor' access to the document library
request.Method = "PUT";
request.Headers.Add("Overwrite", "t");
byte[] buffer = new byte[4096];
using (Stream stream = request.GetRequestStream())
{
for (int i = uploadStream.Read(buffer, 0, buffer.Length); i > 0; i = uploadStream.Read(buffer, 0, buffer.Length))
{
stream.Write(buffer, 0, i);
}
stream.Close();
}
WebResponse response = request.GetResponse(); // Upload the file
response.Close();
return web.GetItemByUrl(fullPath);
}
/// <summary>
/// Delete web with all subwebs
/// </summary>
/// <param name="web"></param>
public static void DeleteAll(this SPWeb web)
{
if (web == null) throw new ArgumentNullException("web");
for (int i = 0; i < web.Webs.Count; i++)
{
using (SPWeb subWeb = web.Webs[i])
{
subWeb.DeleteAll();
}
}
web.Delete();
}
#region FindList/s
/// <summary>
/// Recursive - returns first positive list identification for a given list name or guid, starting with web and then recursively walking its subwebs
/// </summary>
/// <param name="web"></param>
/// <param name="listNameUrlOrGuid"> </param>
/// <returns>WebListId or WebListId with empty Guids if not found</returns>
public static WebListId FindList(this SPWeb web, string listNameUrlOrGuid) // TODO - prejmenovat na FindListRecursive
{
if (web == null) throw new ArgumentNullException("web");
if (listNameUrlOrGuid == null) throw new ArgumentNullException("listNameUrlOrGuid");
if (listNameUrlOrGuid == string.Empty) return new WebListId();
string cacheName = "FindListWeb_" + web.ID + "_" + listNameUrlOrGuid;
WebListId result = (WebListId) HttpRuntime.Cache.Get(cacheName);
if (result != null && !web.Site.InTimerJob())
{
return new WebListId(result.WebGuid, result.ListGuid);
}
try
{
SPList list = web.OpenList(listNameUrlOrGuid);
if (list == null)
{
if (web.ParentWeb != null)
{
result = web.ParentWeb.FindList(listNameUrlOrGuid);
}
}
if (result == null && list != null)
{
result = new WebListId(list);
}
}
catch (FileNotFoundException) //nastane pri mazani webu
{
result = new WebListId();
}
if (result == null) result = new WebListId();
if (result.IsValid)
{
HttpRuntime.Cache.Insert(cacheName, result, null, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
}
return result.IsValid
? new WebListId(result.WebGuid, result.ListGuid)
: new WebListId { InvalidMessage = "Could not found list '" + listNameUrlOrGuid + "' at web: " + web.Url };
}
/// <summary>
/// Finds all lists of given listType in given web and subwebs
/// </summary>
/// <param name="web"></param>
/// <param name="listType"></param>
/// <returns></returns>
public static WebListDictionary FindListsWithBaseTemplate(this SPWeb web, SPListTemplateType listType)
{
if (web == null) throw new ArgumentNullException("web");
WebListDictionary foundLists = new WebListDictionary();
using (new SPMonitoredScope("FindListsWithBaseTemplate at web " + web.Url + " listType: " + listType))
{
IEnumerable<Guid> webIDs = GetIds(web);
web.Site.RunElevated(delegate(SPSite elevatedSite)
{
foreach (Guid childWebId in webIDs)
{
using (SPWeb childWeb = elevatedSite.OpenW(childWebId, true))
{
foundLists.AddRange(childWeb.Lists.Cast<SPList>().Where(l => l.BaseTemplate == listType).Select(l => new WebListId(l)));
}
}
return null;
});
}
return foundLists;
}
/// <summary>
/// Finds all lists containing content types with ids beginning with given ContentTypeId in given web and its subwebs
/// </summary>
/// <param name="web"></param>
/// <param name="contentTypeId">initial section of a content type id - "0x0108" etc.</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static WebListDictionary FindListsWithContentType(this SPWeb web, string contentTypeId)
{
if (web == null) throw new ArgumentNullException("web");
if (contentTypeId == null) throw new ArgumentNullException("contentTypeId");
SPContentTypeId loadedCtId;
try
{
loadedCtId = new SPContentTypeId(contentTypeId);
}
catch (ArgumentException)
{
throw new ArgumentException("contentTypeId '" + contentTypeId + "' is not valid");
}
return FindListsWithContentType(web, loadedCtId);
}
/// <summary>
/// Finds all lists containing content types with ids beginning with given ContentTypeId in given web and its subwebs
/// </summary>
/// <param name="web"></param>
/// <param name="contentTypeId">initial section of a content type id - "0x0108" etc.</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static WebListDictionary FindListsWithContentType(this SPWeb web, SPContentTypeId contentTypeId)
{
if (web == null) throw new ArgumentNullException("web");
if (contentTypeId == null) throw new ArgumentNullException("contentTypeId");
WebListDictionary foundLists = new WebListDictionary();
using (new SPMonitoredScope("FindListsWithContentType at web " + web.Url + " contentTypeId: " + contentTypeId))
{
IEnumerable<Guid> webIDs = GetIds(web);
web.Site.RunElevated(delegate(SPSite elevatedSite)
{
foreach (Guid childWebId in webIDs)
{
using (SPWeb childWeb = elevatedSite.OpenW(childWebId, true))
{
foreach (SPList list in childWeb.Lists)
{
try
{
if (list.ContentTypes.BestMatch(contentTypeId).IsChildOf(contentTypeId)) //if ct is "0x0108" it will add the childs too
{
foundLists.Add(list);
}
}
// ReSharper disable once EmptyGeneralCatchClause
catch {}
}
}
}
return null;
});
}
return foundLists;
}
public static WebListDictionary FindListsWithField(this SPWeb web, string intFieldName)
{
if (web == null) throw new ArgumentNullException("web");
if (intFieldName == null) throw new ArgumentNullException("intFieldName");
WebListDictionary foundLists = new WebListDictionary();
IEnumerable<Guid> webIDs = GetIds(web);
web.Site.RunElevated(delegate(SPSite elevatedSite)
{
foreach (Guid childWebId in webIDs)
{
using (SPWeb childWeb = elevatedSite.OpenW(childWebId, true))
{
foreach (SPList list in childWeb.Lists)
{
try
{
if (list.ContainsFieldIntName(intFieldName))
{
foundLists.Add(list);
}
}
// ReSharper disable once EmptyGeneralCatchClause
catch {}
}
}
}
return null;
});
return foundLists;
}
/// <summary>
/// Returns the list of Guid of the subwebs and currentWeb. Must be called with elevated privileges
/// </summary>
/// <param name="web"></param>
/// <returns></returns>
private static IEnumerable<Guid> GetIds(SPWeb web)
{
using (new SPMonitoredScope("GetWebIds for web " + web.Url))
{
List<Guid> webIDs = new List<Guid> { web.ID };
web.RunElevated(delegate(SPWeb elevatedWeb)
{
foreach (SPWeb subweb in elevatedWeb.Webs)
{
webIDs.Add(subweb.ID);
subweb.Dispose();
}
return null;
});
return webIDs;
}
}
#endregion
/// <summary>
/// Tries to get SPFolder from given Url
/// </summary>
/// <param name="web"></param>
/// <param name="folderUrl"></param>
/// <returns>SPFolder or null</returns>
public static SPFolder GetFolderFromUrl(this SPWeb web, string folderUrl)
{
if (web == null) throw new ArgumentNullException("web");
if (folderUrl == null) throw new ArgumentNullException("folderUrl");
string resultFolder = folderUrl;
if (folderUrl.EndsWith("Forms"))
{
resultFolder = folderUrl.Substring(0, folderUrl.LastIndexOf("Forms", StringComparison.InvariantCulture));
}
if (folderUrl.EndsWith("Forms/"))
{
resultFolder = folderUrl.Substring(0, folderUrl.LastIndexOf("Forms/", StringComparison.InvariantCulture));
}
SPFolder folder = web.GetFolder(resultFolder);
return folder.Exists ? folder : null;
}
/// <summary>
/// Returns SPListItem for both absolute and relative url of a document (not a list item)
/// </summary>
/// <param name="web"></param>
/// <param name="url"></param>
/// <returns></returns>
public static SPListItem GetItemByUrl(this SPWeb web, string url)
{
if (web == null) throw new ArgumentNullException("web");
if (url == null) throw new ArgumentNullException("url");
return web.GetListItem(url);
}
#region Users Tools
/// <summary>
/// Get Principal's LoginName [lowercase format] by identification
/// </summary>
/// <param name="web"></param>
/// <param name="identification">Can be PrincipalId, SPFieldUser, SPFieldUserValue(lookupformat), LoginName, Name or SPRoleAssigment</param>
/// <returns>login or null</returns>
public static string GetSPPrincipalLoginName(this SPWeb web, object identification)
{
if (web == null) throw new ArgumentNullException("web");
if (identification == null) throw new ArgumentNullException("identification");
SPPrincipal principal = GetSPPrincipal(web, identification);
return principal != null ? principal.LoginNameNormalized() : null;
}
/// <summary>
/// Get Principal's Name by identification
/// </summary>
/// <param name="web"></param>
/// <param name="identification">Can be PrincipalId, SPFieldUser, SPFieldUserValue(lookupformat), LoginName, Name or SPRoleAssigment</param>
/// <returns>Name or null</returns>
public static string GetSPPrincipalName(this SPWeb web, object identification)
{
if (web == null) throw new ArgumentNullException("web");
if (identification == null) throw new ArgumentNullException("identification");
SPPrincipal principal = GetSPPrincipal(web, identification);
return principal != null ? principal.Name : null;
}
/// <summary>
/// Get SPUser by identification
/// </summary>
/// <param name="web"></param>
/// <param name="identification">
/// Can be UserId, SPFieldUser, SPFieldUserValue(lookupformat), LoginName, Name or SPRoleAssigment.
/// If identification is SPFieldUserValueCollection or SPRoleAssignmentCollection returns null.
/// </param>
/// <returns>SPUser or null</returns>
public static SPUser GetSPUser(this SPWeb web, object identification)
{
if (web == null) throw new ArgumentNullException("web");
if (identification == null) throw new ArgumentNullException("identification");
string identAsString = identification.ToString().Trim();
switch (identification.GetType().Name)
{
case "SPGroup":
return null;
case "SPUser":
return (SPUser) identification;
case "SPFieldUserValue":
return ( (SPFieldUserValue) identification ).User;
case "SPFieldUserValueCollection":
SPFieldUserValueCollection usrcol = (SPFieldUserValueCollection) identification;
return usrcol.Count == 1 ? GetSPUser(web, usrcol[0]) : null;
case "SPRoleAssignmentCollection":
SPRoleAssignmentCollection rolecol = (SPRoleAssignmentCollection) identification;
return rolecol.Count == 1 ? GetSPUser(web, rolecol[0]) : null;
case "SPRoleAssignment":
SPRoleAssignment roleAssignment = (SPRoleAssignment) identification;
return roleAssignment.Member is SPUser ? web.SiteUsers.GetByID(roleAssignment.Member.ID) : null;
}
try
{
return web.SiteUsers.GetByID(Convert.ToInt32(identification));
}
// ReSharper disable once EmptyGeneralCatchClause
catch {}
if (identAsString.IndexOf("#", StringComparison.InvariantCulture) > -1) // varianta s pouzitim lookup hodnoty
{
try
{
return web.SiteUsers.GetByID(Convert.ToInt32(identAsString.GetLookupIndex()));
}
// ReSharper disable once EmptyGeneralCatchClause
catch {}
}
try
{
if (identAsString.StartsWith("-1;#i")) identAsString = identAsString.Replace("-1;#i:", "i:");
return web.SiteUsers[identAsString];
}
// ReSharper disable once EmptyGeneralCatchClause
catch {}
// resolve principal - both Login and Name
try
{
SPPrincipalInfo principal = SPUtility.ResolvePrincipal(web, identAsString, SPPrincipalType.User, SPPrincipalSource.All, null, false);
if (principal != null)
{
if (principal.PrincipalId == -1)
{
SPUser user = null;
web.RunWithAllowUnsafeUpdates(delegate
{
user = web.EnsureUser(principal.LoginName);
});
return user;
}
return web.SiteUsers[principal.LoginName];
}
}
// ReSharper disable once EmptyGeneralCatchClause
catch {} // in case we get handed a string similar to claim, there's an exception
//may be a domain group
try
{
SPPrincipalInfo principal = SPUtility.ResolvePrincipal(web, identAsString, SPPrincipalType.SecurityGroup, SPPrincipalSource.All, null, false);
if (principal != null)
{
if (principal.PrincipalId == -1)
{
SPUser user = null;
web.RunWithAllowUnsafeUpdates(delegate
{
user = web.EnsureUser(principal.LoginName);
});
return user;
}
return web.SiteUsers.GetByID(principal.PrincipalId);
}
}
// ReSharper disable once EmptyGeneralCatchClause
catch {} // in case we get handed a string similar to claim, there's an exception
return null;
}
/// <summary>
/// Get SPGroup by identification
/// </summary>
/// <param name="web"></param>
/// <param name="identification">
/// Can be GroupId, SPFieldUserValue(lookupformat), LoginName, Name or SPRoleAssigment.
/// If identification is SPFieldUserValueCollection or SPRoleAssignmentCollection returns null.
/// </param>
/// <returns>SPGroup or null</returns>
public static SPGroup GetSPGroup(this SPWeb web, object identification)
{
if (web == null) throw new ArgumentNullException("web");
if (identification == null) throw new ArgumentNullException("identification");
switch (identification.GetType().Name)
{
case "SPGroup":
return (SPGroup) identification;
case "SPUser":
return null;
case "SPFieldUserValue":
try
{
return web.SiteGroups.GetByID(( (SPFieldUserValue) identification ).LookupId);
}
catch (Exception)
{
return null;
}
case "SPFieldUserValueCollection":
case "SPRoleAssignmentCollection":
return null;
case "SPRoleAssignment":
{
SPRoleAssignment roleAssignment = (SPRoleAssignment) identification;
if (roleAssignment.Member is SPGroup)
{
try
{
return web.SiteGroups.GetByID(roleAssignment.Member.ID);
}
// ReSharper disable once EmptyGeneralCatchClause
catch (Exception) {}
}
else
{
return null;
}
break;
}
}
try
{
return web.SiteGroups.GetByID(Convert.ToInt32(identification));
}
// ReSharper disable once EmptyGeneralCatchClause
catch {}
string identificationString = identification.ToString();
if (identificationString.IndexOf("#", StringComparison.InvariantCulture) > -1) // varianta s pouzitim lookup hodnoty
{
try
{
return web.SiteGroups.GetByID(Convert.ToInt32(identificationString.GetLookupIndex()));
}
// ReSharper disable once EmptyGeneralCatchClause
catch {}
}
try
{
return web.SiteGroups[identificationString];
}
// ReSharper disable once EmptyGeneralCatchClause
catch {}
// varianta s resolve principal - resolvne jak login tak Name
string groupStr = identificationString.GetLookupValue().Trim();
SPPrincipalInfo principal = SPUtility.ResolvePrincipal(web, groupStr, SPPrincipalType.SharePointGroup, SPPrincipalSource.All, null, false);
if (principal != null)
{
return web.SiteGroups.GetByID(principal.PrincipalId);
}
return null;
}
/// <summary>
/// Get SPPrincipal(SPGroup or SPUser) by identification
/// </summary>
/// <param name="web"></param>
/// <param name="identification">
/// Can be UserId, GroupId, SPFieldUser, SPFieldUserValue(lookupformat), LoginName, Name or SPRoleAssigment.
/// If identification is SPFieldUserValueCollection or SPRoleAssignmentCollection returns null
/// </param>
/// <returns>SPPrincipal or null</returns>
public static SPPrincipal GetSPPrincipal(this SPWeb web, object identification)
{
if (web == null) throw new ArgumentNullException("web");
if (identification == null) throw new ArgumentNullException("identification");
if (identification.GetType().Name.EqualAny(new[] { "SPFieldUserValueCollection", "SPRoleAssignmentCollection" })) return null;
if (identification is SPPrincipal) return (SPPrincipal) identification;
SPPrincipal principal = (SPPrincipal) GetSPGroup(web, identification) ?? GetSPUser(web, identification);
return principal;
}
/// <summary>
/// Get Principals defined by array of identifications
/// </summary>
/// <param name="web"></param>
/// <param name="identification"></param>
/// <returns>List of SPPrincipal, which are not duplicated and was susscesfully load. List does not contains null elements.</returns>
public static List<SPPrincipal> GetSPPrincipals(this SPWeb web, object identification)
{
if (web == null) throw new ArgumentNullException("web");
if (identification == null) throw new ArgumentNullException("identification");
return GetSPPrincipals(web, new List<object> { identification });
}
/// <summary>
/// Get Principals defined by array of identifications or straight by string
/// </summary>
/// <param name="web"></param>
/// <param name="identifications"></param>
/// <returns>List of SPPrincipal, which are not duplicated and was susscesfully load. List does not contains null elements.</returns>
public static List<SPPrincipal> GetSPPrincipals(this SPWeb web, IEnumerable identifications)
{
if (web == null) throw new ArgumentNullException("web");
if (identifications == null) throw new ArgumentNullException("identifications");
List<SPPrincipal> principals = new List<SPPrincipal>();
using (new SPMonitoredScope("GetSPPrincipals"))
{
// ReSharper disable PossibleMultipleEnumeration
if (identifications is string) return GetSPPrincipals(web, new[] { identifications }); //Muze byt primo string
List<string> processLog = new List<string>();
try
{
foreach (object identification in identifications.Cast<object>().Where(i => i != null))
{
string identAsString = identification.ToString();
if (identification is SPRoleAssignment)
{
processLog.Add(( (SPRoleAssignment) identification ).Member.ToString());
}
else
{
processLog.Add(identification.ToString());
}
if (identification is SPRoleAssignmentCollection)
{
SPRoleAssignmentCollection col = (SPRoleAssignmentCollection) identification;
principals.AddRange(col.Cast<SPRoleAssignment>().Select(role => role.Member));
}
else if (identAsString.Contains(";#")) //Muze byt primo string
{
SPFieldUserValueCollection col = new SPFieldUserValueCollection(web, identification.ToString());
principals.AddRange(col.Select(userValue => GetSPPrincipal(web, userValue)));
}
else if (identAsString.Contains(";")) // melo by se pouzivat pokud mame pouze balik loginu oddelenych strednikem
{
string[] spl = identAsString.SplitByChars(";");
principals.AddRange(spl.Select(userValue => GetSPPrincipal(web, userValue)));
}
else
{
principals.Add(GetSPPrincipal(web, identification));
}
}
}
catch (Exception exc)
{
throw new Exception("GetSPPrincipals exception, users processed (last is the culprit): \n" + processLog.JoinStrings(", ") + "\nOriginal exception:\n" + exc + "\n" + exc.StackTrace);
}
principals.RemoveAll(p => p == null); //odstranime vyskyty, ktere se nepodarilo nacist
// ReSharper restore PossibleMultipleEnumeration
return principals.Distinct(new SPPrincipalComparer()).ToList();
}
}
public static List<SPUser> GetSPUsersFromADGroup(this SPWeb web, string loginName, List<string> loadedLogins = null, int depth = 0)
{
if (web == null) throw new ArgumentNullException("web");
if (loginName == null) throw new ArgumentNullException("loginName");
using (new SPMonitoredScope("GetSPUsersFromADGroup: " + loginName))
{
bool reachMax;
List<SPUser> users = new List<SPUser>();
SPPrincipalInfo[] principals;
try
{
principals = SPUtility.GetPrincipalsInGroup(web, loginName, 10000, out reachMax);
}
catch (InvalidOperationException) // claims doesn't work with GetPrincipalsInGroup
{
return users;
}
if (principals == null) return users;
if (reachMax) throw new Exception("Dosahnut maximalni limit! Pravdepodobne rekurze v AD"); //TODO rekurzi resime zvlast takze to bude jeste neco jineho
List<string> logins = principals.Select(p => p.LoginName).ToList();
foreach (string login in logins)
{
SPUser user = web.GetSPUser(login);
if (user == null) continue; //uzivatel muze byt disabled pak ho nenactem
if (user.IsDomainGroup)
{
if (loadedLogins == null) loadedLogins = new List<string>();
if (loadedLogins.Contains(loginName))
{
var loadedLoginsInfo = "Recursion usage of Group in Active Directory!!\n";
foreach (string loadedLogin in loadedLogins)
{
SPUser lu = web.GetSPUser(loadedLogin);
loadedLoginsInfo += "[" + lu.ID + "] - [ " + lu.Name + "] - [" + lu.LoginName + "]\n";
}
throw new System.DirectoryServices.ActiveDirectory.ActiveDirectoryObjectExistsException(loadedLoginsInfo);
}
loadedLogins.Add(loginName);
users.AddRange(GetSPUsersFromADGroup(web, login, loadedLogins, depth++));
loadedLogins.Remove(loginName);
}
else
{
users.Add(user);
}
}
return users.Distinct(new SPUserComparer()).ToList();
}
}
/// <summary>
/// Get SPUsers defined by array of identifications. If identification is group, all users from that will be returns
/// </summary>
/// <param name="web"></param>
/// <param name="identification"></param>
/// <returns></returns>
public static List<SPUser> GetSPUsers(this SPWeb web, object identification)
{
if (web == null) throw new ArgumentNullException("web");
if (identification == null) throw new ArgumentNullException("identification");
return GetSPUsers(web, new[] { identification });
}
/// <summary>
/// Get SPUsers defined by array of identifications or straight by string. If identification is group, all users from that will be returns
/// </summary>
/// <param name="web"></param>
/// <param name="identifications"></param>
/// <returns>List with users or empty list. Never returns null</returns>
public static List<SPUser> GetSPUsers(this SPWeb web, IEnumerable identifications)
{
if (web == null) throw new ArgumentNullException("web");
if (identifications == null) throw new ArgumentNullException("identifications");
List<SPUser> users = new List<SPUser>();
using (new SPMonitoredScope("GetSPUsers"))
{
foreach (SPPrincipal principal in GetSPPrincipals(web, identifications))
{
using (new SPMonitoredScope("Process SPPrincipal - " + principal.LoginName))
{
if (principal is SPUser)
{
SPUser user = (SPUser) principal;
// TODO resit, ze muze byt SP skupina se stejnym jmenem jako AD skupina, a v SP skupine muze byt AD skupina + k ni jeste dalsi uzivatel
// a v tu chvili se nacte jen AD skupina, a co k ni je v te SP skupine uz se nezobrazi
if (user.IsDomainGroup) //nacteme uzivatele z AD skupiny
{
users.AddRange(GetSPUsersFromADGroup(web, user.LoginName));
}
else
{
users.Add(user); //obycejny SPUSer
}
}
else
{
SPGroup group = (SPGroup) principal;
users.AddRange(group.GetSPUsers()); //SP skupiny rozepiseme
}
}
}
}
return users.Distinct(new SPUserComparer()).ToList();
}
#endregion
/// <summary>
/// Checks if we're running in sandbox
/// </summary>
/// <param name="web"></param>
/// <returns></returns>
public static bool InSandbox(this SPWeb web)
{
return AppDomain.CurrentDomain.FriendlyName.Contains("Sandbox");
}
/// <summary>
/// Checks if we're running in OWSTIMER
/// </summary>
/// <param name="web"></param>
/// <returns></returns>
public static bool InTimerJob(this SPWeb web)
{
return System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToLowerInvariant() == "owstimer";
}
#region OpenList
/// <summary>
/// Tries to open list with given guid - mostly for usage in scripts
/// </summary>
/// <param name="web"></param>
/// <param name="listGuid"></param>
/// <param name="throwExc"></param>
/// <returns>list or null</returns>
/// <exception cref="SPListAccesDeniedException"></exception>
/// <exception cref="SPListNotFoundException"></exception>
public static SPList OpenList(this SPWeb web, Guid listGuid, bool throwExc = false)
{
if (web == null) throw new ArgumentNullException("web");
if (listGuid == null) throw new ArgumentNullException("listGuid");
if (listGuid.IsEmpty()) throw new ArgumentException("Guid is Empty");
return OpenListFromGuid(web, listGuid, throwExc);
}
/// <summary>
/// Tries to open a list by name, "internal name" (part of url, since there's no official internal name for lists in SP)
/// or Guid in string
/// </summary>
/// <param name="web"></param>
/// <param name="listIdentification"></param>
/// <param name="throwExc"></param>
/// <returns>list or null</returns>
/// <exception cref="SPListAccesDeniedException"></exception>
/// <exception cref="SPListNotFoundException"></exception>
public static SPList OpenList(this SPWeb web, string listIdentification, bool throwExc = false)
{
if (web == null) throw new ArgumentNullException("web");
if (listIdentification == null) throw new ArgumentNullException("listIdentification");
string cacheKey = "OpenlistDict_" + web.Site.ID + web.ID;
using (new SPMonitoredScope("OpenList - " + listIdentification))
{
Guid listGuid;
#region Open By Guid
if (Guid.TryParse(listIdentification, out listGuid))
{
return OpenListFromGuid(web, listGuid, throwExc);
}
#endregion
#region Open by InternalName
string listInternalName = SPListExtensions.ListInternalName(listIdentification.Trim());
Dictionary<string, Guid> result = (Dictionary<string, Guid>) HttpRuntime.Cache.Get(cacheKey);
if (result == null)
{
//Nacteme vse pro celej web
web.RunElevated(delegate(SPWeb elevatedWeb)
{
result = elevatedWeb.Lists.Cast<SPList>().ToDictionary(d => d.InternalName(), d => d.ID);
HttpRuntime.Cache.Insert(cacheKey, result, null, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
return null;
});
}
if (result.ContainsKey(listInternalName))
{
return OpenListFromGuid(web, result[listInternalName], throwExc);
}
#endregion
if (!throwExc) return null;
throw new SPListNotFoundException(listIdentification, web);
}
}
public static SPList OpenListFromGuid(SPWeb web, Guid listGuid, bool throwExc)
{
try
{
bool originalCatchValue = SPSecurity.CatchAccessDeniedException;
SPSecurity.CatchAccessDeniedException = false;
SPList list = web.Lists[listGuid];
SPSecurity.CatchAccessDeniedException = originalCatchValue;
return list;
}
catch (UnauthorizedAccessException)
{
if (throwExc)
{
throw new SPListAccesDeniedException(listGuid, web);
}
}
catch (Exception) {}
if (throwExc)
{
throw new SPListNotFoundException(listGuid, web);
}
return null;
}
#endregion
public static void RunWithAllowUnsafeUpdates(this SPWeb web, Action func)
{
if (web == null) throw new ArgumentNullException("web");
if (func == null) throw new ArgumentNullException("func");
if (web.AllowUnsafeUpdates) func();
else
{
web.AllowUnsafeUpdates = true;
func();
web.AllowUnsafeUpdates = false;
}
}
/// <summary>
/// Runs code as elevated admin
/// </summary>
/// <param name="web"></param>
/// <param name="func"></param>
/// <returns></returns>
public static object RunElevated(this SPWeb web, Func<SPWeb, object> func)
{
if (web == null) throw new ArgumentNullException("web");
if (func == null) throw new ArgumentNullException("func");
Guid siteGuid = web.Site.ID;
Guid webGuid = web.ID;
object result = null;
if (web.InSandbox() || web.CurrentUser.IsSiteAdmin) //Don't runeleveted if user is admin or already run with admin rights
{
bool origSiteunsafe = web.Site.AllowUnsafeUpdates;
web.Site.AllowUnsafeUpdates = true;
web.RunWithAllowUnsafeUpdates(delegate { result = func(web); });
web.Site.AllowUnsafeUpdates = origSiteunsafe;
}
else
{
using (new SPMonitoredScope("RunElevated SPWeb - " + web.Url))
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite elevatedSite = new SPSite(siteGuid, web.Site.GetSystemToken()))
{
elevatedSite.AllowUnsafeUpdates = true;
using (SPWeb elevatedWeb = elevatedSite.OpenW(webGuid, true))
{
elevatedWeb.AllowUnsafeUpdates = true;
result = func(elevatedWeb);
elevatedWeb.AllowUnsafeUpdates = false;
}
elevatedSite.AllowUnsafeUpdates = false;
}
});
}
}
return result;
}
}
}