Saturday, May 14, 2011

PowerShell to remove web parts with errors from SharePoint 2010

One of the things that I run into whenever I do an upgrade from an older version of SharePoint to SharePoint 2010 is that not all of the custom functionality previously implemented still works. This is especially true when moving from WSS 2.0 to SharePoint 2010.

When a client has 50+ sites and these web parts are on every landing page the process of removing them manually is just out of the questions. So how do we solve this?

Well, in SharePoint 2007 I would have said that you should go bug a developer and have them write you a console application to loop through and remove any and all erroring web parts. However, in SharePoint 2010 the story is slightly different.

PowerShell to the rescue!!!

function Remove-SPErroringWebParts
{
[CmdletBinding()]

param
(
[parameter(ValueFromPipeline=$true,Mandatory=$true,Position=0)]
$Web
)

begin
{
Out-Host -InputObject "Beginning Remove-SPErroringWebParts..."
}

process
{
if ($Web -is [string]) {Out-Host -InputObject " This is a string.";$Web = Get-SPWeb $Web}
if ($Web -is [Microsoft.SharePoint.Administration.SPWebApplication]) {Out-Host -InputObject " This is a web app.";$Web = Get-SPSite -WebApplication $Web -Limit All -Confirm:$false}
if ($Web -is [Microsoft.SharePoint.SPSite[]]) {Out-Host -InputObject " This is an array of sites.";$Web | %{Set-AllBackToSiteDefinition -Web $_}}
if ($Web -is [Microsoft.SharePoint.SPSite] ) {Out-Host -InputObject " This is a site.";$Web = (Get-SPWeb -Site $Web -Limit All -Confirm:$false)}
if ($Web -is [array]) {" This is an array."; $Web | %{ Remove-SPErroringWebParts $_ } }
if ($Web -is [microsoft.SharePoint.SPWeb])
{
Out-Host -InputObject " This is a web."
$webpartmanager = $Web.GetLimitedWebPartManager(($Web.Url + "/default.aspx"), [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

#Create a separate list of webparts to be deleted to avoid enumeration error
$webpartsToDelete = $webpartmanager.WebParts | ?{$_.ErrorMessage -ne $null}

foreach ($webpart in $webpartsToDelete)
{
if ($webpart.ErrorMessage -ne $null)
{
Out-Host -InputObject " Deleting webpart.."
$webpartmanager.DeleteWebPart($webpart)
}
}
$Web.Dispose()
}
}

end
{
Out-Host -InputObject "Finished Remove-SPErroringWebParts."
}
}



The function above looks far more complicated than it really is. I wrote it so that I could save it off and use it for later projects. To be honest, this function isn’t really done as I need to add a great deal of error handling, it only checks the “/default.aspx” page so if you have Publishing sites or even sites that use the Wiki Home Page it will not work, and there is some Out-Host commands that will most likely be changed to Out-Debug. However, I digress. Writing PowerShell for reusability is a completely different discussion that I will probably write about at a later date. Until then there are a couple really great blog posts you can check out if you are interested:




For now, lets take a look at what a more minimalistic approach would be.




$Web = Get-SPWeb "http://site/web"
$webpartmanager = $Web.GetLimitedWebPartManager(($Web.Url + "/default.aspx"), [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$webpartsToDelete = $webpartmanager.WebParts | ?{$_.ErrorMessage -ne $null}
foreach ($webpart in $webpartsToDelete)
{
if ($webpart.ErrorMessage -ne $null)
{
Out-Host -InputObject " Deleting webpart.."
$webpartmanager.DeleteWebPart($webpart)
}
}
$Web.Dispose()



Much less code there but less power as well. This will obviously only work on an SPWeb object and the code would need to be adjusted for any other scope. Let’s take a look at the usage of the original function.

On an SPWeb object:




Get-SPWeb "http://server/site/web" | Remove-SPErroringWebParts


On an SPSite object:


Get-SPSite "http://server/site" | Remove-SPErroringWebParts


On an SPWebApplication object:


Get-SPWebApplication "http://server" | Remove-SPErroringWebParts


Or on an string:


"http://server/site/web" | Remove-SPErroringWebParts





Either way the function will loop through all of the “/default.aspx” pages on the SPWeb objects of the given scope and remove any web parts that have errors.

I’m sure I don’t have to tell you that this can be just as dangerous as it is helpful if you have web parts that are erroring that you are not aware of. As always use caution and I’m not responsible for anything you break while using anything you find here. Smile


I hope this helps! Any and all feedback is welcomed and appreciated. Let me know what you think!

Friday, May 13, 2011

Sign on as different user to SharePoint 2010 site published through TMG 2010

As of Service Pack 1, TMG now officially supports publishing SharePoint 2010 sites. If you have tried this you may have ran into an issue when trying to use the “Sign in as Different User” functionality. Fortunately Microsoft has released an update that resolves the issue.

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

The update is included in Software Update 1 rollup 3 for TMG SP1. This rollup can be found here:

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

However, just having applied the rollup does not solve the issue.  You will also need to run one of the following scripts on one of your TMG front ends.

For HTML Forms Authentication with Kerberos Constrained Delegation:

Const SE_VPS_GUID = "{143F5698-103B-12D4-FF34-1F34767DEabc}"
Const SE_VPS_NAME = "EnableSharepointSignIn"
Const SE_VPS_VALUE = true

Sub SetValue()

' Create the root object.
Dim root ' The FPCLib.FPC root object
Set root = CreateObject("FPC.Root")

'Declare the other objects that are needed.
Dim array ' An FPCArray object
Dim VendorSets ' An FPCVendorParametersSets collection
Dim VendorSet ' An FPCVendorParametersSet object

' Get references to the array object
' and to the network rules collection.
Set array = root.GetContainingArray
Set VendorSets = array.VendorParametersSets

On Error Resume Next
Set VendorSet = VendorSets.Item( SE_VPS_GUID )

If Err.Number <> 0 Then
Err.Clear

' Add the item.
Set VendorSet = VendorSets.Add( SE_VPS_GUID )
CheckError
WScript.Echo "New VendorSet added... " & VendorSet.Name

Else
WScript.Echo "Existing VendorSet found... value- " & VendorSet.Value(SE_VPS_NAME)
End If

if VendorSet.Value(SE_VPS_NAME) <> SE_VPS_VALUE Then

Err.Clear
VendorSet.Value(SE_VPS_NAME) = SE_VPS_VALUE

If Err.Number <> 0 Then
CheckError
Else
VendorSets.Save false, true
CheckError

If Err.Number = 0 Then
WScript.Echo "Done with " & SE_VPS_NAME & ", saved!"
End If
End If
Else
WScript.Echo "Done with " & SE_VPS_NAME & ", no change!"
End If

End Sub

Sub CheckError()

If Err.Number <> 0 Then
WScript.Echo "An error occurred: 0x" & Hex(Err.Number) & " " & Err.Description
Err.Clear
End If

End Sub

SetValue





For Integrated Authentication with Kerberos Constrained Delegation: You will need to run the script above as well as the script below.




Const SE_VPS_GUID = "{143F5698-103B-12D4-FF34-1F34767DEabc}"
Const SE_VPS_NAME = "UseOnlyNTLMForWindowsAuth"
Const SE_VPS_VALUE = 1

Sub SetValue()

' Create the root object.
Dim root ' The FPCLib.FPC root object
Set root = CreateObject("FPC.Root")

'Declare the other objects that are needed.
Dim array ' An FPCArray object
Dim VendorSets ' An FPCVendorParametersSets collection
Dim VendorSet ' An FPCVendorParametersSet object

' Get references to the array object
' and to the network rules collection.
Set array = root.GetContainingArray
Set VendorSets = array.VendorParametersSets

On Error Resume Next
Set VendorSet = VendorSets.Item( SE_VPS_GUID )

If Err.Number <> 0 Then
Err.Clear

' Add the item.
Set VendorSet = VendorSets.Add( SE_VPS_GUID )
CheckError
WScript.Echo "New VendorSet added... " & VendorSet.Name

Else
WScript.Echo "Existing VendorSet found... value- " & VendorSet.Value(SE_VPS_NAME)
End If

if VendorSet.Value(SE_VPS_NAME) <> SE_VPS_VALUE Then

Err.Clear
VendorSet.Value(SE_VPS_NAME) = SE_VPS_VALUE

If Err.Number <> 0 Then
CheckError
Else
VendorSets.Save false, true
CheckError

If Err.Number = 0 Then
WScript.Echo "Done with " & SE_VPS_NAME & ", saved!"
End If
End If
Else
WScript.Echo "Done with " & SE_VPS_NAME & ", no change!"
End If

End Sub

Sub CheckError()

If Err.Number <> 0 Then
WScript.Echo "An error occurred: 0x" & Hex(Err.Number) & " " & Err.Description
Err.Clear
End If

End Sub

SetValue





Once this is complete you should be able to Log in as a Different User to your hearts content.



I hope this helps!

Thursday, October 29, 2009

WSS and MOSS October CU Released

The October cumulative update for WSS 3.0 and MOSS 2007 has been released yesterday here are the KB article links for these fixes:

The fixes can be downloaded using the following links:

Monday, October 19, 2009

SharePoint 2010 – And the Downloadable Content keeps coming!!!

The content keeps trickling in. Microsoft has been releasing posters all day, (http://ctrulove.blogspot.com/2009/10/sharepoint-2010-posters-for-everyone.html) but wait, there’s more!

More POSTERS!!!!

SharePoint 2010- SharePoint Developer Platform Wall Poster - The SharePoint 2010 Developer Platform wall poster (PDF format) shows a view of the SharePoint 2010 developer tools, community ecosystem, execution environment, SharePoint Server 2010 workloads, and target application types. The poster is intended to be printed at 24 inches x 36 inches (61 centimeters x 91 centimeters).

SharePoint 2010- Developer Platform White Paper - This white paper provides an overview of the SharePoint 2010 Developer Platform for ASP.NET developers.

Enterprise Search Planning for SharePoint Server 2010 - This model describes primary architecture design decisions for search environments.

Design Search for SharePoint Server 2010 - This model describes the steps to determine a basic design for a SharePoint Server 2010 search architecture.

SharePoint Server 2010 Search Architecture - This model describes the physical and logical architecture components of the search system.

 

Learning and Evaluation Stuff Too!!!

SharePoint 2010- Getting Started with Development on SharePoint 2010- Hands on Labs in C# and Visual Basic - Use these 10 hands-on lab manuals for SharePoint 2010 to get started learning SharePoint 2010 development.

SharePoint 2010- Developer and IT Professional Learning Plan - This document provides information to help developers and IT professionals learn Microsoft SharePoint 2010.

SharePoint 2010- Professional Developer Evaluation Guide and Walkthroughs - The SharePoint 2010 developer evaluation guide describes the SharePoint 2010 developer platform, including walkthroughs of some of the new capabilities for developers.

SharePoint Server 2010 Evaluation Guide - Introduction and overview of SharePoint Server 2010 for IT pros.

I am sure there is more to come. Enjoy!

SharePoint 2010 and PowerShell

Great reference posted by Dmitry Sotnikov. 467 cmdlets!!!

http://dmitrysotnikov.wordpress.com/2009/10/19/sharepoint-2010-cmdlet-reference/

SharePoint 2010 Posters for Everyone!

Microsoft has apparently decided that posters are the way to go. In tandem with the start of the SharePoint Conference Keynote starting a deluge of SharePoint 2010 posters were released to http://download.microsoft.com. Below you can find them all for your viewing pleasure:

Services in SharePoint Products 2010 - Describes and illustrates the services architecture, including and common ways to deploy services in your overall solution design.

Hosting Environments for SharePoint 2010 Products - Summarizes the support for hosting environments and illustrates common hosting architectures.

Business Connectivity Services poster - Microsoft Business Connectivity Services enable users to interact with external data by using SharePoint lists and Microsoft Office 2010.

Topologies for SharePoint Server 2010 - Describes common ways to build and scale farm topologies, including planning which servers to start services on.

Cross-farm Services in SharePoint 2010 Products - Illustrates how to deploy services across farms to provide centralized administration of services.

SharePoint Enterprise Search - Compares and contrasts search technologies in SharePoint 2010 Products

Upgrade planning poster - Describes requirements and considerations for planning to upgrade to SharePoint Server 2010

Upgrade approaches poster - This model describes the three basic approaches to upgrading to SharePoint Server 2010: in-place, database attach, or a hybrid of the two.

Upgrade testing poster - To help ensure a smooth transition to SharePoint Server 2010, perform a trial upgrade to find issues likely to surface during the actual process.

Upgrading services poster - You need to give special consideration to the issues involved when you upgrade services from the previous version of SharePoint Server

Getting started with BI in SharePoint Server 2010 - Discusses the business intelligence tools available in SharePoint Server 2010

Enjoy!

SharePoint 2010 Upgrade Posters Now Available!

Just hours before the SharePoint Conference in Vegas, new content is already arriving out on http://download.microsoft.com. You can now get 4 posters that provide a lot of insight into the 2007 to 2010 upgrade process. And here they are:

Upgrade approaches poster - This model describes the three basic approaches to upgrading to SharePoint Server 2010: in-place, database attach, or a hybrid of the two.

Upgrading services poster - You need to give special consideration to the issues involved when you upgrade services from the previous version of SharePoint Server.

Upgrade planning poster - Describes requirements and considerations for planning to upgrade to SharePoint Server 2010.

Upgrade testing poster - To help ensure a smooth transition to SharePoint Server 2010, perform a trial upgrade to find issues likely to surface during the actual process.

Enjoy!