TwitterLinkedInBlog

Thursday, February 26, 2015

PowerShell - Find Content Type in Use

$siteURL = http://sample/
$contentType = "Sample"
$web = Get-SPWeb $siteURL
$ct = $web.ContentTypes[$contentType]
if ($ct) {
$ctusage = [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($ct)
      foreach ($ctuse in $ctusage) {
        $list = $web.GetList($ctuse.Url)
        $contentTypeCollection = $list.ContentTypes;
        #$contentTypeCollection.Delete($contentTypeCollection[$ContentType].Id);
        Write-host "(Would have) Deleted -" $contentType "- content type from -" $ctuse.Url "-"
        }
#$ct.Delete()
Write-host "(Would have) Deleted -" $contentType "- from site."
} else { Write-host "Nothing to delete." }
$web.Dispose()


Reference:
http://stackoverflow.com/questions/6427315/unable-to-delete-sharepoint-2010-contenttype-contenty-type-in-use

Tuesday, February 24, 2015

PowerShell - Merge-SPLogFile

$logPath = "C:\MergedULSLogs\log.txt"
$startTime = "02/23/2015 07:50"
$endTime = "02/23/2015 07:59"
$correlationID = "a9afa74b-119f-46bf-aea8-ec2d886ee490"

Merge-SPLogFile -Path $logPath -Overwrite -StartTime $startTime -EndTime $endTime -Correlation $correlationID

Then open the log.txt file with ULS Viewer.

Monday, February 23, 2015

PowerShell - Delete Duplicate Quick Launch Nodes

Scenario:
  • Site slow to load, resulting in an exception with a correlation ID
  • ULS log error: PortalSiteMapProvider was unable to fetch children for node at URL
  • Temporarily hide the quick launch so users can navigate to the site: SharePoint Designer 2010 > http://samplesite > Left Nav > 'Sample Site Name' > uncheck 'Display Quick Launch' > Save.
  • Find the offending Quick Launch node by running the following SQL query against the content database. You should see many, many repeating entries: SELECT * FROM NavNodes
  • Delete the repeats from the Quick Launch via PowerShell:
    $FindString = “Offending Sample Title”
    $web = get-spweb ("http://samplesite/")
    for ($i = $web.Navigation.QuickLaunch.Count-1; $i -ge 0; $i--)
    {
    write-host $web.Navigation.QuickLaunch[$i].Title "heading deleting...... "
    $web.Navigation.QuickLaunch[$i].Delete()
    write-host "heading deleted successfully !!!."
    }
  • Display the quick launch again.

Resources: