Wednesday, November 19, 2014

Search Using JSOM – SharePoint 2013

We can use JSOM for Keyword query search in SharePoint 2013. I’ll explain basics of this using a CEWP and jquery.

Prerequisites: Search should be configured and working in the system.
Solution:
  1. Create a web part page in your SharePoint 2013 or Office 365 SharePoint Site.
  2. Add Content Editor web part on the page.

3. Open any text editor/visual studio and write JS file ‘Search_JSOM.js’, code is copied below:

<script src=”//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js”></script>
<script type=”text/javascript” src=”/_layouts/15/sp.runtime.debug.js”></script>
<script type=”text/javascript” src=”/_layouts/15/sp.js”></script>
<script type=”text/javascript” src=”/_layouts/15/SP.RequestExecutor.js”></script>
<script type=”text/javascript” src=”/_layouts/15/SP.search.js”></script>

<style>
table.sresult, th.sresult , td.sresult {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table.sresult tr:nth-child(odd) {
background-color: #f1f1f1;
}
table.sresult tr:nth-child(even) {
background-color: #ffffff;
}
</style>

<script type=”text/javascript”>

$(function () {
var results;
$(“#btnSearch”).click(executeSearch);
$(“#btnSearch”).button();
});

 
//SERCH FUNCTION

function executeSearch(event) {
var appweburl = _spPageContextInfo.siteAbsoluteUrl;
var clientContext = new SP.ClientContext(appweburl);
var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext);
keywordQuery.set_queryText($(“#searchTextBox”).val());
var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);
results = searchExecutor.executeQuery(keywordQuery);
clientContext.executeQueryAsync(onQuerySuccess, onQueryFailed);
}

function onQuerySuccess(sender, args) {
$(“#result_box”).empty();
var table = $(“<table class=’sresult’>”, { ID: “resultsTable” });
table.append($(“<thead>”)
.append($(“<td>”).text(“Title”))
.append($(“<td>”).text(“Path”))
.append($(“<td>”).text(“Owner”))
.append($(“<td>”).text(“Modified”)));

$.each(results.m_value.ResultTables[0].ResultRows, function () {
table.append($(“<tr>”)
.append($(“<td>”).append(this.Title))
.append($(“<td>”).append(this.Path))
.append($(“<td>”).append(this.Author))
.append($(“<td>”).append(this.Write)));
});

$(“#result_box”).append(table);
}

function onQueryFailed(sender, args) {
$(“#result_box”).empty();
$(“#result_box”).text(“Error: ” + ‘Request failed. ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}

</script>

<div>
<label for=”searchTextBox”>Search: </label>
<input id=”searchTextBox” type=’text’ style=”width: 200px”></input>
<input id=”btnSearch” type=’button’ style=”width: 100px” value=”Search”></input>
<br/>
<div id=”result_box”></div>
</div>

JS explained:
JS starts with required script references (jquery, sp.js, sp.search.js etc.) and style for search results table. Followed by java script function ‘executeSearch’ to get client context, read ‘keyword’ and then execute search query on keyword. ‘onQuerySuccess’ function will handle success case and create a table with search results that will be displayed on the page. ‘onQueryFailed’ will display error message if ‘executeQueryAsync’ fails. Finally a text box to provide ‘keyword’, button to search and div for search results are added as html.

4. Now upload this ‘Search_JSOM.js’ to site assets or any other library of SharePoint site.
5. Edit Content Editor Web part on page and provide URL of ‘Search_JSOM.js’ file as Content Link. Save the page, it will look like:













6. Enter a valid text which exists in SharePoint and already crawled, and click on search button; relevant results will be displayed as a table.
















Thank you...

Monday, November 17, 2014

Customize Display templates – SharePoint 2013 & SharePoint Online

In SharePoint 2013 we have display templates to handle the display of search results in web parts. I would recommend to read this msdn article to understand display templates first.

I will give a walk through on how do we customize a display template for a particular requirement (in this case to display e-mail address of person).

Prerequisites: Search should be configured and working in the system.    
Solution:
1.       Create a web part page “MySearch.aspx”, and added “Search Box” & “Search Results” web parts.
2.       Configure both the web parts that search box query goes to search result.


3.       Go to Site Settings -> Master Page Gallery -> Display Templates -> Search
4.       Download “Item_Person.html” file, the display template for People Item.

5. Change html to display Email – add the highlighted code as below:

6.       Upload the updated file to display template library and publish the file.
7.       Go to the MySearch page -> Edit Page-> Edit ‘Search Results’ Web part
8.       Under ‘Display Templates’, chose ‘Use a single template to display items’ and select ‘People Item’.

                
                       
9.       Save and exit the Page.
10.   Search name in search box, it should return email id with People data.


Sunday, July 6, 2014

PowerShell: Script to update solution

Solution deployment is one important task for SharePoint developer/admin, being it in local environment or any other Production/non-production farm.
Here is a script to automate this job and avoid errors. Only manual task here is to provide “Variable Values”, i.e. – name of WSP, path, web application URL etc.
In this case WSP is already added to farm solution store and deployed. We did some changes in existing code and need to update the solution. The Update-SPSolution cmdlet upgrades a deployed SharePoint solution in the farm. Use this cmdlet only if a new solution contains the same set of files and features as the deployed solution. If files and features are different, the solution must be retracted and redeployed.

1.       Copy the script below, save it as PS file, i.e. – UpdateSolution.ps1
2.       Edit it in any compatible editor; change Declaration of the Variables section as per your requirement.
3.       Open PS prompt/SharePoint management Shell with admin rights.
4.       Navigate to the folder where PS file is saved
5.       Execute the PS file as .\UpdateSolution.ps1


# ***********Script Start*********************************************
## Function – Solution Update
## Author – Deepak Solanki (deepaksinghsolanki@gmail.com)
## Version – 1.0
##
## Checks to ensure that Microsoft.SharePoint.Powershell is loaded, if not, adding pssnapin
## Checks if solution is already exists or deployed in Farm and give appropriate message
## The Update-SPSolution cmdlet upgrades a deployed SharePoint solution in the farm.
## Use this cmdlet only if a new solution contains the same set of files and features as the deployed solution.
## If files and features are different, the solution must be retracted and redeployed.
# ********************************************************************

 #########################Declaration of the Variables###############################
#Solution Name
$SolutionName = “MySolution.wsp”;
#Literal Path
$SolutionPath = “C:\Deployment\MySolution.wsp”;
############################ Declaration of the (helper) function   # ####################
Function UpdateSolution([string] $SolutionName, [string] $SolutionPath)
{  
if($SolutionName.ToLower().EndsWith(“.wsp”) -and $SolutionPath.ToLower().EndsWith(“.wsp”))  
{     
$count = 0     
##check to ensure that Microsoft.SharePoint.Powershell is loaded     
$snapin = get-pssnapin | where-object{$_.Name -eq “Microsoft.SharePoint.Powershell”}    if($snapin -eq $null) 
{     
write-host “Loading SharePoint snapin”     
add-pssnapin “Microsoft.SharePoint.Powershell”      } 
     ##check if WSP already exists in Farm     
$WSPExist = Get-SPSolution | Where {  ($SolutionName -eq $_.Name)} 
     if($WSPExist -eq $null)     
{        
Write-Host “$SolutionName Solution does not exist in solution store, can not be updated.” -ForegroundColor Red     
}     
elseif ((Get-SPSolution $SolutionName).Deployed)   
{    
Write-Host “$SolutionName Solution already exists in solution store and has been deployed. Starting update…” -ForegroundColor Green    
$SolutionTocheck = Get-SPSolution -identity $SolutionName    
if ($SolutionToCheck.ContainsGlobalAssembly)         
{     
Update-SPSolution -Identity $SolutionTocheck -LiteralPath $SolutionPath -GACDeployment    
}    
else    
{     
Update-SPSolution -Identity $SolutionTocheck -LiteralPath $SolutionPath    
}    
Write-Host “Operation completed.”  
 }     
else     
{      
Write-Host “$SolutionName Solution exists in solution store but has not been deployed, can not be updated” -ForegroundColor Red   
}  
}  
else  
{  Write-Host “Please provide a valid parameter” -ForegroundColor Red  }
}
############################Actual Function Call  # ###########################
UpdateSolution $SolutionName $SolutionPath;

# ***********Script End here*****************************


PowerShell – Reading from CSV file

Reading from a csv, and doing some operation on the records returned by csv is one of the most common tasks in PowerShell. I’ll try to explain different cases here with sample code.
Below is the sample csv file “Sourcedata.csv” we’ll be using as example

TaskID, AssignTo, Duration
001, Tom, 55
002, Rand, 230
003, Mat, 86

##Script Starts here
 ##First declare a variable to store full path of  csv file
$SourceFile = “C:\Sourcedata.csv”;

##Use Test-Path to ensure that given file exists
if(Test-Path $SourceFile)
{
## Case 1 – Read the full CSV file and store result in a variable
$allRecords = Import-csv -path $SourceFile

##Case 2 – If csv has huge data and you want selected rows only, you can filter data with where condition
$selectedRecords = Import-csv -path $SourceFile|Where-Object {$_.AssignTo.Contains(“Rand”)}

##Case 3- If csv has data but no column headers and you want to put headers in order to manipulate data later, you can add headers while importing csv
$allRecordswithHeader = Import-csv -Header TaskID, AssignTo, Duration -path $SourceFile

#Now you have data with you and want to start operations on each of the row, simple- iterate through records
foreach($record in $allRecords)
{
$assignTo = $record.AssignTo;
Write-Host $assignTo
## Do your operation here..
}
##Script Ends here

Happy scripting 

PowerShell : Create Managed Properties in SharePoint Search Service Application

A common requirement in SharePoint search is to create Managed Properties and map it to crawled properties so that it can be used in search query.
Below is the approach to write a PS script that can be used to create one or more managed properties using an input XML file.
1. Create an xml file ‘ManagedProperties.xml’ as below and provide details.




2. Write the script in any suitable editor and save it as ”ManagedProperties.ps1′
# ******************************************************************* #
# Function – ManagedProperty Creation #
# Author – Deepak Solanki [deepaksinghsolanki@gmail.com]
# ********************************************************************
######################################################
# make sure to load SharePoint snapin if not defined #######################################################[System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
$snapin=”Microsoft.SharePoint.PowerShell”
if (get-pssnapin $snapin -ea “silentlycontinue”) 
{    
write-host -f Green “PSsnapin $snapin is loaded” 
}
elseif (get-pssnapin $snapin -registered -ea “silentlycontinue”) 
{    
write-host -f Green “PSsnapin $snapin is registered”     Add-PSSnapin $snapin     write-host -f Green “PSsnapin $snapin is loaded” 
}
else 
{     
write-host -f orange “PSSnapin $snapin not found” -foregroundcolor Red 
}

#################################################################
# Function definition #
function createManagedProperty($xmlFile,$searchApplicationName) 
{    
$configXml = [xml]( get-content $xmlFile)    
$searchapp = Get-SPEnterpriseSearchServiceApplication $searchApplicationName
$category = Get-SPEnterpriseSearchMetadataCategory -Identity SharePoint -SearchApplication $searchapp
 foreach($metaProp in  $configXml.managedproperties.managedproperty)
{        
Write-Host -ForegroundColor Green “Creating property: ” $metaProp.Name         $crawledPropName = $metaProp.crawlproperty           
##Check if Managed property already exist        
$ManagedProp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication  $searchapp -Identity $metaProp.name -ErrorAction SilentlyContinue
  ##Delete existing managed property        
if($ManagedProp -ne $null)         
{            
$ManagedProp.DeleteAllMappings()            
Remove-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Identity $metaProp.name -Confirm:$false         
}   
else   
{    
##Create Managed property    
New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name $metaProp.name -Type $metaProp.type -Queryable $true -Retrievable $true    
     
$ManagedProp = Get-SPEnterpriseSearchMetadataManagedProperty -SearchApplication  $searchapp -Identity $metaProp.name      
##Check if Crawled Property is available        
$crawledproperty = Get-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Name $crawledPropName -ErrorAction SilentlyContinue     
##Map managed property to crawl property        
if($crawledproperty -ne $null)         
{            
New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $ManagedProp -CrawledProperty $crawledproperty         
}   
else   
{    
Write-Host -ForegroundColor Red “Crawled property does not exist: ” $crawledPropName     
}           
}
}
###########################################
# Declaration of the constants            #
###########################################
$SearchApplicationName = “Search Service Application”
$configfile = “ManagedProperties.xml”
$executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$file = “$executingScriptDirectory/$configfile”
###Actual execution starts here###
Write-Host -foregroundcolor Green “***Script Started***”
createManagedProperty -searchApplicationName $SearchApplicationName -xmlFile $file
Write-Host -foregroundcolor Green “***Script Completed***”

3. Make sure that PS script and XML file are in same directory.
4. Ensure that you have provided correct “$SearchApplicationName” in PS script.
5. Open the PowerShell/SharePoint PowerShell console and run the PS script.
6. While Copying XML and script from this blog, verify if special characters (“-$ etc) are correct.
Happy Scripting!!!


Sunday, October 7, 2012

PowerShell - Solution deployment simplified


As a SharePoint developer or Admin, what we do most frequently in our day-to-day activities; is to play around with SharePoint solutions (WSPs) and Features..
And since we cannot memorize all the available operations, here are simplified PowerShell commands:

SPSolution Operations:

Add Solution to SharePoint Farm- You are ready with solution package (WSP) for deployment and this is a fresh deployment; first time deployment to the farm. SharePoint can consume a solution only if it is uploaded in SP solution store, and the operation to upload a solution is Add-SPSolution

Command:
Add-SPSolution -LiteralPath -Confirm
Parameters:
LiteralPath: Mandatory, Takes full path of WSP as string
Confirm: Optional, Ask for confirmation before operation
Example:
Add-Spsolution -Literalpath "C:\Solutions\MySolution.wsp"

Install Solution - Now solution is available in farm, next step is to use it. Installing a WSP comes with many options based on scope (for specific web application or all) and content (DLL(s) for GAC or files).
Command:
Install-SPSolution -Identity -GACDeployment
Install-SPSolution -Identity -AllWebApplication -GACDeployment
Install-SPSolution -Identity -WebApplication -GACDeployment
Parameters:
Identity: Required, Full name/GUID of WSP as string
AllWebapplications: Optional, If WSP to be used by all webapplications in farm
WebApplication: Optional, If WSP is for specific web application, URL as string
GACDeployment: Optional, If WSP includes some DLL to GAC
Force: this parameter can be used if installation is throwing some error due to feature activation or time job issue.
Confirm: Optional, Ask for confirmation before operation
Example:
Install-SPSolution -Identity "MySolution.wsp" -GACDeployment
Install-SPSolution -Identity "MySolution.wsp" -AllWebApplication -GACDeployment
Install-SPSolution -Identity "MySolution.wsp" -WebApplication "http://mysite.mydomain.com/" -GACDeployment

Upgrade a deployed Solution- Your solution package (WSP) is already deployed and installed; now you did some changes in code and want to deploy the latest, here comes Update-SPSolution. This command will upgrade the deployed solution with latest DLL(s) and files.
Command:
Update-SPSolution -Identity -LiteralPath -GACDeployment
Parameters:
Identity: Required, Full name/GUID of WSP as string
LiteralPath: Mandatory, Takes full path of WSP as string
GACDeployment: Optional, If WSP includes some DLL to GAC
Force: this parameter can be used if installation is throwing some error due to feature activation or time job issue.
Confirm: Optional, Ask for confirmation before operation
Example:
Update-SPSolution -Identity MySolution.wsp -LiteralPath "C:\Solutions\MySolution.wsp" -GACDeployment

Deployment Rollback: There are scenarios where we may need to rollback the deployment; If the deployment caused some major issue in farm/application or you added a big component (i.e.-site/list definition) in you solution and want a fresh deployment. To rollback a deployment, first retract/uninstall the WSP and then remove it from farm.
Retract Solution - Similar to installation, uninstalling a WSP comes with many options based on scope (for specific web application or all).
Command:
Uninstall-SPSolution -Identity -Confirm
Uninstall-SPSolution -Identity -AllWebApplication -Confirm
Uninstall-SPSolution -Identity -WebApplication -Confirm
Parameters:
Identity: Required, Full name/GUID of WSP as string
AllWebapplications: Required, If WSP to be retracted from all webapplications in farm
WebApplication: Required, If WSP is for specific web application, URL as string
Confirm: Optional, Ask for confirmation before operation
Example:
Uninstall-SPSolution -Identity "MySolution.wsp"
Uninstall-SPSolution -Identity "MySolution.wsp" -AllWebApplication -Confirm:$false
Uninstall-SPSolution -Identity "MySolution.wsp" -Webapplication "http://mysite.mydomain.com/" -Confirm:$false

Remove Solution to SharePoint Farm- After retraction, solution can be removed from SharePoint farm solution store.
Command:
Remove-SPSolution -Identity -Confirm
Parameters:
Identity: Required, Full name/GUID of WSP as string
Confirm: Optional, Ask for confirmation before operation
Example:
Remove-SPSolution "MySolution.wsp"
Remove-SPSolution -Identity "Unilever.Global.CDF.wsp" -Confirm:$false

SPFeature Operations:
Install/Uninstall Features in SharePoint Farm- Normally feature installation/un-installation are taken care with SPSolution operations which contains the feature. But we come across situations where features needs to be Installed/Uninstalled separately. Below are the commands and examples.

Install Feature Command:
Install-SPFeature -Path -Force -Confirm
Parameters:
Path: Mandatory, Takes relative path from the folder “$env:ProgramFiles\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\” to the feature as string
Confirm: Optional, Ask for confirmation before operation ($true/$false)
Force: Optional, this parameter can be used for force installation.
Example:
Install-SPFeature -Path "MyFeature"

Un-Install Feature Command:
Uninstall-SPFeature -Path -Force -Confirm
Parameters:
Path: Mandatory, Takes relative path from the folder “$env:ProgramFiles\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\” to the feature as string
Confirm: Optional, Ask for confirmation before operation ($true/$false)
Force: Optional, this parameter can be used for force operation.
Example:
Uninstall-SPFeature -Path "MyFeature"

Activate/Deactivate Feature- Below are the commands and examples.
Activate Feature Command:
Enable-SPFeature –Identity -URL Force -Confirm
Parameters:
Identity: Required, Name or GUID of feature as string
URL: Optional,Site/Web URL depending on feature scope
Confirm: Optional, Ask for confirmation before operation ($true/$false)
Force: Optional, this parameter can be used for force installation.
Example:
Enable-SPFeature –Identity "MyFeature" -URL "http://mysite.mydomain.com"

Deactivate Feature Command:
Disable-SPFeature –Identity -URL Force -Confirm
Parameters:
Identity: Required, Name or GUID of feature as string
URL: Optional,Site/Web URL depending on feature scope
Confirm: Optional, Ask for confirmation before operation ($true/$false)
Force: Optional, this parameter can be used for force operation.
Example:
Disable-SPFeature –Identity "MyFeature" -URL "http://mysite.mydomain.com"