0
# SharePoint Sites
1
2
Comprehensive SharePoint functionality including sites, lists, libraries, files, folders, and content management through SharePoint REST API. Provides complete document and content management capabilities for SharePoint Online environments.
3
4
## Capabilities
5
6
### Site and Web Management
7
8
SharePoint site collection and web management with comprehensive configuration, navigation, and content organization capabilities.
9
10
```python { .api }
11
class Site:
12
"""SharePoint site collection with comprehensive management capabilities."""
13
14
# Core Properties
15
id: str
16
url: str
17
web_url: str
18
display_name: str
19
name: str
20
description: str
21
created: str
22
last_modified: str
23
server_relative_url: str
24
25
def get(self) -> 'Site':
26
"""
27
Retrieve site information and metadata.
28
29
Returns:
30
Site: Updated site object
31
"""
32
33
def update(self) -> 'Site':
34
"""
35
Update site properties.
36
37
Returns:
38
Site: Updated site object
39
"""
40
41
def get_changes(self, query: Dict[str, Any]) -> List[Dict[str, Any]]:
42
"""
43
Get site changes based on change query.
44
45
Args:
46
query (Dict): Change query parameters
47
48
Returns:
49
List[Dict]: Site changes matching query
50
"""
51
52
# Navigation Properties
53
@property
54
def root_web(self) -> 'Web':
55
"""Root web of the site collection."""
56
57
@property
58
def lists(self) -> 'ListCollection':
59
"""Lists in the site."""
60
61
@property
62
def webs(self) -> 'WebCollection':
63
"""Subwebs in the site."""
64
65
class Web:
66
"""SharePoint web with comprehensive content and configuration management."""
67
68
# Core Properties
69
id: str
70
title: str
71
description: str
72
url: str
73
server_relative_url: str
74
web_template: str
75
language: int
76
created: str
77
last_item_modified_date: str
78
last_item_user_modified_date: str
79
ui_version: int
80
81
def get(self) -> 'Web':
82
"""
83
Retrieve web information and properties.
84
85
Returns:
86
Web: Updated web object
87
"""
88
89
def update(self) -> 'Web':
90
"""
91
Update web properties and settings.
92
93
Returns:
94
Web: Updated web object
95
"""
96
97
def delete(self) -> None:
98
"""Delete web and all its content."""
99
100
def apply_web_template(self, web_template: str) -> None:
101
"""
102
Apply web template to web.
103
104
Args:
105
web_template (str): Web template name
106
"""
107
108
def get_file_by_server_relative_url(self, server_relative_url: str) -> 'File':
109
"""
110
Get file by server-relative URL.
111
112
Args:
113
server_relative_url (str): Server-relative URL of file
114
115
Returns:
116
File: File object at specified URL
117
"""
118
119
def get_folder_by_server_relative_url(self, server_relative_url: str) -> 'Folder':
120
"""
121
Get folder by server-relative URL.
122
123
Args:
124
server_relative_url (str): Server-relative URL of folder
125
126
Returns:
127
Folder: Folder object at specified URL
128
"""
129
130
# Navigation Properties
131
@property
132
def lists(self) -> 'ListCollection':
133
"""Lists in the web."""
134
135
@property
136
def folders(self) -> 'FolderCollection':
137
"""Folders in the web."""
138
139
@property
140
def files(self) -> 'FileCollection':
141
"""Files in the web."""
142
143
@property
144
def webs(self) -> 'WebCollection':
145
"""Subwebs of this web."""
146
147
@property
148
def site_users(self) -> 'UserCollection':
149
"""Users with access to the web."""
150
151
@property
152
def site_groups(self) -> 'GroupCollection':
153
"""SharePoint groups in the web."""
154
155
@property
156
def role_definitions(self) -> 'RoleDefinitionCollection':
157
"""Permission levels defined in the web."""
158
159
class WebCollection:
160
"""Collection of SharePoint webs with management capabilities."""
161
162
def get(self) -> 'WebCollection':
163
"""Retrieve collection of webs."""
164
165
def add(self, web_creation_info: Dict[str, Any]) -> Web:
166
"""
167
Create new subweb.
168
169
Args:
170
web_creation_info (Dict): Web creation parameters
171
172
Returns:
173
Web: Created web object
174
"""
175
176
def get_by_id(self, web_id: str) -> Web:
177
"""
178
Get web by ID.
179
180
Args:
181
web_id (str): Web unique identifier
182
183
Returns:
184
Web: Web object
185
"""
186
```
187
188
### List and Library Management
189
190
SharePoint list and document library operations with comprehensive item management, content types, and field customization.
191
192
```python { .api }
193
class List:
194
"""SharePoint list with comprehensive item and configuration management."""
195
196
# Core Properties
197
id: str
198
title: str
199
description: str
200
base_template: int
201
base_type: int # 0=GenericList, 1=DocumentLibrary
202
created: str
203
last_item_modified_date: str
204
last_item_user_modified_date: str
205
item_count: int
206
hidden: bool
207
208
def get(self) -> 'List':
209
"""
210
Retrieve list information and properties.
211
212
Returns:
213
List: Updated list object
214
"""
215
216
def update(self) -> 'List':
217
"""
218
Update list properties and settings.
219
220
Returns:
221
List: Updated list object
222
"""
223
224
def delete(self) -> None:
225
"""Delete list and all its content."""
226
227
def get_items(self, caml_query: str = None) -> 'ListItemCollection':
228
"""
229
Get list items with optional CAML query.
230
231
Args:
232
caml_query (str, optional): CAML query to filter items
233
234
Returns:
235
ListItemCollection: Collection of list items
236
"""
237
238
def add_item(self, item_properties: Dict[str, Any]) -> 'ListItem':
239
"""
240
Add new item to list.
241
242
Args:
243
item_properties (Dict): Item field values
244
245
Returns:
246
ListItem: Created list item
247
"""
248
249
def get_item_by_id(self, item_id: int) -> 'ListItem':
250
"""
251
Get list item by ID.
252
253
Args:
254
item_id (int): Item ID
255
256
Returns:
257
ListItem: List item object
258
"""
259
260
def break_role_inheritance(self, copy_role_assignments: bool = True, clear_subscopes: bool = True) -> None:
261
"""
262
Break permission inheritance from parent.
263
264
Args:
265
copy_role_assignments (bool): Copy existing permissions
266
clear_subscopes (bool): Clear permissions on child objects
267
"""
268
269
def reset_role_inheritance(self) -> None:
270
"""Reset permission inheritance to parent."""
271
272
# Navigation Properties
273
@property
274
def items(self) -> 'ListItemCollection':
275
"""Items in the list."""
276
277
@property
278
def fields(self) -> 'FieldCollection':
279
"""Fields (columns) in the list."""
280
281
@property
282
def views(self) -> 'ViewCollection':
283
"""Views defined for the list."""
284
285
@property
286
def content_types(self) -> 'ContentTypeCollection':
287
"""Content types enabled for the list."""
288
289
@property
290
def root_folder(self) -> 'Folder':
291
"""Root folder of the list."""
292
293
@property
294
def role_assignments(self) -> 'RoleAssignmentCollection':
295
"""Permission assignments for the list."""
296
297
class ListCollection:
298
"""Collection of SharePoint lists with management capabilities."""
299
300
def get(self) -> 'ListCollection':
301
"""Retrieve collection of lists."""
302
303
def filter(self, expression: str) -> 'ListCollection':
304
"""
305
Filter lists by OData expression.
306
307
Args:
308
expression (str): OData filter expression
309
310
Returns:
311
ListCollection: Filtered collection
312
"""
313
314
def get_by_title(self, title: str) -> List:
315
"""
316
Get list by title.
317
318
Args:
319
title (str): List title
320
321
Returns:
322
List: List object
323
"""
324
325
def get_by_id(self, list_id: str) -> List:
326
"""
327
Get list by ID.
328
329
Args:
330
list_id (str): List unique identifier
331
332
Returns:
333
List: List object
334
"""
335
336
def add(self, list_creation_info: Dict[str, Any]) -> List:
337
"""
338
Create new list.
339
340
Args:
341
list_creation_info (Dict): List creation parameters
342
343
Returns:
344
List: Created list object
345
"""
346
347
class ListItem:
348
"""SharePoint list item with comprehensive field and permission management."""
349
350
# Core Properties
351
id: int
352
title: str
353
created: str
354
modified: str
355
author_id: int
356
editor_id: int
357
file_system_object_type: int # 0=File, 1=Folder
358
359
def get(self) -> 'ListItem':
360
"""
361
Retrieve list item and field values.
362
363
Returns:
364
ListItem: Updated list item object
365
"""
366
367
def update(self) -> 'ListItem':
368
"""
369
Update list item field values.
370
371
Returns:
372
ListItem: Updated list item object
373
"""
374
375
def delete(self) -> None:
376
"""Delete list item."""
377
378
def recycle(self) -> str:
379
"""
380
Move item to recycle bin.
381
382
Returns:
383
str: Recycle bin item ID
384
"""
385
386
def get_field_value(self, field_name: str) -> Any:
387
"""
388
Get field value by field name.
389
390
Args:
391
field_name (str): Internal field name
392
393
Returns:
394
Any: Field value
395
"""
396
397
def set_field_value(self, field_name: str, field_value: Any) -> None:
398
"""
399
Set field value by field name.
400
401
Args:
402
field_name (str): Internal field name
403
field_value (Any): Field value to set
404
"""
405
406
def validate_update_list_item(self, form_values: List[Dict[str, str]], new_document_update: bool = False) -> List[Dict[str, Any]]:
407
"""
408
Validate and update list item using form values.
409
410
Args:
411
form_values (List[Dict]): Field values to validate and update
412
new_document_update (bool): Whether this is a new document update
413
414
Returns:
415
List[Dict]: Validation results
416
"""
417
418
# Navigation Properties
419
@property
420
def properties(self) -> Dict[str, Any]:
421
"""All field values as dictionary."""
422
423
@property
424
def file(self) -> 'File':
425
"""Associated file (for document libraries)."""
426
427
@property
428
def folder(self) -> 'Folder':
429
"""Associated folder (for folder items)."""
430
431
@property
432
def attachment_files(self) -> 'AttachmentCollection':
433
"""File attachments for the item."""
434
435
@property
436
def role_assignments(self) -> 'RoleAssignmentCollection':
437
"""Permission assignments for the item."""
438
439
class ListItemCollection:
440
"""Collection of SharePoint list items with query capabilities."""
441
442
def get(self) -> 'ListItemCollection':
443
"""Retrieve collection of list items."""
444
445
def filter(self, expression: str) -> 'ListItemCollection':
446
"""
447
Filter items by OData expression.
448
449
Args:
450
expression (str): OData filter expression
451
452
Returns:
453
ListItemCollection: Filtered collection
454
"""
455
456
def select(self, properties: List[str]) -> 'ListItemCollection':
457
"""
458
Select specific fields to retrieve.
459
460
Args:
461
properties (List[str]): Field names to select
462
463
Returns:
464
ListItemCollection: Collection with selected fields
465
"""
466
467
def top(self, count: int) -> 'ListItemCollection':
468
"""
469
Limit results to top N items.
470
471
Args:
472
count (int): Maximum number of items
473
474
Returns:
475
ListItemCollection: Limited collection
476
"""
477
478
def get_by_id(self, item_id: int) -> ListItem:
479
"""
480
Get item by ID.
481
482
Args:
483
item_id (int): Item ID
484
485
Returns:
486
ListItem: List item object
487
"""
488
489
def add(self, item_creation_info: Dict[str, Any]) -> ListItem:
490
"""
491
Add new item to collection.
492
493
Args:
494
item_creation_info (Dict): Item properties
495
496
Returns:
497
ListItem: Created list item
498
"""
499
```
500
501
### File and Folder Operations
502
503
Comprehensive file system operations with version control, check-in/check-out, and metadata management for SharePoint document libraries.
504
505
```python { .api }
506
class File:
507
"""SharePoint file with comprehensive content and version management."""
508
509
# Core Properties
510
name: str
511
server_relative_url: str
512
time_created: str
513
time_last_modified: str
514
title: str
515
ui_version: int
516
ui_version_label: str
517
unique_id: str
518
length: int
519
major_version: int
520
minor_version: int
521
check_out_type: int
522
checkout_user_id: int
523
524
def get(self) -> 'File':
525
"""
526
Retrieve file information and metadata.
527
528
Returns:
529
File: Updated file object
530
"""
531
532
def delete(self) -> None:
533
"""Delete file."""
534
535
def recycle(self) -> str:
536
"""
537
Move file to recycle bin.
538
539
Returns:
540
str: Recycle bin item ID
541
"""
542
543
def checkout(self) -> None:
544
"""Check out file for editing."""
545
546
def checkin(self, comment: str = None, check_in_type: int = 1) -> None:
547
"""
548
Check in file after editing.
549
550
Args:
551
comment (str, optional): Check-in comment
552
check_in_type (int): Check-in type (0=MinorCheckIn, 1=MajorCheckIn, 2=OverwriteCheckIn)
553
"""
554
555
def undo_checkout(self) -> None:
556
"""Undo file checkout."""
557
558
def publish(self, comment: str = None) -> None:
559
"""
560
Publish major version of file.
561
562
Args:
563
comment (str, optional): Publishing comment
564
"""
565
566
def unpublish(self, comment: str = None) -> None:
567
"""
568
Unpublish file.
569
570
Args:
571
comment (str, optional): Unpublishing comment
572
"""
573
574
def approve(self, comment: str = None) -> None:
575
"""
576
Approve file for publication.
577
578
Args:
579
comment (str, optional): Approval comment
580
"""
581
582
def deny(self, comment: str = None) -> None:
583
"""
584
Deny file approval.
585
586
Args:
587
comment (str, optional): Denial comment
588
"""
589
590
def get_content(self) -> bytes:
591
"""
592
Download file content.
593
594
Returns:
595
bytes: File content as bytes
596
"""
597
598
def save_binary(self, content: bytes) -> None:
599
"""
600
Upload file content.
601
602
Args:
603
content (bytes): File content to upload
604
"""
605
606
def copy_to(self, new_url: str, overwrite: bool = False) -> None:
607
"""
608
Copy file to new location.
609
610
Args:
611
new_url (str): Destination URL
612
overwrite (bool): Overwrite existing file
613
"""
614
615
def move_to(self, new_url: str, overwrite: bool = False) -> None:
616
"""
617
Move file to new location.
618
619
Args:
620
new_url (str): Destination URL
621
overwrite (bool): Overwrite existing file
622
"""
623
624
# Navigation Properties
625
@property
626
def list_item_all_fields(self) -> ListItem:
627
"""Associated list item with all fields."""
628
629
@property
630
def versions(self) -> 'FileVersionCollection':
631
"""File version history."""
632
633
@property
634
def properties(self) -> Dict[str, Any]:
635
"""File properties and metadata."""
636
637
class FileCollection:
638
"""Collection of SharePoint files with management capabilities."""
639
640
def get(self) -> 'FileCollection':
641
"""Retrieve collection of files."""
642
643
def add(self, url: str, content: bytes, overwrite: bool = False) -> File:
644
"""
645
Add new file to collection.
646
647
Args:
648
url (str): File URL
649
content (bytes): File content
650
overwrite (bool): Overwrite existing file
651
652
Returns:
653
File: Added file object
654
"""
655
656
def add_template_file(self, url_of_file: str, template_file_type: int) -> File:
657
"""
658
Add file from template.
659
660
Args:
661
url_of_file (str): File URL
662
template_file_type (int): Template type
663
664
Returns:
665
File: Created file from template
666
"""
667
668
def get_by_url(self, url: str) -> File:
669
"""
670
Get file by URL.
671
672
Args:
673
url (str): File URL
674
675
Returns:
676
File: File object
677
"""
678
679
class Folder:
680
"""SharePoint folder with comprehensive content and permission management."""
681
682
# Core Properties
683
name: str
684
server_relative_url: str
685
time_created: str
686
time_last_modified: str
687
unique_id: str
688
item_count: int
689
690
def get(self) -> 'Folder':
691
"""
692
Retrieve folder information.
693
694
Returns:
695
Folder: Updated folder object
696
"""
697
698
def delete(self) -> None:
699
"""Delete folder and all contents."""
700
701
def recycle(self) -> str:
702
"""
703
Move folder to recycle bin.
704
705
Returns:
706
str: Recycle bin item ID
707
"""
708
709
def copy_to(self, new_url: str) -> None:
710
"""
711
Copy folder to new location.
712
713
Args:
714
new_url (str): Destination URL
715
"""
716
717
def move_to(self, new_url: str) -> None:
718
"""
719
Move folder to new location.
720
721
Args:
722
new_url (str): Destination URL
723
"""
724
725
def upload_file(self, name: str, content: bytes) -> File:
726
"""
727
Upload file to folder.
728
729
Args:
730
name (str): File name
731
content (bytes): File content
732
733
Returns:
734
File: Uploaded file object
735
"""
736
737
def add_sub_folder(self, name: str) -> 'Folder':
738
"""
739
Create subfolder.
740
741
Args:
742
name (str): Folder name
743
744
Returns:
745
Folder: Created subfolder
746
"""
747
748
# Navigation Properties
749
@property
750
def files(self) -> FileCollection:
751
"""Files in the folder."""
752
753
@property
754
def folders(self) -> 'FolderCollection':
755
"""Subfolders in the folder."""
756
757
@property
758
def list_item_all_fields(self) -> ListItem:
759
"""Associated list item."""
760
761
@property
762
def properties(self) -> Dict[str, Any]:
763
"""Folder properties and metadata."""
764
765
class FolderCollection:
766
"""Collection of SharePoint folders with management capabilities."""
767
768
def get(self) -> 'FolderCollection':
769
"""Retrieve collection of folders."""
770
771
def add(self, url: str) -> Folder:
772
"""
773
Add new folder to collection.
774
775
Args:
776
url (str): Folder URL
777
778
Returns:
779
Folder: Created folder object
780
"""
781
782
def get_by_url(self, url: str) -> Folder:
783
"""
784
Get folder by URL.
785
786
Args:
787
url (str): Folder URL
788
789
Returns:
790
Folder: Folder object
791
"""
792
```
793
794
## Usage Examples
795
796
### Site and Web Operations
797
798
```python
799
from office365.sharepoint.client_context import ClientContext
800
801
ctx = ClientContext("https://tenant.sharepoint.com/sites/sitename").with_client_credentials(
802
client_id="your-client-id",
803
client_secret="your-client-secret"
804
)
805
806
# Get site information
807
site = ctx.site.get().execute_query()
808
print(f"Site: {site.url}")
809
810
# Get web information
811
web = ctx.web.get().execute_query()
812
print(f"Web title: {web.title}, URL: {web.url}")
813
814
# Create subweb
815
subweb_info = {
816
"Title": "Project Site",
817
"Url": "project",
818
"Description": "Project collaboration site",
819
"WebTemplate": "STS#3", # Team Site template
820
"Language": 1033, # English
821
"UseSamePermissionsAsParentSite": True
822
}
823
new_web = web.webs.add(subweb_info).execute_query()
824
print(f"Created subweb: {new_web.title}")
825
```
826
827
### List and Item Operations
828
829
```python
830
# Get document library
831
docs_lib = web.lists.get_by_title("Documents").get().execute_query()
832
print(f"Library: {docs_lib.title}, Item count: {docs_lib.item_count}")
833
834
# Create custom list
835
list_info = {
836
"Title": "Project Tasks",
837
"Description": "Tasks for the project",
838
"BaseTemplate": 100 # Generic List
839
}
840
new_list = web.lists.add(list_info).execute_query()
841
842
# Add list item
843
item_properties = {
844
"Title": "Setup development environment",
845
"Description": "Install and configure development tools",
846
"Priority": "High"
847
}
848
new_item = new_list.items.add(item_properties).execute_query()
849
print(f"Created item: {new_item.properties['Title']}")
850
851
# Get list items
852
items = new_list.items.select(["Title", "Description", "Priority"]).get().execute_query()
853
for item in items:
854
print(f"Task: {item.properties['Title']} - {item.properties['Priority']}")
855
856
# Update list item
857
new_item.set_field_value("Priority", "Medium")
858
new_item.update().execute_query()
859
```
860
861
### File Operations
862
863
```python
864
# Upload file to document library
865
with open("project_plan.docx", "rb") as file_content:
866
uploaded_file = docs_lib.root_folder.upload_file("project_plan.docx", file_content.read())
867
ctx.execute_query()
868
print(f"Uploaded: {uploaded_file.server_relative_url}")
869
870
# Get file information
871
file_info = docs_lib.root_folder.files.get_by_url("project_plan.docx").get().execute_query()
872
print(f"File: {file_info.name}, Size: {file_info.length} bytes")
873
874
# Download file
875
file_content = file_info.get_content()
876
with open("downloaded_plan.docx", "wb") as local_file:
877
local_file.write(file_content)
878
879
# Check out and check in file
880
file_info.checkout()
881
ctx.execute_query()
882
print("File checked out")
883
884
# Update file content (simplified)
885
with open("updated_plan.docx", "rb") as updated_content:
886
file_info.save_binary(updated_content.read())
887
ctx.execute_query()
888
889
# Check in file
890
file_info.checkin("Updated project timeline", 1) # Major version
891
ctx.execute_query()
892
print("File checked in")
893
```
894
895
### Folder Management
896
897
```python
898
# Create folder structure
899
project_folder = docs_lib.root_folder.add_sub_folder("Project Alpha").execute_query()
900
docs_folder = project_folder.add_sub_folder("Documents").execute_query()
901
images_folder = project_folder.add_sub_folder("Images").execute_query()
902
903
# List folder contents
904
folders = docs_lib.root_folder.folders.get().execute_query()
905
for folder in folders:
906
print(f"Folder: {folder.name}")
907
908
files = docs_lib.root_folder.files.get().execute_query()
909
for file in files:
910
print(f"File: {file.name}")
911
```
912
913
## Types
914
915
```python { .api }
916
from typing import Dict, List, Any, Optional
917
918
class WebCreationInformation:
919
"""Information for creating new web."""
920
921
title: str
922
url: str
923
description: str
924
web_template: str
925
language: int
926
use_same_permissions_as_parent_site: bool
927
928
class ListCreationInformation:
929
"""Information for creating new list."""
930
931
title: str
932
description: str
933
base_template: int
934
document_template_type: int
935
quick_launch_option: int
936
template_feature_id: str
937
938
class ListItemCreationInformation:
939
"""Information for creating new list item."""
940
941
folder_url: str
942
list_item_type: str
943
under_lyingObjectType: int
944
945
class FileCreationInformation:
946
"""Information for creating new file."""
947
948
content: bytes
949
overwrite: bool
950
url: str
951
952
class FolderCreationInformation:
953
"""Information for creating new folder."""
954
955
url: str
956
underlyingObjectType: int
957
958
class ChangeQuery:
959
"""Query parameters for retrieving changes."""
960
961
add: bool
962
alert: bool
963
content_type: bool
964
delete_object: bool
965
field: bool
966
file: bool
967
folder: bool
968
group: bool
969
group_membership: bool
970
item: bool
971
list: bool
972
move: bool
973
navigation: bool
974
rename: bool
975
restore: bool
976
role_assignment: bool
977
role_definition: bool
978
security_policy: bool
979
site: bool
980
system_update: bool
981
update: bool
982
user: bool
983
view: bool
984
web: bool
985
986
class CamlQuery:
987
"""CAML query for retrieving list items."""
988
989
view_xml: str
990
folder_server_relative_url: str
991
list_item_collection_position: Dict[str, str]
992
993
class FieldUserValue:
994
"""User field value."""
995
996
lookup_id: int
997
lookup_value: str
998
email: str
999
1000
class FieldLookupValue:
1001
"""Lookup field value."""
1002
1003
lookup_id: int
1004
lookup_value: str
1005
1006
class FieldUrlValue:
1007
"""URL field value."""
1008
1009
description: str
1010
url: str
1011
```