glenc / sp.py

A Python library for working with the SharePoint object model

sp.py / src / setprofileprop.py
100644 72 lines (48 sloc) 1.683 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
# setprofileprop.py
"""
Set Profile Property
This script will set a property on all user profiles based on the pattern provided.
 
Usage:
 
ipy setprofileprop.py --url http://myserver --prop Picture
--value http://myserver/pics/{Alias}.jpg
 
Arguments:
 
url - server url
prop - property to set
value - value to set the property to
[--help] - show this help
 
"""
 
import sp
from sp import sharedservices
import re
import sys
import scriptutil
 
__all__ = ["set_profile_prop"]
 
def main(argv):
args = scriptutil.getargs(argv, ["url=", "prop=", "value="], [], __doc__, True)
set_profile_prop(args["url"], args["prop"], args["value"])
 
 
def set_profile_prop(url, prop, value):
"""Executes the script"""
 
# walk over all profiles and call apply_value
sharedservices.enum_profiles(url, lambda p: _apply_value(p, prop, value))
 
 
def _apply_value(profile, prop, value):
"""Applies the value to the profile provided"""
 
print "Processing", profile["AccountName"]
 
# take the value, locate any {key} patterns, then
# replace each with the property from the profile
regex = re.compile("{(.*?)}")
for match in regex.finditer(value):
key = match.group(1)
 
# handle special case where 'Alias' maps to 'AccountName'
trim = False
if key == "Alias":
key = "AccountName"
trim = True
 
val = profile[key].Value
 
if trim == True:
val = val.split("\\")[1]
 
value = value.replace(match.group(0), val)
 
# how apply updated value to the profile property
profile[prop].Value = value
profile.Commit()
 
 
 
if __name__ == '__main__':
main(sys.argv[1:])