I'm trying to create a SharePoint site using the REST API with Powershell. I've successfully created lists, modified lists, and many other things but creating a site has me stumped. Below is the code that builds the request that I invoke. Authentication works so I have removed those sections of code. When I run the script I get a 400 Bad Request so I'm thinking that I'm not including something in the headers or in the body. Any help would be greatly appreciated!

$webURL = "https:// <SharePoint site URL> "
$urlAuth = $webUrl + "/_api/web/webinfos/add"

<Section Removed - Get FedAuth Token>

<Section Removed - Get FormDigest Value>

params = @{} 
params.Headers = @{"Accept" = "application/json; odata=verbose"; "X-RequestDigest" = $formDigest; "Content-Type" = "application/json;odata=verbose"} params.Uri = $urlAuth 
params.Body = [pscustomobject]@{
       _metadata = @{type = "SP.WebCreationInformation"}
       Url = "testsubsite"
       Title = "testsubsite"
       Description = "siteDescription"
       Language = "1033"
       WebTemplate = "STS"
       UseUniquePermissions = "False"
       } | ConvertTo-Json 
$params.Method = "POST" 
$params.WebSession = $session

$response = Invoke-RestMethod @params
  • Which version of SharePoint? – OneOfThePetes Aug 14 '19 at 12:56
  • @Nullldata - Sharepoint 2013 – nboothe Aug 14 '19 at 12:58
  • I've added tags to your question to indicate SharePoint 2013. It helps! :) – OneOfThePetes Aug 14 '19 at 13:04
  • Just out of interest, does it have to be using REST? There are other ways to create SharePoint sites in PowerShell: New-SPSite and New-SPWeb are 2 cmdlets that can help with this... – OneOfThePetes Aug 14 '19 at 13:16
  • @Nullldata - I have use certificate based authentication to do anything on SharePoint. I was under the assumption that I couldn't use the cmdlets with certificate based authentication. Is this a correct assumption? Cmdlets would make this so much easier. – nboothe Aug 14 '19 at 13:34

Your Answer

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

Browse other questions tagged or ask your own question.