-1

I am trying to construct the URI for SharePoint web services.

I think this is the base URL http://itteamsites.water.com

I have tried: http://itteamsites.water.com/_api

I tried this based on this article: https://msdn.microsoft.com/en-us/library/office/dn292556.aspx

But this gives me 404 NOT FOUND. If someone could point me to right direction that would be great. Thank you.

0

I used this exact code below yesterday as is without changing even one line. It worked.

I did have to download the SP 2013 Client Components SDK.

I brought up PowerShell V3 and copy/pasted in the code below.

Then I ran this:

# Invoke-RestSPO -Url "https://XXXXXXXX.sharepoint.com/_api/web"

It worked first time.

You can use this to make sure your URLs are correct.

The link it came from is list at the top.


# Consuming the SharePoint 2013 REST API from PowerShell
# http://blog.vgrem.com/2014/04/15/consuming-the-sharepoint-2013-rest-api-from-powershell/

Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" 

<#
.Synopsis
    Sends an HTTP or HTTPS request to a SharePoint Online REST-compliant web service.
.DESCRIPTION
    This function sends an HTTP or HTTPS request to a Representational State
    Transfer (REST)-compliant ("RESTful") SharePoint Online web service.
.EXAMPLE
   Invoke-SPORestMethod -Url "https://contoso.sharepoint.com/_api/web"
.EXAMPLE
   Invoke-SPORestMethod -Url "https://contoso.sharepoint.com/_api/contextinfo" -Method "Post"
#>

Function Invoke-RestSPO(){

Param(
[Parameter(Mandatory=$True)]
[String]$Url,

[Parameter(Mandatory=$False)]
[Microsoft.PowerShell.Commands.WebRequestMethod]$Method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get,

[Parameter(Mandatory=$True)]
[String]$UserName,

[Parameter(Mandatory=$False)]
[String]$Password,

[Parameter(Mandatory=$False)]
[String]$Metadata,

[Parameter(Mandatory=$False)]
[System.Byte[]]$Body,

[Parameter(Mandatory=$False)]
[String]$RequestDigest,

[Parameter(Mandatory=$False)]
[String]$ETag,

[Parameter(Mandatory=$False)]
[String]$XHTTPMethod,

[Parameter(Mandatory=$False)]
[System.String]$Accept = "application/json;odata=verbose",

[Parameter(Mandatory=$False)]
[String]$ContentType = "application/json;odata=verbose",

[Parameter(Mandatory=$False)]
[Boolean]$BinaryStringResponseBody = $False

)




   if([string]::IsNullOrEmpty($Password)) {
      $SecurePassword = Read-Host -Prompt "Enter the password" -AsSecureString 
   }
   else {
      $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
   }


   $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
   $request = [System.Net.WebRequest]::Create($Url)
   $request.Credentials = $credentials
   $request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
   $request.ContentType = $ContentType
   $request.Accept = $Accept
   $request.Method=$Method

   if($RequestDigest) { 
      $request.Headers.Add("X-RequestDigest", $RequestDigest)
   }
   if($ETag) { 
      $request.Headers.Add("If-Match", $ETag)
   }
   if($XHTTPMethod) { 
      $request.Headers.Add("X-HTTP-Method", $XHTTPMethod)
   }
   if($Metadata -or $Body) {
      if($Metadata) {     
         $Body = [byte[]][char[]]$Metadata
      }      
      $request.ContentLength = $Body.Length 
      $stream = $request.GetRequestStream()
      $stream.Write($Body, 0, $Body.Length)
   }
   else {
      $request.ContentLength = 0
   }

   #Process Response
   $response = $request.GetResponse()
   try {
       if($BinaryStringResponseBody -eq $False) {    
           $streamReader = New-Object System.IO.StreamReader $response.GetResponseStream()
           try {
              $data=$streamReader.ReadToEnd()
              $results = $data | ConvertFrom-Json
              $results.d 
           }
           finally {
              $streamReader.Dispose()
           }
        }
        else {
           $dataStream = New-Object System.IO.MemoryStream
           try {
           Stream-CopyTo -Source $response.GetResponseStream() -Destination $dataStream
           $dataStream.ToArray()
           }
           finally {
              $dataStream.Dispose()
           } 
        }
    }
    finally {
        $response.Dispose()
    }

}


# Get Context Info 
Function Get-SPOContextInfo(){

Param(
[Parameter(Mandatory=$True)]
[String]$WebUrl,

[Parameter(Mandatory=$True)]
[String]$UserName,

[Parameter(Mandatory=$False)]
[String]$Password
)


   $Url = $WebUrl + "/_api/contextinfo"
   Invoke-RestSPO $Url Post $UserName $Password
}



Function Stream-CopyTo([System.IO.Stream]$Source, [System.IO.Stream]$Destination)
{
    $buffer = New-Object Byte[] 8192 
    $bytesRead = 0
    while (($bytesRead = $Source.Read($buffer, 0, $buffer.Length)) -gt 0)
    {
         $Destination.Write($buffer, 0, $bytesRead)
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.