Friday, October 31, 2014

SharePoint 2013 Warmup Script

Context: People were complaining over the performance of their SharePoint sites. So, I decided to write a SharePoint 2013 warmup script. I followed Jasonth script and changed it according to my needs by adding a xml file where I specified all the configurations. The configuration file consist of information, which webapplications should be invoked fully (all Site collections) and which specific site collections should be invoked.
The following is my PowerShell warmup script for Sharepoint 2013;
clear

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$configurationFilePath = 'D:\Workspace\PS-Scripts\WarmupSettings.xml';

Start-SPAssignment -Global

if (test-path $configurationFilePath) {
    
    [xml]$configurationFile = Get-Content $configurationFilePath;

    Write-Host "Invoking WebApplications with all site(s)...." -ForegroundColor Cyan;
    # Get All WebApplications whose all sites will be invoked
    $appUrls = Select-Xml -Xml $configurationFile -XPath "//WebAplication[@AllSites='TRUE']" |
                    ForEach-Object { $_.Node.url }
                              
    foreach ($appUrl in $appUrls) {        
        $sites = get-spsite -webapplication $appUrl -Limit ALL
        write-host 'WebApplication: ' $appUrl -foregroundcolor "magenta";
        foreach ($site in $sites) {
            write-host $site.Url;
            $r = Invoke-WebRequest -URI $site.Url -UseDefaultCredentials
            $r.StatusCode
        }        
    }

    Write-Host "Invoking Specific Site Collections......." -ForegroundColor Cyan;
    # Invoke Specific Site Collections
    $apps = Select-Xml -Xml $configurationFile -XPath "//WebAplication[@AllSites='FALSE']";
    foreach ($app in $apps) {
         <#$appUrl = $app.Node.url;
         $app = $apps.Node[0]
         $siteUrls = Select-Xml -Xml $app -XPath "/SiteCollection" |
                     ForEach-Object { $_.Node.InnerText }#>         
         
         $appUrl = $app.Node.url;
         write-host 'WebApplication: ' $app.Node.url -foregroundcolor "magenta";
         foreach($siteUrl in $app.Node.SiteCollection){
            $siteCollectionUrl = $appUrl + $siteUrl
            Write-Host $siteCollectionUrl
            $r = Invoke-WebRequest -URI $siteCollectionUrl -UseDefaultCredentials
            $r.StatusCode
         }                                       
    }

                    
 }

Stop-SPAssignment -Global

Write-Host "Finished Invoking Sites....." -ForegroundColor DarkYellow;
The xml file named as ‘WarmupSettings.xml’ is as follows. It is important to note that each WebApplication element has a property ‘AllSites’. When AllSites is set to TRUE, the PS script is going to invoke each site collection once. When AllSites element is set to false, PS Script will invoke site collections which are specified in the SiteCollection element.

  
 
 
 
  /sites/Help  
 
 
  /sites/test1
  /sites/test2
  /sites/test3
  
   

Thursday, October 30, 2014

Caml Query with Managed Metadata, Date Time and Content Type in PowerShell

Context: I had to fetch list items from a list based on a CAML query. The caml query should fetch records based on two different column types(Managed metadata, datetime) and a contenttype.
The following is the PS script that I wrote to accomplish this task.

$listName = "List Name"
$webURL = "http://sharepoint/sites/sharepoint";
$para = "Managed metadata value";
$today = [Microsoft.SharePoint.Utilities.SPUtility]
         ::CreateISO8601DateTimeFromSystemDateTime([DateTime]::Today)
$ContentType = "custom contenttype"; 

$query = New-Object Microsoft.SharePoint.SPQuery
$camlQuery = [string]:: Format('
            
                
                    
                    {0}
                
                
                    
                        
                        {1}
                    
                     
                        
                        {2}
                        
                    
                           
            
        ',$ContentType , $para, $today);

$query.Query = $camlQuery
$listItem = $list.GetItems($query);

foreach($item in $listItem){ 
    Write-Host 'ID: ' $item['ID'] 
    Write-Host 'Title: ' $item['Title'] 
}