Skip to content

Commit

Permalink
#736 Retrieve a lookup field value example, type hints for List colle…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
vgrem committed Oct 7, 2023
1 parent b704a20 commit 582ab2b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
34 changes: 34 additions & 0 deletions examples/sharepoint/listitems/get_lookup_field_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Demonstrates how to retrieve a lookup field values from SharePoint List
"""
from office365.sharepoint.client_context import ClientContext
from tests import test_client_credentials, test_team_site_url

ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)

list_tasks = ctx.web.lists.get_by_title("Company Tasks")
items = (
list_tasks.items.get()
.select(
[
"*",
"AssignedTo/Id",
"AssignedTo/Title",
"Predecessors/Id",
"Predecessors/Title",
]
)
.expand(["AssignedTo", "Predecessors"])
.top(10)
.execute_query()
)
for item in items:
assigned_to = item.properties.get("AssignedTo", {}).get("Id", None)
predecessors_ids = [
v.get("Id", None) for k, v in item.properties.get("Predecessors", {}).items()
]
print(
"AssignedTo Id: {0}, Predecessors Ids: {1}".format(
assigned_to, predecessors_ids
)
)
3 changes: 1 addition & 2 deletions examples/sharepoint/listitems/set_lookup_field_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.fields.lookup_value import FieldLookupValue
from office365.sharepoint.fields.user_value import FieldUserValue
from tests import test_client_credentials, test_team_site_url

ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
Expand All @@ -16,7 +15,7 @@
lookup_field_value = FieldLookupValue(task_id)
items[1].set_property("ParentTask", lookup_field_value).update().execute_query()

me = ctx.web.current_user
# me = ctx.web.current_user
# items[0].set_property("AssignedTo", FieldUserValue(me.id)).update().execute_query()
# items[0].set_property("AssignedTo", FieldUserValue.from_user(me)).update().execute_query()
print("Item has been updated")
4 changes: 2 additions & 2 deletions office365/runtime/client_value_collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uuid
from typing import Generic, Iterator, List, Optional, TypeVar
from typing import Dict, Generic, Iterator, List, Optional, TypeVar

from typing_extensions import Self

Expand All @@ -12,7 +12,7 @@

class ClientValueCollection(ClientValue, Generic[T]):
def __init__(self, item_type, initial_values=None):
# type: (T, Optional[List]) -> None
# type: (T, Optional[List | Dict]) -> None
super(ClientValueCollection, self).__init__()
if initial_values is None:
initial_values = []
Expand Down
10 changes: 9 additions & 1 deletion office365/sharepoint/base_entity_collection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
from typing import TypeVar

from office365.runtime.client_object_collection import ClientObjectCollection

T = TypeVar("T")


class BaseEntityCollection(ClientObjectCollection[T]):
"""
SharePoint entity set
"""

class BaseEntityCollection(ClientObjectCollection):
@property
def context(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion office365/sharepoint/listitems/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from office365.sharepoint.listitems.listitem import ListItem


class ListItemCollection(BaseEntityCollection):
class ListItemCollection(BaseEntityCollection[ListItem]):
"""List Item collection"""

def __init__(self, context, resource_path=None):
Expand Down
4 changes: 2 additions & 2 deletions office365/sharepoint/lists/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def create_document_and_get_edit_link(
:param str document_template_type: A number representing the type of document to create.
:param str template_url: Specifies the URL of the document template (2) to base the new document on.
"""
return_type = ClientResult(self.context, str())
return_type = ClientResult(self.context, str()) # type: ClientResult[str]
payload = {
"fileName": file_name,
"folderPath": folder_path,
Expand Down Expand Up @@ -631,7 +631,7 @@ def reserve_list_item_id(self):
"""
Reserves the returned list item identifier for the idempotent creation of a list item.
"""
return_type = ClientResult(self.context, int())
return_type = ClientResult(self.context, int()) # type: ClientResult[int]
qry = ServiceOperationQuery(
self, "ReserveListItemId", None, None, None, return_type
)
Expand Down

0 comments on commit 582ab2b

Please sign in to comment.