Skip to main content

Create Search Scope and apply ruele using power shell also define Search Setting to Site Collection

[string]$searchSiteName = "Search"

# Creates the search scope on site so it shows on the dropdown
function CreateSearchScope([string] $siteURL){

    # Variables for Site
    [string]$searchServiceApplicationName = "Search Service Application"

    [string]$adminServiceName = "SPAdmeinV4"
    [string]$lmsContentTypeTemplateID = "STS_ListItem_10001"
    [string]$searchDropDownValue= "Search Dropdown"
    [string]$scopeName = "LMS Search"

    # Gets the Search Service Application Object
    Write-Host "Get-SPEnterPriseSearchServiceApplication" -foregroundcolor green
    $ssa = Get-SPEnterPriseSearchServiceApplication -Identity $searchServiceApplicationName

    # Get the root web
    Write-Host "Get the root web." -foregroundcolor green
    $site = New-Object Microsoft.SharePoint.SPSite($siteURL)

    # Gets the scope
    Write-Host "Get-SPEnterpriseSearchQueryScope" -foregroundcolor green
    $scope = Get-SPEnterpriseSearchQueryScope -Identity $scopeName -SearchApplication $ssa -ea "silentlycontinue"

    Write-Host "Create a scope object" -foregroundcolor green
    $existingScope = $null
    if($scope -eq $null)
    {
        Write-Host "Create a scope New-SPEnterpriseSearchQueryScope" -foregroundcolor green
        $existingScope = New-SPEnterpriseSearchQueryScope -Name $scopeName -Description "Searches LMS Course Content Type Lists only." -SearchApplication $ssa -DisplayInAdminUI $true
    }
    else
    {
        Write-Host "Gets the existing scope Get-SPEnterpriseSearchQueryScope" -foregroundcolor green
        $existingScope = Get-SPEnterpriseSearchQueryScope -Identity $scopeName -SearchApplication $ssa -ea "silentlycontinue"
    }

    Write-Host "Gets all scope rules."
    $allScopeRules = Get-SPEnterpriseSearchQueryScopeRule -SearchApplication $searchServiceApplicationName -Scope $scopeName
  
    #Checks the Rule if already exists
    $checkRule = $allScopeRules | Where-Object { $_.RuleType -eq "Url" -and $_.MatchingString -eq "$siteURL/" }
  
    ######$checkRule | ForEach-Object { Write-Host "ID : " $_.ID ", Filter Behaviour : " $_.FilterBehaviour ", LogPrefix : " $_.LogPrefix ", Property : " $_.Property ", RuleType : " $_.RuleType ", Value : " $_.Value }
  
    if( $checkRule -ne $null )
    {
        Write-Host "Rule existed for URL - New-SPEnterpriseSearchQueryScope" -foregroundcolor green
    }
    else
    {
        Write-Host "Create URL Rule - New-SPEnterpriseSearchQueryScope" -foregroundcolor green
        $rule = New-SPEnterpriseSearchQueryScopeRule -RuleType "Url" -Url $siteURL -MatchingString "$siteURL/" -FilterBehavior "Include" -UrlScopeRuleType "Folder" -scope $existingScope
    }

    #Checks the Rule if already exists
    $checkRule = $allScopeRules | Where-Object { $_.RuleType -eq "PropertyQuery" -and $_.Value -eq $lmsContentTypeTemplateID }
  
    ######$checkRule | ForEach-Object { Write-Host "ID : " $_.ID ", Filter Behaviour : " $_.FilterBehaviour ", LogPrefix : " $_.LogPrefix ", Property : " $_.Property ", RuleType : " $_.RuleType ", Value : " $_.Value }

    if( $checkRule -ne $null )
    {
        Write-Host "Rule existed for Property type - New-SPEnterpriseSearchQueryScope" -foregroundcolor green
    }
    else
    {
        Write-Host "Create Content Type - New-SPEnterpriseSearchQueryScope" -foregroundcolor green
        $rule = New-SPEnterpriseSearchQueryScopeRule -RuleType PropertyQuery -ManagedProperty "contentclass" -PropertyValue $lmsContentTypeTemplateID -FilterBehavior "Require" -url $siteURL -scope $existingScope -SearchApplication $ssa
    }


    Write-Host "Start applying settings to the web." -foregroundcolor green
    $rootWeb = $site.RootWeb
    $rootWeb.AllowUnsafeUpdates = $true
    $rootWeb.AllProperties["SRCH_ENH_FTR_URL"] = "/" + $searchSiteName
    $rootWeb.AllProperties["SRCH_SITE_DROPDOWN_MODE"] = "ShowDD"
    $rootWeb.AllProperties["SRCH_TRAGET_RESULTS_PAGE"] ="/_layouts/OSSSearchResults.aspx"
    $rootWeb.update()
    Write-Host "Finished applying settings to the web." -foregroundcolor green
  

    Write-Host "Start Calling SearchScopeGroup Ps1 for updating group" -foregroundcolor green
    .$scriptDir"\"UpdateSearchScopeGroup.ps1
  
    write-host "Loading Scripts"
    Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharpVersion3  -ErrorAction Stop
    write-host "Call Methods of C# code"-foregroundcolor green
    [gp.sp.LMSSearchScopeGroup.GPSearchScopeGroup]::UpdateGPSearchScopeGroup($siteURL)
  
    Write-Host "Finished Updating Search Dropdown and set LMS Search To Default" -foregroundcolor green


  
  }

Comments

Popular posts from this blog

Migrating from Skype for Business to Microsoft Teams: A Step-by-Step Guide

Do you still use Skype for Business to meet the communication and collaboration needs of your business? If so, now is the perfect time to think about switching to Microsoft Teams, a cutting-edge platform with cutting-edge capabilities and seamless connectivity with other Microsoft services. But if you're unfamiliar with the procedure, switching to a new platform can seem like a difficult task. I'll walk you through the process of switching from Skype for Business to Microsoft Teams in this article. Plan the migration in Step 1 You must make a plan before you start the relocation procedure. Set a deadline, make a list of all the Skype for Business capabilities you presently use, and choose whether to migrate gradually or all at once. Step 2: Set up your surroundings. Make sure your network and infrastructure fulfil Microsoft Teams' standards. Upgrades to your hardware, software, and licences might be necessary. Additionally, confirm that you have the right permissions to ca

How to: Debug SharePoint Applications

You can greatly simplify debugging by using Visual Studio extensions for Windows SharePoint Services. This topic includes procedures that explain the following: Debugging with Visual Studio extensions for Windows SharePoint Services Performing manual debugging Performing remote debugging Debugging with Visual Studio extensions for Windows SharePoint Services Press the F5 key to begin debugging with Visual Studio extensions for Windows SharePoint Services. The following procedure demonstrates how to enable F5 debugging. To enable F5 debugging Locate and open the target SharePoint application's Web.config file. By default, it is located in C:\Inetpub\wwwroot\wss\VirtualDirectories\80. Find the following line of code and change the  debug  attribute to  true . Save the changes to the Web.config file. In Visual Studio, right-click the SharePoint project, and then click  Properties . Click the  Debug  tab, and then type the target SharePoint URL in t

SharePoint 2010 – How to use Audio and Video Webpart

Video and Audio Web Part [ a.k.a Media Web Part ] is one of the new cool web parts in SharePoint 2010, so in this article I am going to show you the way to add this web part in your page. To add Media web part to the page you must activate those two features 1- SharePoint Server Publishing Infrastructure Feature in Site Collection features 2- SharePoint Server Publishing Feature in Site features After that edit page then click on Insert tab in SharePoint Ribbon then select Video and Audio Now the Media Web Part in the page Click on the Media Web Part and you will find Options Tab in the Ribbon As you can see in the Options tab there are Chang Media button allow you to select the video and Chang Image button to change the image of web part and Styles (Dark, Light) Click on Change Media to select the Media file. From Computer opens up the Upload media so you can upload the video and display it. Once you click ok and saving the page. Click on play button and enj