Pages

Search This Blog

Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Wednesday, November 9, 2016

Add Query Rule to SharePoint Search with Powershell for ignoring specific lists and libraries from being searched

If you are using SharePoint Search for your portal, you will observe that there may be many lists/libraries which gets searched, although you might not want the content for those libraries to be searchable. As an example, workflow history lists, custom lists containing master data.

A better way to exclude these lists is to identify the lists/libraries and add them in ignore rule for search. I have created a powershell script for the same. This can be helpful in case we want to add the rule in multiple server farms. Copy below code and paste it in file with extension - .ps1

#_____________________________Add Ignore Rule to Search_____________________________________

#Add SharePoint PowerShell SnapIn if not already added
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
       Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

# just append the list or library name in the variables below which you want to be ignored in search
$ListNames = ("NintexWorkflowHistory","Links","Test List")
$LibraryNames = ("Pages", "Test Library")

$CrawlPaths = ("[SITE_URL]/.*([LIBRARY_NAMES])/.*", "[SITE_URL].*/Lists/([LIST_NAMES])/.*")


# Grab the web app URL from User
write-host "Enter the Site Url:"
[string]$strSiteUrl = Read-Host


[string]$strListNames=""
[string]$strLibraryNames=""

try
{

    if ($strSiteUrl -eq $null)
    {
        Write-Host "Site URL is missing!" -foregroundcolor "magenta"
        return;
    }

    [string]$pipe = ""

    foreach ($List in $ListNames)
    {
        $strListNames = [string]::Concat($strListNames,$pipe,"(", $List ,")" )
        $pipe= "|"
    }
   
    $pipe=""   
    foreach ($Library in $LibraryNames)
    {
         $strLibraryNames = [string]::Concat($strLibraryNames,$pipe,"(", $Library ,")" )
         $pipe= "|"
    }

   

    $SearchApp = Get-SPEnterpriseSearchServiceApplication
  

    foreach ($CrawlPath in $CrawlPaths)
    {
        $CrawlPath = $CrawlPath.Replace("[SITE_URL]",$strSiteUrl);
        $CrawlPath = $CrawlPath.Replace("[LIBRARY_NAMES]",$strLibraryNames);
        $CrawlPath = $CrawlPath.Replace("[LIST_NAMES]",$strListNames);
        
        if ((Get-SPEnterpriseSearchCrawlRule -SearchApplication $SearchApp -Identity $CrawlPath -EA SilentlyContinue))
        {
            #Remove-SPEnterpriseSearchCrawlRule -SearchApplication $SearchApp -Identity $CrawlPath -confirm:$false
            Write-Host "Crawl Rule already exists:" + $CrawlPath -foregroundcolor "magenta"
        }
        else
        {
            $Rule = New-SPEnterpriseSearchCrawlRule -SearchApplication $SearchApp -Path $CrawlPath -Type ExclusionRule -CrawlAsHttp 0 -FollowComplexUrls 0 -IsAdvancedRegularExpression 1
            $Rule.Update()
        }
    }
     Write-Host "Completed successfully..." -foregroundcolor "green"
}
catch
{
    Write-Host  "An exception occurred. Aborting. Exception :" + $_.Exception.ToString()  -foregroundcolor "red"
}



Once deployed, you can view the added rule in SharePoint Search Application.




Monday, July 27, 2015

SharePoint CRUD Operations on ListItem PropertyBag

# Initialize the SharePoint PowerShell environment
Add-PSSnapIn "Microsoft.SharePoint.Powershell" -ErrorAction:SilentlyContinue



# Function to perform add , delete , view and update operation on list item properties
# Parameters :
# -- ItemBagKey : Name of property of list item
# -- Operation : Type of operation  "ADD" , "UPDATE" , "DELETE" , "VIEW"
# -- NewValue : Value for the item property 
# -- Item : List item to operate upon 


Function UpdateKey{
      param([string]$ItemBagKey,[string]$Operation,[string]$NewValue,[Microsoft.SharePoint.SPListItem]$item)
      if($Operation -eq "ADD") # If we are adding property to item.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  Write-Host "Key already found in item bag." -ForegroundColor Yellow;
                  $item.Properties[$ItemBagKey] = $NewValue;
                  $item.SystemUpdate($false);
                  Write-Host "Key update with new value : "  $NewValue - ForegroundColor Green;
            }
            else
            {
                  Write-Host "Key not found in item bag." -ForegroundColor Yellow;
                  $item.Properties.Add($ItemBagKey,$NewValue);
                  $item.SystemUpdate($false);
                  Write-Host "Key added with value : "  $NewValue -ForegroundColor Green;
            }
      }
      elseif($Operation -eq "DELETE") # If we are deleting property from item.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  $item.Properties.Remove($ItemBagKey);
                  $item.SystemUpdate($false);
                  Write-Host "Key removed from item bag : "  $ItemBagKey -ForegroundColor Green;
            }
            else
            {
                  Write-Host "Key not found in item bag to remove." -ForegroundColor Red;
            }
      }
      elseif($Operation -eq "UPDATE") # If we are updating property of item.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  $item.Properties[$ItemBagKey] = $NewValue;
                  $item.SystemUpdate($false);
                  Write-Host "Key updated with new value : "  $NewValue -ForegroundColor Green;
            }
            else
            {
                  Write-Host "Key not found in item bag." -ForegroundColor Red;
            }
      }
      elseif($Operation -eq "VIEW") # If we want to view value item property.
      {
            if($item.Properties.ContainsKey($ItemBagKey))
            {
                  Write-Host "Property value : "  $item.Properties[$ItemBagKey] -ForegroundColor Yellow;
            }
            else
            {
                  Write-Host "Key not found in item bag." -ForegroundColor Red;
            }
      }
}

try
{
     
      #Put your site URL in placeholder <siteurl> 
      $web = Get-SPWeb <siteurl>     

      #Put list name in placeholder <listname> 
      $list = $web.Lists["<listname>"] 


      #If the list is found
      if($list)
      {
  
  #Put list item id in placeholder <itemid> 
  $item = $list.GetItemById(<itemid> );


  #Put Item property key that you want to operate upon in placeholder               <propkey> 
  $_ItemBagKey = "<propkey>"


  #Replace placeholder <OP> with type of operation you want to perfrom it            could be one among four values ( ADD, UPDATE , DELETE , VIEW )
  $_Operation  = "<OP>"  # "ADD" # "UPDATE" # "DELETE" # "VIEW"


       #Replace placeholder <propvalue> with new value that you want to update          if you want to delete or view just leave the value blank.
       $_NewValue   = "<propvalue>"
  

 #call function to perform operation
 UpdateKey $_ItemBagKey $_Operation $_NewValue $item


 #Dispose web object to release resources.
 $web.Dispose()

}
else
{
   Write-Host "List not found." -ForegroundColor Red;
}
}
catch
{
   # Add your exception handling routine here to trap and handle the exception details.
}






Wednesday, April 1, 2015

Remove all items from a List in SharePoint through PowerShell


We can use below script to clean data from multiple lists in a SharePoint site. This could be very helpful while developing solutions in SharePoint as we may need to clean the data many times during development. This script requires path to a text file which contains the list names ( each name should be in a new line ).

Copy paste below script in a text file and rename the file extension to - *.ps1 

Write-Host
Write-Host

write-host "Enter the SharePoint Site URL : "
[string]$URL = Read-Host
[string]$WebURL =  $URL.Trim()


$objSite = Get-SPWeb $WebURL -ErrorAction SilentlyContinue


if($objSite -eq $null)
{
               Write-Host
               Write-Host "Site URL not correct or Site does not extsts. Re-run the script with correct URL." -foregroundcolor Red
               Read-Host
}
else
{
        Write-Host
        Write-Host
           write-host "Enter the Text File Path that contains Name of Lists to be cleared : "
        [string]$FilePathTmp = Read-Host
        [string]$FilePath =  $FilePathTmp.Trim().Replace('"','')
        $ListNames = Get-Content -Path $FilePath
        if($ListNames -eq $null)
        {
            Write-Host
               Write-Host "Sorry! Unable to get contents from file." -foregroundcolor Red
               Read-Host
        }
        else
        {
            Write-Host
            write-host  "["  $ListNames.count  "] List Name(s) found in file. " -foregroundcolor Green
            Write-Host
            if( $ListNames.count -gt 0)
            {
                [bool] $found=$false
                foreach ($ListName in $ListNames)
                {
                    if($ListName -ne $null)
                    {
                        $list = $objSite.lists | where {$_.title -eq $ListName}
                        if($list -ne $null)
                        {
                            $found=$true
                            Write-Host $ListName " Found ...." -foregroundcolor Green
                            Write-host "---- List $($list.title) has $($list.items.count) entries"
                            $items = $list.items
                            foreach ($item in $items)
                            {
                                $list.getitembyid($Item.id).Delete()
                            }
                            Write-Host "---- List " $ListName " cleared successfully...." -foregroundcolor Green
                        }
                        else
                        {
                            Write-Host
                            Write-Host $ListName " NOT FOUND in current site...." -foregroundcolor Red
                        }
                    }
                }
                if($found -eq $true)
                {
                    Write-Host
                    Write-Host "Lists Cleared Sucessfully.... Press Enter to exit." -foregroundcolor Yellow
                       Read-Host
                }
            }
            else
            {
                Write-Host
                Write-Host "No List Name found in file.... Press Enter to exit." -foregroundcolor Red
                   Read-Host
            }
           
        }
        $objSite.Dispose()
}