This repository was 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
/
Copy pathSPSite.cs
534 lines (473 loc) · 20.6 KB
/
SPSite.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
/* 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.IO;
using System.Linq;
using System.Web;
using System.Web.Caching;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
namespace Navertica.SharePoint.Extensions
{
public static class SPSiteExtensions
{
/// <summary>
/// Returns first positive list identification for a given list name or guid, starting on rootweb and then recursively walking the subwebs
/// under elevated privileges (if possible - not sandboxed).
/// </summary>
/// <param name="site"></param>
/// <param name="listNameOrGuid"></param>
/// <returns>WebListId</returns>
public static WebListId FindList(this SPSite site, string listNameOrGuid)
{
using (new SPMonitoredScope("FindList: " + listNameOrGuid))
{
if (site == null) throw new ArgumentNullException("site");
if (listNameOrGuid == null) throw new ArgumentNullException("listNameOrGuid");
if (listNameOrGuid == string.Empty) return new WebListId();
bool set = false;
string cacheName = "FindListSite_" + site.ID + "_" + listNameOrGuid;
WebListId result = (WebListId) HttpRuntime.Cache.Get(cacheName);
if (result != null && !site.InTimerJob())
{
return new WebListId(result.WebGuid, result.ListGuid);
}
SPList list;
result = site.RootWeb.FindList(listNameOrGuid);
if (result != null && result.IsValid) set = true;
if (!set)
{
site.RunElevated(delegate(SPSite elevatedSite)
{
for (int i = 0; i < elevatedSite.AllWebs.Count; i++)
{
try
{
using (SPWeb otherWeb = elevatedSite.AllWebs[i])
{
list = otherWeb.OpenList(listNameOrGuid);
if (list != null)
{
result = new WebListId(list);
return null;
}
}
}
// ReSharper disable once EmptyGeneralCatchClause
catch (Exception) {}
}
return null;
});
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 '" + listNameOrGuid + "' at site: " + site.Url };
}
}
/// <summary>
/// Finds all lists of given listType in given site and its webs
/// </summary>
/// <param name="site"></param>
/// <param name="listType"></param>
/// <returns></returns>
public static WebListDictionary FindListsWithBaseTemplate(this SPSite site, SPListTemplateType listType)
{
if (site == null) throw new ArgumentNullException("site");
using (new SPMonitoredScope("FindListsWithBaseTemplate at site " + site.Url + " listType: " + listType))
{
WebListDictionary foundLists = new WebListDictionary();
site.RunElevated(delegate(SPSite elevatedSite)
{
for (int i = 0; i < elevatedSite.AllWebs.Count; i++)
{
using (SPWeb web = elevatedSite.AllWebs[i])
{
foundLists.AddRange(web.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 site and its webs
/// </summary>
/// <param name="site"></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 SPSite site, string contentTypeId)
{
if (site == null) throw new ArgumentNullException("site");
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(site, loadedCtId);
}
/// <summary>
/// Finds all lists containing content types with ids beginning with given ContentTypeId in given site and its webs
/// </summary>
/// <param name="site"></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 SPSite site, SPContentTypeId contentTypeId)
{
if (site == null) throw new ArgumentNullException("site");
if (contentTypeId == null) throw new ArgumentNullException("contentTypeId");
WebListDictionary foundLists = new WebListDictionary();
using (new SPMonitoredScope("FindListsWithContentType at site " + site.Url + " contentTypeId: " + contentTypeId))
{
site.RunElevated(delegate(SPSite elevatedSite)
{
for (int i = 0; i < elevatedSite.AllWebs.Count; i++)
{
using (SPWeb otherWeb = elevatedSite.AllWebs[i])
{
foreach (SPList list in otherWeb.Lists)
{
try
{
if (list.ContentTypes.BestMatch(contentTypeId).IsChildOf(contentTypeId)) //if contentTypeId is "0x0108" it will add the children too
{
foundLists.Add(list);
}
}
// ReSharper disable once EmptyGeneralCatchClause
catch {}
}
}
}
return null;
});
}
return foundLists;
}
/// <summary>
/// Returns WebListDictionary of all lists containing field with specified internal name
/// </summary>
/// <param name="site"></param>
/// <param name="intFieldName"></param>
/// <returns></returns>
public static WebListDictionary FindListsWithField(this SPSite site, string intFieldName)
{
if (site == null) throw new ArgumentNullException("site");
if (intFieldName == null) throw new ArgumentNullException("intFieldName");
WebListDictionary foundLists = new WebListDictionary();
site.RunElevated(delegate(SPSite elevatedSite)
{
for (int i = 0; i < elevatedSite.AllWebs.Count; i++)
{
using (SPWeb otherWeb = elevatedSite.AllWebs[i])
{
foreach (SPList list in otherWeb.Lists)
{
try
{
if (list.ContainsFieldIntName(intFieldName))
{
foundLists.Add(list);
}
}
// ReSharper disable once EmptyGeneralCatchClause
catch {}
}
}
}
return null;
});
return foundLists;
}
/// <summary>
/// Returns WebListItemId for both absolute and relative url of a document (not a list item)
/// </summary>
/// <param name="site"></param>
/// <param name="url">a</param>
/// <returns>WebListItemId</returns>
/// <exception cref="SPWebNotFoundException"></exception>
/// <exception cref="SPWebAccesDeniedException"></exception>
public static WebListItemId GetItemByUrl(this SPSite site, string url)
{
if (site == null) throw new ArgumentNullException("site");
if (url == null) throw new ArgumentNullException("url");
WebListItemId result = new WebListItemId();
using (SPWeb web = site.OpenW(url, true))
{
try
{
result = new WebListItemId(web.GetListItem(url));
}
catch (Exception e)
{
result.InvalidMessage = e.ToString();
}
}
return result;
}
/// <summary>
/// Gets the admin token
/// http://dotnetfollower.com/wordpress/tag/runwithelevatedprivileges/
/// </summary>
/// <param name="site"></param>
/// <returns></returns>
public static SPUserToken GetSystemToken(this SPSite site)
{
if (site == null) throw new ArgumentNullException("site");
SPUserToken res = null;
bool originalCatchValue = SPSecurity.CatchAccessDeniedException;
try
{
SPSecurity.CatchAccessDeniedException = false;
res = site.SystemAccount.UserToken;
}
catch (UnauthorizedAccessException)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite elevatedSPSite = new SPSite(site.ID))
{
res = elevatedSPSite.SystemAccount.UserToken; // (***)
}
});
}
finally
{
SPSecurity.CatchAccessDeniedException = originalCatchValue;
}
return res;
}
/// <summary>
/// Checks if we're running in sandbox
/// </summary>
/// <param name="site"></param>
/// <returns></returns>
public static bool InSandbox(this SPSite site)
{
return AppDomain.CurrentDomain.FriendlyName.Contains("Sandbox");
}
/// <summary>
/// Checks if we're running in OWSTIMER
/// </summary>
/// <param name="site"></param>
/// <returns></returns>
public static bool InTimerJob(this SPSite site)
{
return System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToLowerInvariant() == "owstimer";
}
#region OpenW
/// <summary>
/// Return an open web identified by Guid - fallback solution for IronPython scripts in case there's a Guid instead of string.
/// Resulting SPWeb has to be DISPOSED MANUALLY
/// </summary>
/// <param name="site"></param>
/// <param name="webGuid"></param>
/// <param name="throwExc">should failure to open web throw Exception</param>
/// <returns>open SPWeb or null</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="SPWebAccesDeniedException"></exception>
/// <exception cref="SPWebNotFoundException"></exception>
public static SPWeb OpenW(this SPSite site, Guid webGuid, bool throwExc = false)
{
if (site == null) throw new ArgumentNullException("site");
if (webGuid == null) throw new ArgumentNullException("webGuid");
if (webGuid.IsEmpty()) throw new ArgumentException("Guid is Empty");
return OpenW(site, webGuid.ToString(), throwExc);
}
/// <summary>
/// Return an open web identified by name, url or Guid in string.
/// Resulting SPWeb has to be DISPOSED MANUALLY
/// </summary>
/// <param name="site"></param>
/// <param name="webText"></param>
/// <param name="throwExc">should failure to open web throw Exception</param>
/// <param name="requireExactUrl">url http://portal/RiskPoint/default.aspx with true is null, with false will return SPWeb</param>
/// <returns>open SPWeb or null</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="SPWebAccesDeniedException"></exception>
/// <exception cref="SPWebNotFoundException"></exception>
public static SPWeb OpenW(this SPSite site, string webText, bool throwExc = false, bool requireExactUrl = false)
{
if (site == null) throw new ArgumentNullException("site");
if (webText == null) throw new ArgumentNullException("webText");
using (new SPMonitoredScope("OpenW - " + webText))
{
bool originalCatchValue = SPSecurity.CatchAccessDeniedException;
Guid webGuid = Guid.Empty;
try
{
webGuid = new Guid(webText);
}
// ReSharper disable once EmptyGeneralCatchClause
catch {}
if (!webGuid.IsEmpty())
{
try
{
SPSecurity.CatchAccessDeniedException = false;
SPWeb webG = site.OpenWeb(webGuid);
SPSecurity.CatchAccessDeniedException = originalCatchValue;
if (webG.Exists) return webG;
}
catch (UnauthorizedAccessException)
{
if (throwExc) throw new SPWebAccesDeniedException(webGuid, site);
}
catch (FileNotFoundException)
{
if (throwExc) throw new SPWebNotFoundException(webGuid, site);
}
catch (DirectoryNotFoundException) //vyhazuje kdyz mam guid z jiny site
{
if (throwExc) throw new SPWebNotFoundException(webGuid, site);
}
catch
{
if (throwExc) throw;
}
return null;
}
webText = webText.Replace(site.Url, "").Trim();
bool dispose = false;
SPWeb web = null;
try
{
SPSecurity.CatchAccessDeniedException = false;
try
{
web = site.OpenWeb(webText, requireExactUrl);
}
catch (ArgumentException)
{
if (webText.StartsWith("/"))
{
webText = webText.Remove(0, 1);
web = site.OpenWeb(webText, requireExactUrl);
}
}
if (web != null)
{
// ReSharper disable once UnusedVariable
string webTitle = web.Title; // web can be not null and at the same time throw either UnauthorizedAccessException or FileNotFoundException
if (web.Exists) return web;
}
}
catch (UnauthorizedAccessException)
{
dispose = true;
if (throwExc) throw new SPWebAccesDeniedException(webText, site);
}
catch (FileNotFoundException)
{
dispose = true;
if (throwExc) throw new SPWebNotFoundException(webText, site);
}
catch
{
dispose = true;
if (throwExc) throw;
}
finally
{
if (dispose && web != null) web.Dispose();
SPSecurity.CatchAccessDeniedException = originalCatchValue;
}
}
return null;
}
#endregion
/// <summary>
/// Run code as elevated admin
/// </summary>
/// <param name="site"></param>
/// <param name="func"></param>
/// <returns></returns>
public static object RunElevated(this SPSite site, Func<SPSite, object> func)
{
if (site == null) throw new ArgumentNullException("site");
if (func == null) throw new ArgumentNullException("func");
Guid siteGuid = site.ID;
object result = null;
if (site.InSandbox() || site.RootWeb.CurrentUser.IsSiteAdmin)
{
bool origSiteunsafe = site.AllowUnsafeUpdates;
site.AllowUnsafeUpdates = true;
result = func(site);
site.AllowUnsafeUpdates = origSiteunsafe;
}
else
{
using (new SPMonitoredScope("RunElevated SPSite - " + site.Url))
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite elevatedSite = new SPSite(siteGuid, GetSystemToken(site)))
{
elevatedSite.AllowUnsafeUpdates = true;
result = func(elevatedSite);
elevatedSite.AllowUnsafeUpdates = false;
}
});
}
}
return result;
}
/// <summary>
/// Run code as custom user
/// </summary>
/// <param name="site"></param>
/// <param name="user"> </param>
/// <param name="func"></param>
/// <returns></returns>
public static object RunAsUser(this SPSite site, SPUser user, Func<SPSite, object> func)
{
if (site == null) throw new ArgumentNullException("site");
if (user == null) throw new ArgumentNullException("user");
if (func == null) throw new ArgumentNullException("func");
Guid siteGuid = site.ID;
object result;
SPUserToken token = user.UserToken;
if (site.InSandbox())
{
bool origSiteunsafe = site.AllowUnsafeUpdates;
site.AllowUnsafeUpdates = true;
result = func(site);
site.AllowUnsafeUpdates = origSiteunsafe;
}
else
{
using (new SPMonitoredScope("SPSite - RunAsUser"))
{
using (SPSite alternativeSite = new SPSite(siteGuid, token))
{
alternativeSite.AllowUnsafeUpdates = true;
result = func(alternativeSite);
alternativeSite.AllowUnsafeUpdates = false;
}
}
}
return result;
}
}
}