Spaces:
Runtime error
Runtime error
Create Autenticacion_Sharepoint.py
Browse files- Autenticacion_Sharepoint.py +69 -0
Autenticacion_Sharepoint.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from office365.runtime.auth.user_credential import UserCredential
|
2 |
+
from office365.sharepoint.listitems.listitem import ListItem
|
3 |
+
from office365.runtime.auth.authentication_context import AuthenticationContext
|
4 |
+
from office365.runtime.http.request_options import RequestOptions
|
5 |
+
from office365.runtime.client_request import ClientRequest
|
6 |
+
from office365.sharepoint.client_context import ClientContext
|
7 |
+
from office365.sharepoint.fields.creation_information import FieldCreationInformation
|
8 |
+
from office365.sharepoint.fields.field import FieldType
|
9 |
+
import os
|
10 |
+
|
11 |
+
|
12 |
+
def auth_sharepoint():
|
13 |
+
####################################################################################################
|
14 |
+
# AUTENTICACIÓN
|
15 |
+
############################################################################
|
16 |
+
SITE_URL = "https://lagunaai.sharepoint.com/sites/ConversacionesChatbots"
|
17 |
+
USERNAME=os.environ['USERNAME']
|
18 |
+
PASSWORD=os.environ['PASSWORD']
|
19 |
+
user_credentials = UserCredential(USERNAME,PASSWORD)
|
20 |
+
context = ClientContext(SITE_URL).with_credentials(user_credentials)
|
21 |
+
context.load(context.web)
|
22 |
+
context.execute_query()
|
23 |
+
return context
|
24 |
+
#print("{0}".format(context.web.url))
|
25 |
+
|
26 |
+
context=auth_sharepoint()
|
27 |
+
##Creación y verificación de campos o columnas de la lista de sharepoint
|
28 |
+
#evitamos la creacion de muchos columnas con el "mismo nombre"
|
29 |
+
|
30 |
+
#################################################
|
31 |
+
# LISTA QUE VAMOS A MODIFICAR
|
32 |
+
#################################################
|
33 |
+
list = context.web.lists.get_by_title("Development_Vaping") #CONECTAMOS A LA LISTA
|
34 |
+
|
35 |
+
# Define la información para la creación del campo
|
36 |
+
content = FieldCreationInformation("Content", FieldType.Note)#se define un campo que recibe multiples lineas de texto
|
37 |
+
number_cel_inf= FieldCreationInformation("User_ID", FieldType.Text)#se define un campo que recibe una unica linea de texto
|
38 |
+
nombre_inf= FieldCreationInformation("User_Name", FieldType.Text)
|
39 |
+
|
40 |
+
# Verifica si los campos ya existen
|
41 |
+
fields = list.fields
|
42 |
+
context.load(fields)
|
43 |
+
context.execute_query()
|
44 |
+
|
45 |
+
field_names = [field.internal_name for field in fields]
|
46 |
+
print(field_names)
|
47 |
+
def contains_any(field_name, strings):
|
48 |
+
# Retorna True si el nombre del campo contiene alguna de las cadenas en la lista strings
|
49 |
+
# False si el nombre del campo no está la cadena en la lista de strings
|
50 |
+
for s in strings:
|
51 |
+
if s in field_name:
|
52 |
+
return True
|
53 |
+
return False
|
54 |
+
|
55 |
+
fields_to_create = ['User_ID', 'User_Name', 'Content']
|
56 |
+
# Recorre los nombres de los campos a crear
|
57 |
+
for field_name in fields_to_create:
|
58 |
+
# si el valor de la función es falso, osea el nombre del campo que queremos no está creado en sharepoint...
|
59 |
+
if not contains_any(field_name, field_names):
|
60 |
+
# Crea el campo correspondiente según la lista de campos que deseamos crear
|
61 |
+
if 'User_ID' in fields_to_create:
|
62 |
+
number_cel = list.fields.add(number_cel_inf)
|
63 |
+
if 'User_Name' in fields_to_create:
|
64 |
+
nombre_inf = list.fields.add(nombre_inf)
|
65 |
+
if 'Content' in fields_to_create:
|
66 |
+
contenido = list.fields.add(content)
|
67 |
+
#print(field_names)
|
68 |
+
context.execute_query()
|
69 |
+
|