Monday, October 17, 2016

Search Result Webpart,Filter result If QueryString is present in URL

There was a requirement to filter the search result based on QueryString present in url, and if no QueryString present then should populate all the results.

Issue was simple to resolve if every time there is QueryString present, then below Query will do the job.

Below filtering the result by checking managed metadata property equals the query string value.

Above query was filtering the result based on QueryString given in URL.

However it fails if there is no QueryString present in url.  "No result found" message appear. Ideally should populate all the results. And filter should be optional in query for this scenario.

So I need to modify the query and to add some condition like, if QueryString present then check managed metadata property equals the query string value, else no.

To achieve this, there is syntax {searchboxquery} {? <some KQL expression> } , If MyParameter querystring is not present in URL,  then just first part of KQL query will evaluate  {searchboxquery} 

Using this I started getting all results when there is no QueryString present in the url, and if QueryString is present then it shows filtered result.

Hope you find this useful!

Wednesday, October 12, 2016

Search Query by ContentType name issue

I ran into very weird issue in SharePoint Search while querying the items using ContentType name.

Query: ContentType={content type NAME}

This was giving me the result of content type, however some extra data/results where coming along in the result. There was no clue what is failing there.

Spent few hours and got very useful tip from Search Guru, MIKAEL SVENSON  that not to use ContentType but to replace that with SPContentType.

Modified Query: SPContentType={content type NAME}

Learning: We should use SPContentType instead of ContentType. As Contenttype also includes the mime type and not just the name and may cause issues. SPContentType only stores the name.

I hope this small piece of information will save your time!

SharePoint Online - Get workflow status in List Item using PowerShell

Requirement was for SharePoint online tenant to get the workflow status in list item.
I choose to write quick PowerShell script using CSOM to achieve this.

 Function Get-SPOCredentials([string]$UserName,[string]$Password)  
   if([string]::IsNullOrEmpty($Password)) {  
    $SecurePassword = Read-Host -Prompt "Enter password" -AsSecureString   
   else {  
    $SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force  
   return New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)  
  Function GetWorkflowStatus([Microsoft.SharePoint.Client.List]$list,[int]$listItemId, [string]$workflowName)  
    $context = $list.Context  
    $listItem = $list.GetItemById($listItemId)  
    $workflowStatusField = $listItem.ParentList.Fields.GetByTitle($workflowName)  
    if ($listItem.FieldValuesAsHtml[$workflowStatusField.StaticName] -ne $null)  
      $statusValue = $listItem.FieldValuesAsHtml[$workflowStatusField.StaticName]  
      return $statusValue;  
    return $null  
 $UserName = ""  
 $Password = Read-Host -Prompt "Enter the password"    
 $WebUrl = ""  
 $ListTitle = "MySPList"  
 $ListItemId = 1  
 $WorkflowTitle = "Approval"  
 $Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)  
 $Context.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password  
 $list = $Context.Web.Lists.GetByTitle($ListTitle)  
 $statusValue = GetWorkflowStatus -list $list -listItemId $ListItemId -workflowName $workflowTitle  

Hope this handy script will save your time!