Thursday, October 29, 2015

Delete All Items SharePoint Powershell Script

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")
[System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

write-host

# Enter your configuration here
$siteUrl = "http://mysharepointsite.example.com/"
$listName = "Name of my list"
$batchSize = 1000

write-host "Opening web at $siteUrl..."

$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.OpenWeb()
write-host "Web is: $($web.Title)"

$list = $web.Lists[$listName];
write-host "List is: $($list.Title)"

while ($list.ItemCount -gt 0)
{
  write-host "Item count: $($list.ItemCount)"

  $batch = "<?xml version=`"1.0`" encoding=`"UTF-8`"?><Batch>"
  $i = 0

  foreach ($item in $list.Items)
  {
    $i++
    write-host "`rProcessing ID: $($item.ID) ($i of $batchSize)" -nonewline

    $batch += "<Method><SetList Scope=`"Request`">$($list.ID)</SetList><SetVar Name=`"ID`">$($item.ID)</SetVar><SetVar Name=`"Cmd`">Delete</SetVar><SetVar Name=`"owsfileref`">$($item.File.ServerRelativeUrl)</SetVar></Method>"

    if ($i -ge $batchSize) { break }
  }

  $batch += "</Batch>"

  write-host

  write-host "Sending batch..."

  # We execute it
  $result = $web.ProcessBatchData($batch)

  write-host "Emptying Recycle Bin..."

  # We remove items from recyclebin
  $web.RecycleBin.DeleteAll()

  write-host

  $list.Update()
}

write-host "Done."

Monday, October 19, 2015

Displaying search results to anonymous users in SharePoint 2013

Here’s a SharePoint 2013 situation I’ve run into: you have items in a list/library that cannot be viewed by anonymous users. You have some search related web part such as a content search or search result web part that is supposed to display these items. The problem I ran into is that I wanted information about these items to be displayed in the search result web part while ensuring that anonymous users cannot actually view the items. For example, you may have a list of publications that have a title, publication date and a body of content (see see image).
A SharePoint 2013 list or library with content
A SharePoint 2013 list or library with content
In such a case, I want everyone to be able to see the title and the publication date, however I do not want anonymous users to be able to see the body. The search result web part (via the use of a display template) will only show the title and publication date. If they want to view the content of the item, they have to click on the search result to open it up at which point they will be able to either view the item if they are logged in, or be prompted to log in if they are currently anonymous. Out of the box, if you have a search web part trying to display data to two different types of users (anonymous and authenticated users), with the scenario described above, authenticated users will see results while anonymous users will see no results (see images below)
Results will be shown for authenticated users
Results will be shown for authenticated users
Anonymous users will be given the standard nothing here matches your search message
Anonymous users will be given the standard nothing here matches your search message
Now I’ve seen a few ways that supposedly fix this,and my last search for this issue led me to Soeren L. Nielsen solution for this particular issue. Not to say that his solution doesn’t work, but in my case it seemed that it didn’t work for SharePoint 2013. I found that the key to get this to work lies in the PowerShell script and altering the permissions for anonymous users. The PowerShell script provided in Nielsen’s solution shown below uses the AnonymousPermMask property and ViewFormPages enum:

$web = get-spweb “http://yoursiteurl/subweb/subsubweb”
$list = $web.Lists[“ListName”]
$list.BreakRoleInheritance($true)
$list.AnonymousPermMask = $list.AnonymousPermMask -bor ([int][Microsoft.SharePoint.SPBasePermissions]::ViewFormPages) #binary or adding the permissions
$list.Update()

In the case of 2013, I found that this works with the following changes
$web = get-spweb “http://yoursiteurl/subweb/subsubweb”
$list = $web.Lists[“ListName”]
$list.BreakRoleInheritance($true)
$list.AnonymousPermMask64= $list.AnonymousPermMask64 -bor ([int][Microsoft.SharePoint.SPBasePermissions]::AnonymousSearchAccessList) #binary or adding the permissions
$list.Update()

The two key changes are in bold here. The first is with AnonymousPermMask which according to MSDN is obsolete and you need to use AnonymousPermMask64. The second and probably key change is with the SPBasePermission enum. Rather than using ViewFormPages, you need to use AnonymousSearchAccessList which is described as “Make content of a list or document library retrievable for anonymous users through SharePoint search. The list permissions in the site do not change.”
Hope this helps anyone who ran into a similar challenge!

P.S. – Don’t forget to re-crawl your site after running the PowerShell script…