SharePoint 2013: Como hacer uso de la API REST en PowerShell (I)!

Igual que podemos hacer uso de las APIS de Servidor y de Cliente en PowerShell, también podemos hacer uso de la API REST de SharePoint 2013. Para poder hacer uso de la API REST de SharePoint en PowerShell, simplemente tenemos que usar los objetos .NET adecuados en nuestro Script. De todos los objetos necesarios, como se puede ver en el siguiente script, el más importante es WebRequest ya que en él especificaremos la Url REST que queremos utilizar, las credenciales de acceso, las cabeceras y el método web a usar (GET):

   1: ############################################################################################################################################

   2: # Script that allows to get all the lists in a SharePoint Online site using REST

   3: # Required Parameters:

   4: #  -> $sUserName: User Name to connect to the SharePoint Online Site Collection.

   5: #  -> $sPassword: Password for the user.

   6: #  -> $sDomain: AD Domain for the user.

   7: #  -> $sRESTUrl: API REST Url.

   8: #  -> $WebRMehod: WebRequestMethod to use

   9: ############################################################################################################################################

  10:  

  11: $host.Runspace.ThreadOptions = "ReuseThread"

  12:  

  13: #Definition of the function that gets all the lists in a SharePoint Site using REST

  14: function Get-SPListsUsingRESTAPI

  15: {

  16:     param ($sRESTUrl,$sUserName,$sPassword, $sDomain, $WebRMethod)

  17:     try

  18:     {    

  19:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  20:         Write-Host "Getting all the list in a SharePoint Site using REST" -foregroundcolor Green

  21:         Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green

  22:             

  23:         $spCredentials = New-Object System.Net.NetworkCredential($sUserName,$sPassword,$sDomain)  

  24:         $spWebRequest = [System.Net.WebRequest]::Create($sRESTUrl)

  25:         $spWebRequest.Credentials = $spCredentials

  26:         $spWebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")

  27:         $spWebRequest.Accept = "application/json;odata=verbose"

  28:         $spWebRequest.Method=$WebRMethod

  29:         $spWebResponse = $spWebRequest.GetResponse()

  30:         $spRequestStream = $spWebResponse.GetResponseStream()

  31:         $spReadStream = New-Object System.IO.StreamReader $spRequestStream

  32:         $spData=$spReadStream.ReadToEnd()

  33:         $spResults = $spData | ConvertFrom-Json

  34:         $spLists=$spResults.d.results

  35:         foreach($spList in $spLists)

  36:         {

  37:             Write-Host $spList.Title -ForegroundColor Green

  38:         }                 

  39:     }

  40:     catch [System.Exception]

  41:     {

  42:         write-host -f red $_.Exception.ToString()   

  43:     }    

  44: }

  45:  

  46: #Required Parameters

  47: $sRESTUrl = "http://<SharePointSite_Url>/_api/web/lists" 

  48: $sUserName = "<SharePoint_User>" 

  49: $sPassword ="<User_Password>" 

  50: $sDomain="<AD_Domain>"

  51: $WebRMethod=[Microsoft.PowerShell.Commands.WebRequestMethod]::Get

  52:  

  53: Get-SPListsUsingRESTAPI -sRESTUrl $sRESTUrl  -sUserName $sUserName -sPassword $sPassword -sDomain $sDomain -WebRMethod $WebRMethod

La ejecución del script, que podéis descargaros desde el siguiente enlace, generará la siguiente salida: How to Get all the Lists in a SharePoint Site using REST

image

Deja una respuesta

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s