|
1 |
| -import settings |
| 1 | +from settings import settings |
2 | 2 | from faker import Faker
|
3 |
| - |
4 |
| -from office365.runtime.auth.authentication_context import AuthenticationContext |
| 3 | +from office365.runtime.auth.UserCredential import UserCredential |
5 | 4 | from office365.sharepoint.client_context import ClientContext
|
| 5 | +from office365.sharepoint.list_creation_information import ListCreationInformation |
| 6 | +from office365.sharepoint.list_template_type import ListTemplateType |
6 | 7 |
|
7 | 8 |
|
8 |
| -def generate_tasks(context): |
9 |
| - tasks_list = ctx.web.lists.get_by_title("Tasks") |
10 |
| - for idx in range(0, 10): |
11 |
| - title = "Task{0}".format(idx) |
12 |
| - task_properties = {'__metadata': {'type': 'SP.Data.TasksListItem'}, 'Title': title} |
13 |
| - task_item = tasks_list.add_item(task_properties) |
14 |
| - context.execute_query() |
15 |
| - print("Task '{0}' has been created".format(task_item.properties["Title"])) |
| 9 | +def ensure_list(web, list_properties): |
| 10 | + ctx = web.context |
| 11 | + lists = web.lists.filter("Title eq '{0}'".format(list_properties.Title)) |
| 12 | + ctx.load(lists) |
| 13 | + ctx.execute_query() |
| 14 | + if len(lists) == 1: |
| 15 | + return lists[0] |
| 16 | + target_list = web.lists.add(list_properties) |
| 17 | + ctx.execute_query() |
| 18 | + return target_list |
16 | 19 |
|
17 | 20 |
|
18 | 21 | def generate_contacts(context):
|
19 |
| - contacts_list = ctx.web.lists.get_by_title("Contacts") |
| 22 | + contacts_list = ensure_list(context.web, |
| 23 | + ListCreationInformation("Contacts_Large", |
| 24 | + None, |
| 25 | + ListTemplateType.Contacts) |
| 26 | + ) |
| 27 | + |
20 | 28 | fake = Faker()
|
21 |
| - for idx in range(0, 1): |
22 |
| - name = fake.name() |
23 |
| - contact_properties = {'__metadata': {'type': 'SP.Data.ContactsListItem'}, 'Title': name} |
| 29 | + for idx in range(0, 100): |
| 30 | + contact_properties = { |
| 31 | + |
| 32 | + 'Title': fake.name(), |
| 33 | + 'FullName': fake.name(), |
| 34 | + 'Email': fake.email(), |
| 35 | + 'Company': fake.company(), |
| 36 | + 'WorkPhone': fake.phone_number(), |
| 37 | + 'WorkAddress': fake.street_address(), |
| 38 | + 'WorkCity': fake.city(), |
| 39 | + 'WorkZip': fake.postcode(), |
| 40 | + 'WorkCountry': fake.country(), |
| 41 | + 'WebPage': {'Url': fake.url()} |
| 42 | + } |
24 | 43 | contact_item = contacts_list.add_item(contact_properties)
|
25 | 44 | context.execute_query()
|
26 | 45 | print("Contact '{0}' has been created".format(contact_item.properties["Title"]))
|
27 | 46 |
|
28 | 47 |
|
29 | 48 | if __name__ == '__main__':
|
30 |
| - ctx_auth = AuthenticationContext(url=settings['url']) |
31 |
| - if ctx_auth.acquire_token_for_user(username=settings['username'], password=settings['password']): |
32 |
| - ctx = ClientContext(settings['url'], ctx_auth) |
33 |
| - generate_tasks(ctx) |
34 |
| - # generate_contacts(ctx) |
35 |
| - else: |
36 |
| - print(ctx_auth.get_last_error()) |
| 49 | + ctx = ClientContext.connect_with_credentials("https://mediadev8.sharepoint.com/sites/team", |
| 50 | + UserCredential(settings['user_credentials']['username'], |
| 51 | + settings['user_credentials']['password'])) |
| 52 | + generate_contacts(ctx) |
0 commit comments