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.


 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")  
 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")  
 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)  
    $context.Load($listItem.FieldValuesAsHtml)  
    $workflowStatusField = $listItem.ParentList.Fields.GetByTitle($workflowName)  
    $context.Load($workflowStatusField)  
    $context.ExecuteQuery()  
    if ($listItem.FieldValuesAsHtml[$workflowStatusField.StaticName] -ne $null)  
    {  
      $statusValue = $listItem.FieldValuesAsHtml[$workflowStatusField.StaticName]  
      return $statusValue;  
    }  
    return $null  
  }  
 $UserName = "username@yourtenant.onmicrosoft.com"  
 $Password = Read-Host -Prompt "Enter the password"    
 $WebUrl = "https://yourtenant.sharepoint.com/"  
 $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  
 $Context.Dispose()  

Hope this handy script will save your time!

Tuesday, June 14, 2016

Get SharePoint Document Library Size using PowerShell


There was requirement to get the document library size. Using PowerShell I able to resolve this requirement.

This post is about getting actual Document Library size using PowerShell.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
Add-PSSnapin Microsoft.SharePoint.PowerShell 

$siteURL = "YourSitename" 
$site = new-object Microsoft.SharePoint.SPSite($siteURL)


foreach ($web in $site.AllWebs)
{
 foreach ($list in $web.Lists)
{

        if($list.BaseType -eq "DocumentLibrary")   
      {
    $listSize = 0
   foreach ($item in $list.items) 
        { 
          $listSize += ($item.file).length
        }
     "Web Name: "+$web.Title+", Library Name: "+$list.Title+", Size: "+[Math]::Round(($listSize/1KB),2)+"KB"     
}
}
}

I hope this small script will save your time.

Thursday, March 17, 2016

Query User Information List gives :Error 404 List Not found- SharePoint 2013

Being a SharePoint developer we all know that 
The User Information List stores information about a user by having some metadata set up for the user. Some examples are Picture, Email, DisplayName, LoginName etc. ) This list is only visible to and accessible by administrators.
However today we faced very weird issue that not able to access this hidden list. we can browse it under "/_catalogs/users" but we were not able to query it

Problem : 
 We were not able to query the User Information List in SharePoint 2013 On-Premise.
Tried following stuffs :
1. http://your-site-name/_vti_bin/ListData.svc/UserInformationList
2. http://your-site-name//_api/web/lists/GetByTitle(‘User Information List')
3. In PowerShell - 
$SITE = GET-SPSITE "HTTPS://your-site-name"
$WEB = $SITE.ROOTWEB

$LIST = $WEB.LISTS["USER INFORMATION LIST"]
All the above was giving the 404 error, List was not exist under the $web.Lists - which was very weird.
Solution:
After some r&d found the way to access it using SiteUserInfoList object

$web.SiteUserInfoList.Items
using this we can get the items of User information list.

Happy Coding!






Sunday, December 27, 2015

Office 365 People Search:Hide default result



Out of the box, People search result webpart shows the list of people as a result.

However, I had a requirement to remove the default people search coming in result webpart when page loads.

with the default query :
- {searchboxquery}contentclass=spspeople -accountname:rapidcircle.in


Spent few hours to find the solution finally got this useful tip from my Search guru MIKAEL SVENSON

To remove the default result need to bind the query with {?<query>}

the {?{searchboxquery}} will remove everything in the query if the {searchboxquery} variable is empty

Hope this small piece of information will save your time.





Tuesday, September 29, 2015

Permission denied for "Add and Customize Pages" in Sharepoint Online


Problem:

I have created root site in SharePoint Online, weird problem I started facing is being a site administrator I was not able to customize the page/ upload the any page or master page.

As in image below, for some reason I am set as Deny to "Add and Customize Pages".






The problem is related to 'Scripting Capabilities' being turned off by default for the root site collection in Office 365.

I found some article on community:

https://support.office.com/en-us/article/Turn-scripting-capabilities-on-or-off-1f2c515f-5d7e-448a-9fd7-835da935584f?ui=en-US&=amp&rs=en-US&ad=US

It mentions that turning on scripting through the Sharepoint Admin Settings and wait around 24 hours. However, this did not work for me. still the issue was there.

Following lines of script did a trick.

Solution:

Connect-SPOService -Url https://<yoursitename>-admin.sharepoint.com -credential <admincredentials>

Set-SPOsite https://<your root site url> -DenyAddAndCustomizePages 0


And it immediately started working!

Happy Learning!