Monday, August 20, 2012

Search Center Site Collection using powershell


Hi,

Our Requirement was to create site collection for Search Center.

One more important expectation is once Search Center site collection gets created, two pages are to be created under Pages Library.

Those two pages are "Advanced.aspx" and "Results.aspx".

Respective web parts  needs to available on those pages i.e.

Advanced Search Box web part should be available on "Advanced.aspx"
Search Core Results web part should be available on "Results.aspx"

For that below is the script that has been executed

// CreateSiteCollections.ps1
# Automates creation of site collections based on information stored in a XML file.

# This method returns the directory of the script
$0 = $MyInvocation.MyCommand.Definition          
$dp0 = [System.IO.Path]::GetDirectoryName($0)
$RootPath = $dp0 | split-path 


## Get XML configuration file parameters
[string]$InputFile = $("$RootPath\SiteCollectionScripts\WPSiteCollectionsInputs.xml")   

$xmlinput = [xml] (get-content $InputFile)
$item = $xmlinput.SiteCollectionConfig


#Region Create site collection in the web application, if the site collection exsits then return the existed site collection
#otherwise create a new site collection  
Function CreateSiteCollection($name,$url, $description,$webApplication,$ownerAlias, $secondaryOwnerAlias,$contentdatabase,$Template,$LCID)
{
    Try
    {
        Write-Host -ForegroundColor White "- Start to create the Site Collection: `"$name`"..."
        #$sc = get-spsite -identity $url | Out-Null
        $sc = get-spsite -limit all | Where-Object {$_.Url -eq $url}
        if($sc -eq $null)
        {

            $cntDB = get-spcontentdatabase -webapplication $webApplication | where-object {$_.name -eq $contentdatabase}
            if($cntDB -eq $null)
            {
                $cntDB = new-spcontentdatabase -name $contentdatabase -webapplication $webApplication
            }

            $sc = New-SPSite -Url $url -OwnerAlias $ownerAlias -SecondaryOwnerAlias $secondaryOwnerAlias -ContentDatabase $contentdatabase -Description $description -Name $name -Template $Template -Language $LCID
           
            #Add NT Authority/Authenticated users to Search Center and Site Provisioning Application         
          
            if($name -eq "SearchCenter")
            {
                New-SPUser -UserAlias "NT AUTHORITY\authenticated users" -web $url -PermissionLevel Contribute
               
                #add the search center site collection to the current subscription
                $sub = get-spsitesubscription    
                set-spsite -identity $sc -sitesubscription $sub

            }
        }
        else
        {
            Write-Host -ForegroundColor White "- Site collection: `"$name`" already exists, continuing..."
        }
    }
    catch
    {
        Write-Host $_
    }
}
#EndRegion


#Get the Web Application Details

$ContentPortalName = $item.ContentPortalName


$GetSPWebApplication = Get-SPWebApplication $ContentPortalName
    If ($GetSPWebApplication -eq $Null)
        {

            Write-Warning " - Web application does not exist"
            Write-Warning " - Check if the Web Application has been created or not"
            break
        }
    else
                {
            $WebApp = Get-SpWebApplication $ContentPortalName
                }


$scs = $item.selectnodes("SPSiteCollection")
foreach($sc in $scs)
{
    $name = $sc.Name 
    $scUrl = $WebApp.URL+$sc.ManagedPath
    CreateSiteCollection -name $sc.Name -url $scUrl -description $sc.Description -webApplication $WebApp -ownerAlias $sc.OwnerAlias -secondaryOwnerAlias "$env:USERDOMAIN\$env:USERNAME" -contentdatabase $sc.ContentDatabase -Template $sc.Template -LCID $sc.LCID
    Write-Host -ForegroundColor White "- The site collection: $name has been created"

               
}
// WPSiteCollectionsInputs.xml
<?xml version="1.0" ?>
<SiteCollectionConfig>
                <ContentPortalName>http://webappurl</ContentPortalName>            
                <SPSiteCollection>
                                <Name>SearchCenter</Name>
                                <Description>Enterprise Search Center Site</Description>
                                <ManagedPath>sites/SearchCenter</ManagedPath>
                                <ContentDatabase>dbname</ContentDatabase>
                                <Template>SRCHCEN#0</Template>
                                <LCID>1033</LCID>
                                <OwnerAlias>domain\username</OwnerAlias>
                                <SecondaryOwnerAlias> domain\username</SecondaryOwnerAlias>
                </SPSiteCollection>
</SiteCollectionConfig>

Finally I got the result as below

No comments:

Post a Comment