Get Crawl Status Using Powershell

{Edit} I’ve found another way of doing this{/}

Way One:

I needed a way of knowing when a crawl status was set to paused.  Using a combination of Windows Task, Powershell, SharePoint List, and Workflow I was able to come up with a solution.

Process: Windows Task runs every hour (on the server with Central Administration).  This Task runs my Powershell command.  The command checks all my Content Sources for a crawl status of paused. If the status is paused, the command will write an Item to a SharePoint List.  Associated Workflow on the List then sends me an email.

Link to the completed Powershell command: Powershell Crawl Status (you will need to open the file and save it with a .ps1 extension.)

[void] [System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

[void] [System.Reflection.Assembly]::Load("Microsoft.Office.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

[void] [System.Reflection.Assembly]::Load("Microsoft.Office.Server.Search, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c") | out-null

$SITEURL = "http://win-severname"

$spsite = new-object Microsoft.SharePoint.SPSite ( $SITEURL )
$serverContext = [Microsoft.Office.Server.ServerContext]::Default
$context = [Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($serverContext)
$sspcontent = new-object Microsoft.Office.Server.Search.Administration.Content($context)
$sspContentSources = $sspcontent.ContentSources

[int]$count

$count=0

foreach ($cs in $sspContentSources)

{
if ($cs.CrawlStatus -eq [Microsoft.Office.Server.Search.Administration.CrawlStatus]::Paused)
{

$count++

}
}

if ($count -gt 0)

{

$spweb = $spsite.OpenWeb()
$splist = $spweb.Lists["ListName"];
$items=$splist.get_items() | where { $_.Title -like '*' }

if ($splist.ItemCount -gt 0)
{
$items | % { $_.Delete() }
}
$newItem = $splist.Items.Add()
$newItem["Title"] = "NewItem"
$newItem.Update()

$spweb.Dispose()
$spsite.Dispose()


}

——————————

How to run a Powershell command from a Scheduled Task.

To run a Powershell command you will need to do the following.

1.In the Actions tab:

a.Click New. The New Action dialog box appears.

2.In Settings, in Program/Script, type:

powershell.exe

3.In Add arguments, type the following:

-command “C:\Powershell Crawl Status.ps1”

4.Click OK.

http://technet.microsoft.com/en-us/library/ee649304(WS.10).aspx

Way Two and the EASY way:


$web = New-Object System.Net.WebClient
$web.UseDefaultCredentials = $true

If ($web.DownloadString(“http://craigslistSpam/ssp/admin/_layouts/listcontentsources.aspx”)| select-string “Paused” -CaseSensitive)
{
$emailFrom = "me@craigslistSpam.com"
$emailTo = "you@craigslistSpam.com"
$subject = "testing"
$body = "test email"
$smtpServer = "this can be found in Outlook"
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)
}

 

 

 

Powershell delete items in a list or library

There might be a better way to accomplish this… but for now this works.  Just needed a way to delete all the items in a list.

[System.reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
$spsite = New-Object -TypeName “Microsoft.SharePoint.SPSite” -ArgumentList http://win-vm3$spweb = $spsite.OpenWeb()

$splist = $spweb.Lists[“NameofList”]

$items=$splist.get_items() | where { $_.Title -like ‘*’ }
$items | % { $_.Delete() }

$spweb.Dispose()

$spsite.Dispose()

 

no symbols have been loaded in the document

Fixed.

Windows 7, Visual Studio 2010, SharePoint 2010 installed on desktop.

In Visual Studio 2010 I was not able to set a breakpoint and was shown the error no symbols have been loaded in the document.

?Fix?

Created a new project, but this time I selected to sandbox option.  Even when a breakpoint was set I would still see the error.  Set a breakpoint on my code, clicked Start Debugging (green button), and sure as SH1T, the code stopped on my breakpoint.

failed to create the configuration database

Trying to install SharePoint 2010 on Window 7 and ran into this error.
First, I was using this script to install SharePoint 2010 on my Win7 box.

http://gallery.technet.microsoft.com/scriptcenter/a88cad83-f595-4487-940e-f678ce47eb5f

(make sure you take a look at the Discussions tab.  Ran into a few issues but quickly found solutions on there.

After this, I then hit the failed to create the configuration database error when trying to run the wizard.

Onto another site….

This site outlined all the prerequisiutes needed for a SharePoint 2010 install.

http://blogs.msdn.com/opal/archive/2009/10/25/sharepoint-2010-pre-requisites-download-links.aspx

Not 100% sure what I missed, but I download / installed each of these again.

FIXED!!!

Event Handler ItemAdded returns null value

use the following code to get the item from ItemAdded Event.

Public Overrides Sub ItemAdded(ByVal properties As SPItemEventProperties)
Using spsSite As SPSite = New SPSite(properties.WebUrl)
Using spwWeb As SPWeb = spsSite.OpenWeb

Dim iSPl As SPListItem

If spwWeb.GetFile(properties.AfterUrl).Exists Then
iSPl = spwWeb.GetFile(properties.AfterUrl).Item

ElseIf spwWeb.GetFolder(properties.AfterUrl).Exists Then
iSPl = spwWeb.GetFolder(properties.AfterUrl).Item
End If

End Using
End Using
End Sub

You must restore to an empty web site

not following the directions caused me to get stuck on this one.

using SharePoint Designer, backup the site you want.

Site –>Administration –>Backup Web Site –> Select a location and save

create an empty site. (this is where i messed up)

to create an empty site do the following:

File –> Web Site –> General –> Empty Web Site –> Enter a site name

Then you will have a empty site to restore to.

Site –>Administration –>Restore Web Site –> Select your backup file –> Click Advanced  (make sure your new empty site name is there) –> Click OK

 

InfoPath Hyplerlink opens as Read Only

Recently ran into an issue where you place a hyperlink on a InfoPath form, user clicks the link, the item opens as read-only, no Edit Document option.

The fix is to do a regedit to allow for editing of the document.

example

[HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Common\Internet]
“OpenDocumentsReadWriteWhileBrowsing”=dword:00000001

in the Common\Internet folder you would create a new DWORD value OpenDocumentsReadWriteWhileBrowsing and assign it a Value of 1.

http://support.microsoft.com/kb/870853

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Trying to use an Event Handler in SharePoint 2007 my test user was seeing this error:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

The error was being triggered by an ItemUpdated or ItemAdded event.

fix:

Public Overrides Sub ItemUpdated(ByVal properties As SPItemEventProperties)
SPSecurity.RunWithElevatedPrivileges(AddressOf mCode)
End Sub

Private Sub mCode()
Dim eFire As hEventFiring = New hEventFiring
Using spsSite As SPSite = New SPSite(sPi.WebUrl)
Using spwWeb As SPWeb = spsSite.OpenWeb

Dim iSPl As SPListItem = spwWeb.Lists(sPi.ListId).GetItemById(sPi.ListItem.ID)

Try

eFire.dEventFire()
Dim roleAssig As New SPRoleAssignment(spwWeb.SiteGroups(mGroup))
roleAssig.RoleDefinitionBindings.Add(spwWeb.RoleDefinitions(“Contribute”))

If iSPl.HasUniqueRoleAssignments = False Then
iSPl.BreakRoleInheritance(True)
End If

For Each spra As SPRoleAssignment In iSPl.RoleAssignments
spra.RoleDefinitionBindings.RemoveAll()
spra.Update()
Next

iSPl.RoleAssignments.Add(roleAssig)
iSPl(“CommentField”) = mGroup
iSPl.Update()
eFire.eEventFire()

Catch ex As Exception
iSPl(“CommentField”) = ex.Message.ToString
iSPl.Update()
Finally
End Try
End Using
End Using
End Sub

Class hEventFiring
Inherits SPItemEventReceiver

Public Sub dEventFire()
Me.DisableEventFiring()
End Sub

Public Sub eEventFire()
Me.EnableEventFiring()
End Sub

End Class

————

What I’m doing:

1. Use RunWithElevatedPrivileges to call the code that updates the item.

2. Disable event firing with eFire.dEventFire

3. Break role inheritance using BreakRoleInheritance

4. Assign permissions using iSPl.RoleAssignments.Add(roleAssig)

5. Enable event firing eFire.eEventFire.

The key to this working is Dim iSPl As SPListItem = spwWeb.Lists(sPi.ListId).GetItemById(sPi.ListItem.ID)