Tuesday, March 27, 2012

Find out the attaches SPEventReceivers on a given SPList

Small script to find out what event receivers are attached to a given list / document library.
Of course, you don't need that if you can have the luxury of having SharePointManager on your server.

if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
clear

$url = "https://myservername/mysite/mysubweb"
$listWebRelativeUrl = "/lists/thelist" # for a doc lib just "/DocLibName"

$site = Get-SPSite $url
$web = $site.OpenWeb()
$list =$web.GetList($web.ServerRelativeUrl + $listWebRelativeUrl)
$list.EventReceivers | foreach{write-host $_.Type " " $_.Name " " $_.ID}


$web.Dispose()
$site.Dispose()

Monday, March 26, 2012

Upload Douments in Document Library Sharepoint 2010

I needed to make some tests on Sharepoint Tresholds, so I needed a small script that uploads a large amount of documents (The same document) in my Doc Libraries.
For that I just created in the same folder where my Powershell script is running a small Test.txt file. And Uploaded it a given number of times under a new name.
Here's the script:

if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
clear

$uploadURL = "http://MYWEBURL/"
$fileName = "test.txt"
$uploadLibraries = @("Documents", "Documents2")
#I'm Uploading to the root, but you could put there a folder like "Documents/FolderName"
#I want to uplaod 6000 docs on each library... that takes a while
$TOTALDOCSTOUPLOAD =6000
$startFileIndex = 0
$thisScriptPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$oldFilePath =  $thisScriptPath + "\" + $fileName
                   
$site = Get-SPSite -Identity $uploadURL
if($site -ne $null){
    $web = $site.OpenWeb()
      if($web -ne $null -and $web.Exists){
        foreach($uploadLibrary in $uploadLibraries)
        {
            try
            {
                $docLib = $web.GetFolder("/"+$uploadLibrary)
            }
       
            catch
            {
               write-host 'Document  Library is unavailable'
                $docLib = $null
            }
            for($i = $startFileIndex; $i -lt $TOTALDOCSTOUPLOAD; $i++)
            {
                if($docLib -ne $null)
                {
                    $fileParts = $fileName.split(".")
                    $fileParts[0] = $fileParts[0]+ $i.ToString()
                    $newFileName = [string]::join('.', $fileParts)
               
                    $newFilePath =  $thisScriptPath + "\" + $newFileName
                   
                    Rename-Item $oldFilePath $newFilePath
                    $docUrl = $docLib.ServerRelativeUrl + "/" + $newFileName
                    if($docLib.Files[$docurl].Exists)
                    {
                        $file = $docLib.Files[$docurl]
                        if($file.LockType -eq "None" -and $docLib.RequiresCheckout)
                        {
                                $file.CheckOut()
                        }
                    }
                    $fileStream =$((Get-ChildItem $newFilePath).OpenRead())
                    $docLib.Files.Add($docUrl, $fileStream, $true) | Out-Null
                    $file = $docLib.Files[$docurl]
                    if($file.CheckOutType -ne "None")
                    {
                       if($docLib.RequiresCheckout) {
                        $file.CheckIn("Updated Template", 1)
                        }
                    }
                    $fileStream.Dispose()
                    Rename-Item  $newFilePath $oldFilePath
                    write-Host "Uploade "$newFilePath "to "$uploadLibrary
                }
             }  
        }

        $web.Update()
        $web.Dispose()
    }
    $site.Dispose()
}


Wednesday, March 7, 2012

Enabling / Adding Sharepoint Search Autocompletion

Ever wonder how to get that Auto-completion to work on the SharePoint search box?


Select the Service application:

$srchSvcApp = Get-SPEnterpriseSearchServiceapplication -Identity "Search Service Application"

Add a new Suggestion:

$lang = "de-de"
$newQuerySuggestion = "Powershell for Sharepoint 2010"
 New-SPEnterpriseSearchLanguageResourcePhrase -SearchApplication $srchSvcApp -Language $lang  -Type QuerySuggestionAlwaysSuggest -Name $newQuerySuggestion

Run the time Job:
$querySugestionTimer=Get-SPTimerJob|? {$_.Name -eq "Prepare Query Suggestions"}

$querySugestionTimer.RunNow()