Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Started adding helper functions
- Loading branch information
Glen Cooper
committed
May 17, 2008
1 parent
3b2fb28
commit da3318a
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) |