Archive

Archive for February, 2013

Find and Replace Content Types in SharePoint 2010 using PowerShell

February 25, 2013 7 comments

If you ever needed to replace a specific content type in your SharePoint 2010 site but you do not want to go through every single document library manually to remove and assign a new content type, you can accomplish this by turning to PowerShell.

Below is the script I used to iterate through a site collection and sub-sites, check the content type of each document library and if it was a specific content type , removes it and replaces it with the correct content type.

Copy the script below, change the parameters in “Red” to reflect your environment, save the file on your SharePoint Server.

1.  Log into your SharePoint (CA) Server
2.  Bring up SharePoint Management Shell (PowerShell) with Admin priviliages
3.  Browse to the location you saved the script
4.  Exectue script (.\nameofscript.ps1)

================================================================================

#Get Site Collection
$site = Get-SPSite http://portalsite
 
#Create CSV header
“siteURL `t” + “listName `t” + “listURL `t” + “ContentType `t” + >> Content-Types.csv

#Get Site Collection Webs
$webs = $sites.AllWebs

#Loop through each web in Site Collection
foreach($web in $webs)
{

Write-Host “Checking Sites: ” $web.Title

#Loop through all Site List Content Types
foreach ($ctype in $lst.ContentTypes)
{
#If Content Type equals a specific content type
if ($ctype.Name -eq “NAME OF CONTENT TYPE YOU WANT TO REMOVE“)
{

Write-Host “Content type Document exists on: ” $lst.Title
$web.Url + “`t” + $lst.title + “`t” + $lst.DefaultViewUrl + “`t” + $ctype.Name >> Content-Types.csv

#if ContentTypes Setting is not enabled on the Site, enable them
if ($lst.ContentTypesEnabled -eq $false)
{

#Enable ContentTypes on the site level
$lst.ContentTypesEnabled = $true
$lst.Update()

}

#Remove contentTypeNAME OF CONTENT TYPE YOU WANT TO REMOVEfrom list

Write-host “Removing Content Types ” $ctype.Name  “from ” $lst.Title
$ctToRemove = $lst.ContentTypes[“NAME OF CONTENT TYPE YOU WANT TO REMOVE“]
$lst.ContentTypes.Delete($ctToRemove.Id)
$lst.Update()

#Add Site contentTypeNAME OF CONTENT TYPE YOU WANT TO ADDto list

Wrtie-Host “Adding Content Type NAME OF CONTENT TYPE YOU WANT TO ADD” $lst.Title
$ctToAdd = $sites.RootWeb.ContentTypes[“NAME OF CONTENT TYPE YOU WANT TO ADD“]
$lst.ContentTypes.Add($ctToAdd)
$lst.Update()
}

}

}
$site.Dispose()

Fixing Unable to Open Document Library in Explorer View in SharePoint 2007

February 21, 2013 Leave a comment

I ran into an issue recently that prevented me from opening up any Document Library in Explorer View in my SharePoint 2007 Mysites Web Application.  It worked fine in my main SharePoint Web Application, but not in MySites.  I did not get any errors, but when I tried to open up the document library in explorer view  Actions -> Open with Windows Explorer it did  nothing for me, the normal behavior of a separate Explorer Window popping up, never came up.  Changing the Document Library view to “Explorer View” gave me an IE message:  “Navigation to webpage was cancelled”.

Knowing that the URL of the Explorer View points to the WebFldr.aspx page I tried to access it by going directly to that URL (http://portalsite/name of document library/Forms/WebFldr.aspx).  I received the same IE message above.

I then took a look at the Document Library Explorer View settings. (Settings, Document Library Settings, Views, Explorer View).

I noticed that URL of the view was pointing to a non SSL protocol.  Knowing that my MySites uses the HTTPS protocol I immediately knew something was wrong with the http routing.

Since my SharePoint 2007 environment uses Microsoft Internet Security and Acceleration Server 2006 (ISA) to off load our SSL Certs I made sure the External and Internal ISA rules were set correctly to properly redirect HTTP calls to HTTPS.  This was set up correctly in ISA.

I then checked the Alternate Access Mappings (AAM) settings within Central Administration.

BINGO!

For the MySites web application I noticed there was a Custom zone AAM set with an Internal URL with HTTP protocol, and a Public URL for Zone set to use HTTP as well.

I removed this custom AAM and recreated it as follows.

Internal URL:  http://mysite
Zone: Default
Public URL for Zone:  https://mysite

Once I modified the AAM for MySites I was then able to open up the document library in Explorer View with no problems.

If you experience this same behavior, please check your AAMs and make sure your Internal and Public URL’s are set correctly and are in the correct zones.

Find All WebParts Being Utilized (Open/Closed) on Sites in SharePoint 2010 using PowerShell

February 21, 2013 Leave a comment

I recently had a tasking where I needed to provide a listing of all webparts of a 3rd party solution (KWizCom Remote List Viewer) that were being utilized in my SharePoint 2010 Environment whether they were opened or closed on each Site Collection/Site in my Web Application.

Thanks to Ghazwan he already created a very nice script that does this for us already- http://sharepointquester.com/2012/09/11/list-all-web-parts-in-a-sharepoint-2010-site-collection-using-windows-powershell/

However I needed to do a few extra things, so I basically borrowed from this script and modify it to met my demands.

Below is the final script I used to iterate through my Web Application, check for a specific webpart type (KWizCom Remote List Viewer) and output to a .csv file.

==============================================================


function enumerateWebParts($Url)
{
$webApp = Get-SPWebApplication $Url  #Get WebApplication URL

foreach($web in $webApp | Get-SPSite -Limit All | Get-SPWeb -Limit All) #foreach loop to iterate through all sites and webs of WebApplication
{
if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web)) #Check if site is a publishing site
{
$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)
$pages = $pWeb.PagesList
foreach ($item in $pages.Items)
{
$fileUrl = $webUrl + “/” + $item.File.Url
$manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
$wps = $manager.webparts
foreach ($type in $wps) #loop through each type found $wps
{
if ($type -like "*KWizCom*") #if webpart $type -like KWizCom select webpart object
{
$wps | select-object @{Expression={$pWeb.Url};Label=”Web URL”},@{Expression={$fileUrl};Label=”Page URL”},DisplayTitle, IsVisible, @{Expression={$type};Label=”Type”}
}
}
}
}
else #if web is not a publishing site
{
$pages = $null
$pages = $web.Lists["Site Pages"]
if ($pages)
{
foreach ($item in $pages.Items)
{
$fileUrl = $webUrl + “/” + $item.File.Url
$manager = $item.file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
$wps = $manager.webparts
foreach ($type in $wps)
{
if ($type -like "*KWizCom*")
{
$wps | select-object @{Expression={$Web.Url};Label=”Web URL”},@{Expression={$fileUrl};Label=”Page URL”},DisplayTitle, IsVisible, @{Expression={$type};Label=”Type”}
}
}
}
}
else
{
}
}
Write-Host “… completed processing” $web
}
}
$row = enumerateWebParts(‘http://WebApplication’) #call the enumerateWebParts function pass it the URL for the web application
$row | export-csv KWizCom_WebParts.csv #export csv