Skip to content

Commit bf40110

Browse files
committed
Ensure mandatory entity type for LisiItem, Readme updates
1 parent 1df3ec9 commit bf40110

File tree

5 files changed

+55
-29
lines changed

5 files changed

+55
-29
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The list of supported API versions:
3535

3636
The following auth flows are supported:
3737

38-
- app principals flow: `AuthenticationContext.ctx_auth.acquire_token_for_app(client_id, client_secret)` (refer [Granting access using SharePoint App-Only] (https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs) for a details)
38+
- app principals flow: `AuthenticationContext.ctx_auth.acquire_token_for_app(client_id, client_secret)` (refer [Granting access using SharePoint App-Only](https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs) for a details)
3939
- user credentials flow:`AuthenticationContext.ctx_auth.acquire_token_for_user(username, password)`
4040
- certificate credentials flow `ClientContext.connect_with_certificate(site_url, client_id,thumbprint, certificate_path)`
4141

examples/sharepoint/ConnectionWithCert.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ where
4141
```
4242
site_url - SharePoint site url
4343
client_id - The OAuth client id of the calling application.
44-
thumbprint - ex encoded thumbprint of the certificate
44+
thumbprint - hex encoded thumbprint of the certificate
4545
certificate_path - path to a PEM encoded certificate private key
4646
```
4747

examples/sharepoint/data_generator.py

+38-22
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
1-
import settings
1+
from settings import settings
22
from faker import Faker
3-
4-
from office365.runtime.auth.authentication_context import AuthenticationContext
3+
from office365.runtime.auth.UserCredential import UserCredential
54
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
67

78

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
1619

1720

1821
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+
2028
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+
}
2443
contact_item = contacts_list.add_item(contact_properties)
2544
context.execute_query()
2645
print("Contact '{0}' has been created".format(contact_item.properties["Title"]))
2746

2847

2948
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)

examples/sharepoint/read_large_list.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
from office365.sharepoint.client_context import ClientContext
33
from settings import settings
44

5-
ctx = ClientContext.connect_with_credentials(settings['url'],
5+
ctx = ClientContext.connect_with_credentials("https://mediadev8.sharepoint.com/sites/team",
66
ClientCredential(settings['client_credentials']['client_id'],
77
settings['client_credentials']['client_secret']))
88

99
large_list = ctx.web.lists.get_by_title("Contacts_Large")
10-
paged_items = large_list.get_items().top(2000)
10+
paged_items = large_list.get_items().top(100)
1111
ctx.load(paged_items)
1212
ctx.execute_query()
1313

14+
#res = paged_items[102]
1415
print("Items count: {0}".format(len(paged_items)))
15-
for idx, item in enumerate(paged_items):
16-
print("{0}: {1}".format(idx, item.properties['Title']))
16+
#for idx, item in enumerate(paged_items):
17+
# print("{0}: {1}".format(idx, item.properties['Title']))

office365/sharepoint/listitem.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,13 @@ def set_property(self, name, value, persist_changes=True):
8989
"getItemById", [value], self._parent_collection.resourcePath.parent)
9090

9191
def ensure_type_name(self, target_list):
92-
pass
92+
if not self._entity_type_name:
93+
if not target_list.is_property_available("ListItemEntityTypeFullName"):
94+
self.context.load(target_list, "ListItemEntityTypeFullName")
95+
self.context.get_pending_request().afterExecute += self._init_item_type
96+
else:
97+
self._entity_type_name = target_list.properties['ListItemEntityTypeFullName']
98+
99+
def _init_item_type(self, result):
100+
self.context.get_pending_request().afterExecute -= self._init_item_type
101+
self._entity_type_name = result.properties['ListItemEntityTypeFullName']

0 commit comments

Comments
 (0)