Tuesday 16 October 2012

Delete a List


Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
             
<#
.Synopsis
   Delete Sharepoint list(s) from entire site
.DESCRIPTION
   Delete SharePoint list instance(s) from an entire site, all subwebs
.EXAMPLE
   delete-cSharePointList -ListTitle ListName -webUrl http://wingtipserver
.EXAMPLE
   delete-cSharePointList -ListTitle ListName -webUrl http://wingtipserver -subwebs
.EXAMPLE
   delete-SharePointList -ListTitle_1, listTitle_2, listTitle_n -webUrl http://wingtipserver
.EXAMPLE
   delete-SharePointList -ListTitle_1, listTitle_2, listTitle_n -webUrl http://wingtipserver -subwebs
#>
function delete-SharePointList
{
    [CmdletBinding(SupportsShouldProcess=$true, confirmImpact='High')]
    Param
    (
        # an array of list titles to delete
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [System.String[]] $ListTitle,
              # The url of the web the lists reside in
              [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=1)]
              [ValidateScript({Get-SPSite -Identity $_ | Select-Object -Property Exists -ErrorAction SilentlyContinue})]
              [System.String] $siteUrl,
              # switch to specify whether to delete list from all webs
              [Parameter(ValueFromPipelineByPropertyName=$true, Position=2)]
              [switch] $subwebs
    )

    Begin
    {
              $SPSite = Get-SPSite $siteUrl
    }
   
       Process
    {
              if($subwebs)
              {
                     $SPSite.AllWebs | ForEach-object{
                           deleteList $_ $ListTitle
                     }
              }
              else
              {
                     deleteList $spsite.RootWeb $ListTitle
              }
       }
      
    End
    {
              $SPSite.Dispose()
    }
      
      
}

function deleteList($currentWeb, $listTitle)
{
       $listTitle | ForEach-Object{
              $listInstance = $currentWeb.Lists.TryGetList($_)
                          
              if($listInstance -ne $null)
              {
                     Write-Output ("found a list with the title {0} in web {1}" -f $_, $currentWeb.Url)
             
                     If ($psCmdlet.shouldProcess("$listInstance in web $webUrl", "Delete List"))
                     {
                           $listInstance.Delete()
                           Write-Output ("Deleted list {0} in {1}" -f $_, $currentWeb.Url)
                     }
                     else
                     {
                           Write-Output ("Did not delete list {0} in {1}" -f $_, $currentWeb.Url)
                     }
              }
              else
              {
                     Write-Output ("Failed to find list with title {0} in web {1}" -f $_, $currentWeb.Url)
              }
       }

}

Find Deployed Feature

If you're getting an error that refers to the oh so helpful GUID, here's a handy little script that lists Features and filters them
By Guid:
$results=Get-ChildItem "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\" feature.xml -rec

foreach ($File in $results)
{
   [xml]$feat=gc $file.pspath
   if($feat.feature.id -Like '3752ccf3*')
   {
        $feat.feature | Select ID, Title, Scope, Name
   }
}

By Title:
$results=Get-ChildItem "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\" feature.xml -rec

foreach ($File in $results)
{
   [xml]$feat=gc $file.pspath
   if($feat.feature.title-Like 'TEC*')
   {
        $feat.feature | Select ID, Title, Scope, Name
   }
}

Monday 15 October 2012

Rewire Content Database

this is the final post in reference to restoring your content database. Here I'll cover how to rewire the db permissions so that your user can actually do something with the db.

lets set our focus back to Microsoft sql server management studio.

Check under security for your machine and make sure that the user account you’re concerned with is available. in my case it's ssrap7/Administrator and to remind you my content database is WSS_Content. as you can see below, ssrap7/administrator is not included in my content database so I'm going to go ahead and add it.

to add a new user simply right click on your content database and hit add user, just make sure to select db_owner, refer to image below.

now with with db_owner checked off click the OK button. Next run the following Powershell


function LoadSharePointPowerShellEnviroment
{
                write-host
                write-host "Setting up Powershell enviroment for Sharepoint" -foregroundcolor Blue
                Add-PSSnapin "Microsoft.Sharepoint.PowerShell" -ErrorAction SilentlyContinue
                Write-host "Sharepoint PowerShell Snapin loaded." -foregroundcolor Green
}
LoadSharePointPowerShellEnviroment
Mount-SPContentDatabase "WSS_Content" -DatabaseServer "SSRAP7" -WebApplication
http://SSRAP7/

make sure to specify your database name, database server, and web application URL.

once you've run the power shell, you must add your account as local site admin

Open central administration-> application management ->Select Change site Collection administrators

This will show you the site collection administrators set the primary site collection administrator to your local administrator


hit ok, and BAM, you're done.

Restore a Content Database

In my previous post I demonstrated how to back a content database up, which is all fine and dandy but ultimately useless if you can't restore it.

Go to the manage content databases screen in central administration.
Once there make sure you have the correct web application selected, and click on your content database.
Scroll down to the bottom of the options for your content database and check off "Remove content database" then click OK
When complete, you will be redirected to the “manage content database” screen in central administration with your content database no longer available.

Do the following before the restore:
Stop IIS; use iisreset /stop in the command prompt
Stop the following services
  • SharePoint 2010 Administration
  • SharePoint 2010 Timer
Open up SQL Server Management studio
Right click on the Database you just removed
Go to Tasks-> Restore-> Database...


On the restore Database dialogue, under Source for restore check From device

Then click the browse (...) all the way to the right of the from device: radio control. This will bring up the Specify Backup dialogue.

here select the back up file you wish to restore, refer to my previous post to find out how to make one.

Once you’re back to the restore database dialogue, make sure to actually select your back up file, by checking it off.


With your back up file selected click the options page in the left hand pane. This will bring up the options page.
Make sure to check off overwrite the existing Data base, then hit OK.

This will now restore the content database with production data; this process takes a while so now would be a great time to do some documentation or a coffee break.

Now remember, start up the two services you disabled previously
  • SharePoint 2010 Administration
  • SharePoint 2010 Timer
and start up IIS again iisreset

In the next post I discuss how to rewire your content database to work with your current user