¡SharePoint Online: Como autenticarse con el modelo de objetos en cliente (revisado)!
Como siempre digo, en los cursos de formación que imparto yo soy un alumno más que aprende a partir de las preguntas que realizan los alumnos que asisten a mis cursos. En esta ocasión, lo que hemos aprendido es lo fácil que resulta autenticarse contra SharePoint Online en Office 365 gracias a un nuevo método que incorpora el modelo de objetos en cliente: SharePointOnlineCredentials().
-
Básicamente, en el caso de la implementación .NET del modelo de objetos en cliente necesitamos añadir directivas using a Microsoft.SharePoint.Client y System.Security.
1 |
1: //Espacios de nombres necesarios<!--CRLF--> |
1 |
2: using MO_NET = Microsoft.SharePoint.Client;<!--CRLF--> |
1 |
3: using System.Net;<!--CRLF--> |
1 |
4: using System.Security;<!--CRLF--> |
- Luego a la hora de conectaros a SharePoint Online a través de ClientContext podemos autenticarnos haciendo uso del método anterior al que únicamente le tenemos que pasar nuestro usuario y contraseña de Office 365 y listo.
1 |
1: try<!--CRLF--> |
1 |
2: {<!--CRLF--> |
1 |
3: DataTable dtColeccionListas =<!--CRLF--> |
1 |
4: new DataTable();<!--CRLF--> |
1 |
5: dtColeccionListas.Columns.Add("Title");<!--CRLF--> |
1 |
6: dtColeccionListas.Columns.Add("Id");<!--CRLF--> |
1 |
7: using (MO_NET.ClientContext ctx =<!--CRLF--> |
1 |
8: new MO_NET.ClientContext(txtSitio.Text))<!--CRLF--> |
1 2 3 |
9: { 10: 11: //SharePoint Online<!--CRLF--> |
1 |
12: var login = "<user>@<dominioOffice365>.onmicrosoft.com";<!--CRLF--> |
1 |
13: var password = "<Password>";<!--CRLF--> |
1 |
14: var securePassword = new SecureString();<!--CRLF--> |
1 |
15: foreach (char c in password)<!--CRLF--> |
1 |
16: {<!--CRLF--> |
1 |
17: securePassword.AppendChar(c);<!--CRLF--> |
1 |
18: }<!--CRLF--> |
1 |
19: <!--CRLF--> |
1 |
20: var onlineCredentials = new MO_NET.SharePointOnlineCredentials(login, securePassword);<!--CRLF--> |
1 |
21: ctx.Credentials = onlineCredentials;<!--CRLF--> |
1 |
22: MO_NET.Web wSitio = ctx.Web;<!--CRLF--> |
1 |
23: ctx.Load(wSitio, oSitio => oSitio.Title);<!--CRLF--> |
1 |
24: <!--CRLF--> |
1 |
25: MO_NET.ListCollection lcLists = wSitio.Lists;<!--CRLF--> |
1 |
26: IEnumerable<MO_NET.List> lcListsCollection =<!--CRLF--> |
1 |
27: ctx.LoadQuery(lcLists);<!--CRLF--> |
1 |
28: ctx.ExecuteQuery();<!--CRLF--> |
1 |
29: <!--CRLF--> |
1 |
30: lblMensaje.Text =<!--CRLF--> |
1 |
31: "El número de listas contenidas en el Sitio " +<!--CRLF--> |
1 |
32: wSitio.Title + " es " +<!--CRLF--> |
1 |
33: lcListsCollection.Count().ToString();<!--CRLF--> |
1 |
34: <!--CRLF--> |
1 |
35: foreach (MO_NET.List l in lcListsCollection)<!--CRLF--> |
1 |
36: {<!--CRLF--> |
1 |
37: DataRow dtRow = dtColeccionListas.NewRow();<!--CRLF--> |
1 |
38: dtRow["Title"] = l.Title;<!--CRLF--> |
1 |
39: dtRow["Id"] = l.Id;<!--CRLF--> |
1 |
40: dtColeccionListas.Rows.Add(dtRow);<!--CRLF--> |
1 |
41: }<!--CRLF--> |
1 |
42: grdListas.DataSource = dtColeccionListas;<!--CRLF--> |
1 |
43: }<!--CRLF--> |
1 |
44: }<!--CRLF--> |
1 |
45: catch (MO_NET.InvalidQueryExpressionException ex)<!--CRLF--> |
1 |
46: {<!--CRLF--> |
1 |
47: MessageBox.Show(ex.Message, "¡Error!",<!--CRLF--> |
1 |
48: MessageBoxButtons.OK,<!--CRLF--> |
1 |
49: MessageBoxIcon.Error);<!--CRLF--> |
1 |
50: }<!--CRLF--> |
1 |
51: catch (MO_NET.ClientRequestException ex)<!--CRLF--> |
1 |
52: {<!--CRLF--> |
1 |
53: MessageBox.Show(ex.Message, "¡Error!",<!--CRLF--> |
1 |
54: MessageBoxButtons.OK,<!--CRLF--> |
1 |
55: MessageBoxIcon.Error);<!--CRLF--> |
1 |
56: }<!--CRLF--> |
1 |
57: catch (MO_NET.PropertyOrFieldNotInitializedException ex)<!--CRLF--> |
1 |
58: {<!--CRLF--> |
1 |
59: MessageBox.Show(ex.Message, "¡Error!",<!--CRLF--> |
1 |
60: MessageBoxButtons.OK,<!--CRLF--> |
1 |
61: MessageBoxIcon.Error);<!--CRLF--> |
1 |
62: }<!--CRLF--> |
1 |
63: catch (MO_NET.ServerUnauthorizedAccessException ex)<!--CRLF--> |
1 |
64: {<!--CRLF--> |
1 |
65: MessageBox.Show(ex.Message, "¡Error!",<!--CRLF--> |
1 |
66: MessageBoxButtons.OK,<!--CRLF--> |
1 |
67: MessageBoxIcon.Error);<!--CRLF--> |
1 |
68: }<!--CRLF--> |
1 |
69: catch (MO_NET.ServerException ex)<!--CRLF--> |
1 |
70: {<!--CRLF--> |
1 |
71: MessageBox.Show(ex.Message, "¡Error!",<!--CRLF--> |
1 |
72: MessageBoxButtons.OK,<!--CRLF--> |
1 |
73: MessageBoxIcon.Error);<!--CRLF--> |
1 |
74: }<!--CRLF--> |
1 |
75: catch (Exception ex)<!--CRLF--> |
1 |
76: {<!--CRLF--> |
1 |
77: MessageBox.Show(ex.Message, "¡Error!",<!--CRLF--> |
1 |
78: MessageBoxButtons.OK,<!--CRLF--> |
1 |
79: MessageBoxIcon.Error);<!--CRLF--> |
1 |
80: }<!--CRLF--> |
1 |
81: <!--CRLF--> |
1 |
82: }<!--CRLF--> |
- El resultado es que accedemos a la información del sitio de SharePoint Online sin problemas y sin más que utilizar dos líneas de código para autenticarnos contra el mismo. Sin duda, un gran avance a la hora de interactuar con sitios de SharePoint Online desde aplicaciones remotas.
No comments yet.