Restore deleted site collections SharePoint 2013

In SharePoint 2013 it is possible to restore a accidently deleted site collection. For more information, read this article: http://technet.microsoft.com/en-us/library/hh272537.aspx

You can use the Restore-SPDeletedSite cmdlet to restore a site collection.

However, if you removed the site collection using the Remove-SPSite cmdlet using PowerShell, the site collection will not be stored in a SPDeletedSite object.

This means you cannot restore a site collection that has been removed using PowerShell.

 

Advertisements

Add PDF mimetype for all Web Applications one-liner

By default, PDF files cannot be opened directly from SharePoint 2010/SharePoint 2013.

To add the PDF mimetype to all Web Applications (Instead of doing it seperately for each Web Application), you can use the following one-liner:

Get-SPWebApplication | %{$_.AllowedInlineDownloadedMimeTypes.Add("application/pdf");$_.Update()}

Get current superuser/superreader accounts PowerShell One-liner

This one-liner wil show you the currently configured superuser and superreader accounts for each of your Web Applications.Just paste this line of code in your SharePoint 2010 Management Shell:

Get-SPWebApplication | %{Write-Host “Web Application: ” $_.url “`nSuper user: ” $_.properties[“portalsuperuseraccount”] “`nSuper reader: ” $_.properties[“portalsuperreaderaccount”] “`n”}

This is the output for the above command in my test environment:

image

In my case, I have not properly configured the super user accounts for all my Web Applications.

Be aware that misconfigured super user accounts can lead to access problems. You may not be able to access your SharePoint Web Application!

Get Site Collections per Content Database one-liner

This will be a very short post, as it contains only 1 line of PowerShell code, and is not very hard to understand. It shows you all content databases, and for each of the content databases, it will show you the site collections within this content database.

Get-SPContentDatabase | %{Write-Output "- $($_.Name)”; foreach($site in $_.sites){write-Output $site.url}}

That wasn’t so bad right? Just copy and paste this code into your SharePoint 2010 Management Shell (As administrator), and let PowerShell do your work for you!

image

Now let’s say you want to save this output to a .txt file, you can add “> C:\sitecollections.txt”.

The full command would be:

Get-SPContentDatabase | %{Write-Output "- $($_.Name)”; foreach($site in $_.sites){write-Output $site.url}} > C:\sitecollections.txt

This would create a .txt file with the same information:

image

That’s all there is to it!

Get all SharePoint lists by using PowerShell

My last article explained how you can retrieve all your workflows from your SharePoint 2010 farm. After this, I got some requests from people that wanted an overview of all their used lists in SharePoint 2010.

For this, I adjusted my last script a little to output all your SharePoint lists and libraries. Or you can specify the -URL parameter to get just the lists from the particular site.

param
(
[string] $URL,
[boolean] $WriteToFile = $true
)

#Get all lists in farm
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Counter variables
$webcount = 0
$listcount = 0

if($WriteToFile -eq $true)
{
$outputPath = Read-Host "Outputpath (e.g. C:\directory\filename.txt)"
}
if(!$URL)
{
#Grab all webs
$webs = (Get-SPSite -limit all | Get-SPWeb -Limit all -ErrorAction SilentlyContinue)
}
else
{
$webs = Get-SPWeb $URL
}
if($webs.count -ge 1 -OR $webs.count -eq $null)
{
    foreach($web in $webs)
    {
   #Grab all lists in the current web
    $lists = $web.Lists   
    Write-Host "Website"$web.url -ForegroundColor Green 
    if($WriteToFile -eq $true){Add-Content -Path $outputPath -Value "Website $($web.url)"}
        foreach($list in $lists)
        {
            $listcount +=1  
            Write-Host " – "$list.Title          
            if($WriteToFile -eq $true){Add-Content -Path $outputPath -Value " – $($list.Title)"}
        }
    $webcount +=1
    $web.Dispose()
    }
#Show total counter for checked webs & lists
Write-Host "Amount of webs checked:"$webcount
Write-Host "Amount of lists:"$listcount
}
else
{
Write-Host "No webs retrieved, please check your permissions" -ForegroundColor Red -BackgroundColor Black
}

You can run the script by running the .ps1 file. There are 2 parameters that you can add.

WriteToFile: Default is true. If the parameter is set to true, the console will ask for a output location where the text file will be saved.
URL: Default is empty. If the URL Parameter is set, only the web you specified and all webs under this web will be reported.

Here is a possible outcome:

image

Creating an error in SharePoint 2010

Every once in a while, I give a SharePoint 2010 training, where the main focus lies in the infrastructural background.
One of these aspects is the monitoring of SharePoint 2010 and how to manage it and troubleshoot the best way possible. To do this, I use the ULSViewer.

You would think that it is pretty easy to create an error, seeing as how often you get an “unexpected error” in SharePoint. To make things easier, I found this article by Todd Carter. He shows a way to create custom error pages.

This gave me the idea to use the bug he pointed out, to demonstrate the ULS viewer.
From here it is pretty straightforward.
I opened the ULS viewer to start scanning my ULS by pressing CTRL + U. The ULS Viewer will automatically find the location of the logs and will open it real-time.

Using the bug, I navigated to “http://yourdomain.com/_layouts/Error.aspx?ErrorText=Error for demo purposes.”. I have registered this domain in my local DNS file for demonstrating purposes.
This shows the following screen:

image

Using the ULS viewer, i filtered the log for correlation ID “fb134e8c-553b-47ff-9a5d-e81d329e10dd “.

Now, the log has been filtered only to see the error I had created!

image

Of course, in this case, the only message that is visible is the GET request for the error page, but it did the trick!

Export/Import SharePoint 2010 solutions using PowerShell (Part 2/3)

Following up on the exporting of all solutions deployed in SharePoint 2010 in part 1, now it’s time to import these solutions to a different SharePoint 2010 farm.

Part 2 – Import all exported solutions to any SharePoint 2010 environment

In this part, we will cover how to add the solutions to another SharePoint 2010 farm. This is equal to using the “Add-SPSolution” command in PowerShell, only it does this for all solutions in a given directory.

Here is the script we will use:

#This script will add all solutions in the selected folder
Add-PSSnapin Microsoft.SharePoint.PowerShell

#Functions
function getPath
{
    #Get the directory that contains the solutions
    $inputPath = Read-Host "Enter the full path that contains the solutions (eg. C:\solutions)"
    return $inputPath
}

function addSolutions($importPath)
{
$counterItemsAdded = 0
Write-Host "Adding the solutions to the SharePoint environment" -fore green

#Check if specified path exists, if it does, add all solutions in the directory
if((Test-Path $importPath) -eq $True)
    {
    $items = Get-ChildItem $importPath | ?{$_.extension -eq ".cab" -or $_.extension -eq ".wsp"} #Gets all files that have the .cab or .wsp extension
   #The following line checks all currently installed solutions and adds these to $solutions
    $solutions = (Get-SPFarm).Solutions | Foreach-Object{$solnames += $_.name;} #Get all current SPSolutions
   
        foreach($item in $items)
        {
        #If the solution does not exist within the current environment
        if($solnames -notmatch $item.toString())
            {
                Add-SPSolution -LiteralPath $item.Fullname | out-null
                Write-Host "Imported $item" -fore green
                $counterItemsAdded += 1
            }
        else
            {       
                Write-Host "$item already exists on the SharePoint environment." -fore red
            }
        }
        #If at least 1 solution has been added
        if($counterItemsAdded -ge 1)
        {
        Write-Host "The solutions have been added`n" -fore green
        Write-Host "Remember, the solutions will not be available until you deploy them" -fore yellow
        }
        else
        {
        Write-Host "The specified directory didn’t contain any valid .WSP or .CAB files" -fore red
        }
    }
#If the current path does not exist, ask for the path again   
else   
    {
    Write-Host "Directory does not exist. `nPlease enter the path in the following format: C:\Solutions" -fore yellow
    addSolutions(getPath)
    }
}

Write-Host "This script will add all solutions in the specified directory to SharePoint 2010" -fore green

#Call functions
addSolutions(getPath)

The output of this script (when there are actually some valid .wsp files in the directory) will look something like this:

image

Like the output says, in order to use the solutions, you will have to deploy them first. You can do this manually, by using the PowerShell command “Install-SPSolution”, or you can go to part 3, where I created a script to do deploy ALL added solutions to your web applications.