Monday, April 2, 2012

Troubleshoot Sharepoint Crawler with Fiddler

This Blog is simple but extremely useful (Thanks Matthew MCDermott):

http://www.ableblue.com/blog/archive/2012/01/04/troubleshooting-sharepoint-search-crawl

Find SPLists without Default View

One issues why sometimes the search crawler in SharePoint takes too long, is because of the lack of Default View on the lists form the Site. See the C# Code and explanation at
http://blogs.technet.com/b/victorbutuza/archive/2010/01/15/crawl-taking-indefinitely-to-complete.aspx
This is the power-shell version of the c# code shown on the mentioned site.

 
if(-not(
Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"})
) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
clear
$spwa = Get-SPWebApplication -Identity "http://myWebAppURL"
foreach ($osite in $spwa.Sites)
{
      foreach ($oweb in $osite.AllWebs)
      {
           write-host $oweb.Url
           write-host "========================================================="
           foreach ($olist in $oweb.Lists)
        {
            if ($olist.Hidden -eq $false)
            {
                  try
                {
                  write-host "Title: "  $olist.Title;
                    if ($olist.DefaultView -eq $null)
                    {
                                   write-host "ERROR: No default list view found " -ForegroundColor Red
                             }
                    else
                    { 
                        #remove this if you have lots of lists
                        write-host "DefaultViewURL: " $olist.DefaultViewUrl
                        write-host "DefaultViewTitle: " $olist.DefaultView.Title
                    }
                }
                catch
                {
                   write-host "ERROR"

                }
            }
            }
           write-host "========================================================="
        $oweb.Dispose();
      }
      $osite.Dispose();
}

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()


Tuesday, February 28, 2012

Get and Renaming SPFolder c#

This is how you can rename a SPFolder. There is no need to use MoveTo() as suggested by others.
The user has to have rights to do it, or enclose the whole thing inside a

var siteId = "Guid of my site";
var webUrl= "Server relative path to my web";
SPSecurity.RunWithElevatedPrivileges(delegate(){
    using (SPSite elevatedSite = new SPSite(siteId))
   {
       using (SPWeb elevatedWeb = elevatedSite.OpenWeb(webUrl))
      {

          string requestFolderUrl = webUrl+ "/MYLIST/OldFolderName";
          SPFolder requestFolder = elevatedWeb.GetFolder(requestFolderUrl);

          if (requestFolder.Exists)
          {

               requestFolderItem[SPBuiltInFieldId.Title] = "NewName"; //not really needed for renaming
               //requestFolderItem[
SPBuiltInFieldId.BaseName] =  "NewName"; //this Fails.don't know why
               requestFolderItem["BaseName"] =  "NewName"; //this Works!
               //don't want to update the Modified time nor the modified by Info
              //If you want to modify this info use Update, and avoid being in a RunWithElevatedPrivileges
             ///because else you would have the System User registered
               requestFolderItem.SystemUpdate(false);

           }
      }
   }
}

Thursday, February 16, 2012

Remove Event Handlers from SPList Powershell

This is how you can  delete Event handlers from a SPList

$webUrl= "http://yoursite.com/subsite"
$listName = "myList"

$site = Get-SPSite($siteUrl)
$web = $site.OpenWeb()
$listWithEventHandlers = $web.Lists[$listName]

#you can find out the index of the eventhadler through a selector, or just beeing lazy
#and loop the eventhanlders


 $allEvenReceivers  = $list.EventReceivers
foreach($ev in $allEvenReceivers  )
{
   write-host $ev.Name;
}
$index = 0
$eventHandlerToDelete = $listWithEventHandlers.EventReceivers[$index]$eventHandlerToDelete.Delete()
$listWithEventHandlers.Update()

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