Configuring SharePoint 2013 Search REST API for anonymous users (AUTOMATED)

By | 2013-06-16

Waldek has posted on this topic already, and has an extensive post available.
This post only contains a simple automated solution. The issue/goal here is:

“One of the great capabilities of SharePoint 2013 is the Search REST API. Unfortunately it’s disabled for anonymous users by default. Find out how to enable SharePoint 2013 Search REST API for anonymous users.”

I have created a [queryparametertemplate.xml] file. In it, I have put in tokens for the GUIDs we have to insert..

<QueryPropertiesTemplate xmlns="http://www.microsoft.com/sharepoint/search/KnownTypes/2008/08" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <QueryProperties i:type="KeywordQueryProperties">
	<EnableStemming>true</EnableStemming>
	<FarmId>--farmid--</FarmId>
	<SiteId>--siteid--</SiteId>
	<WebId>--webid--</WebId>
....
</QueryPropertiesTemplate>

Next we use PowerShell to get the needed GUIDs and insert them into the [queryparametertemplate.xml] file:

Write-Host -f Yellow " Reading template XML queryparametertemplate.xml"  -NoNewline

$queryparametertemplate = get-content .queryparametertemplate.xml

Write-Host -f Green " .. OK" 

Write-Host -f Yellow " Setting values in XML queryparametertemplate.xml"  -NoNewline

$farmid= (Get-SPFarm).Id
$site = Get-SPSite($targetsitecollectionurl)
$siteid = $site.Id
$webid = $site.RootWeb.Id

$queryparametertemplate = $queryparametertemplate.Replace("--farmid--",$farmid.ToString())
$queryparametertemplate = $queryparametertemplate.Replace("--siteid--",$siteid.ToString())
$queryparametertemplate = $queryparametertemplate.Replace("--webid--",$webid.ToString())

Write-Host -f Green " .. OK" 

Write-Host -f Yellow " Saving file queryparameters.xml"  -NoNewline

Set-Content -Path ".queryparameters.xml" -Value $queryparametertemplate

Write-Host -f Green " .. OK"

Now we can upload the file to the Proper Document Library:

# Parameters
$listname = "QueryPropertiesTemplate"
$listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
$description = "Systeem lijst t.b.v. de anoniem zoeken."

# Start
Write-Host -f Green " "

$list = $site.RootWeb.Lists[$listname]

if ( $list -eq $null ) {
    Write-Host -f Yellow " Creating new DocLib: $listname"  -NoNewline
    # Create the List
    $list = $site.RootWeb.Lists.Add($listname ,$description,$listTemplate)
    Write-Host -f Green " .. OK" 
}

Write-Host -f Yellow " Setting Properties for DocLib: $listname" -NoNewline
$list = $site.RootWeb.Lists[$listname]

$list.NoCrawl = $true
$list.OnQuickLaunch = $false
$list.Followable = $false
$list.EnableVersioning = $false

$list.EnableFolderCreation = $false
$list.EnableMinorVersions= $false
$list.EnableModeration = $false

$list.Update()
Write-Host -f Green " .. OK" 

Write-Host -f Yellow " Uploading queryparameters.xml to DocLib"  -NoNewline

# Get instance of RootFolder
$folder = $site.RootWeb.GetFolder($listname)

# Get the file on Disk that we want to upload
$file = Get-Item .queryparameters.xml -Erroraction Stop

# Upload the file.
$spfile = $folder.Files.Add($listname +"/queryparameters.xml",$file.OpenRead(), $false)

if( $spfile -ne $null ){
  $spfile.Item["Title"] = "Anonieme Search Queryparameters"
  $spfile.Item.SystemUpdate()

}
Write-Host -f Green " .. OK"

I have made the Template XML File and PowerShell scripts available for download here: make-search-anon-api

4 thoughts on “Configuring SharePoint 2013 Search REST API for anonymous users (AUTOMATED)

  1. Wicus van den Berg

    Hi there. Good blog. Just a point to note here for users on Office 365. I searched the web high and low to find an answer. How do I get the FarmId for Office 365??? The answer is simple.. You don’t.. Just leave the FarmId section blank in your template. It works, believe me, I know.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.