/
sp.py
53 lines (37 loc) · 1.1 KB
/
sp.py
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
# Helper Functions
# Enumeration
#
# These are simple enumeration methods for walking over various SharePoint
# objects and collections.
def _enum(col, fn):
"""Enumerate a collection and call function fn for each item."""
for x in col:
fn(x)
def enum_sites(webapp, fn):
"""
Enumerate all site collections in the specified web application
and call the specified function with each site collection.
"""
# just in case we were passed a URL, get the web app
webapp = get_webapplication(webapp)
_enum(webapp.Sites, fn)
# Get Object Helper Methods
# These methods take in some sort of object identifier (usually a URL)
# and return the appropriate object instance
def get_webapplication(url):
"""Gets a web application by its URL"""
if type(url) is SPWebApplication:
return url
return SPWebApplication.Lookup(Uri(url))
def get_site(url):
"""Gets a site collection by its URL"""
if type(url) is SPSite:
return url
return SPSite(url)
def get_web(url):
"""Gets a web by its URL"""
if type(url) is SPWeb:
return url
if type(url) is SPSite:
return url.RootWeb
return SPSite(url).OpenWeb(url)