Tuesday, September 14, 2010

Using GeSHi to syntax higligh

I just found this great tool to create syntax highligt in Windows Live Writer.

The tool is call GeSHi (Generic Syntax Highlighter) and has the following goals:

  1. Support for a wide range of popular languages.
  2. Easy to add a new language for highlighting.
  3. Highly customisable output formats.

For more infomation:

image

Managing Custom Areas and Categories with Powershell

The patterns & practices SharePoint Guidance team has posted this article on how to manag custom areas and categories i SharePoint 2010 http://msdn.microsoft.com/en-us/library/ff798462.aspx. But all the examples is written in C# (and that’s a good thing Smile) but I don’t want to have to use a feature receiver add my custom areas and categories, so I have create this demo PowerShell script, strongly inspire by the Microsoft article.

  1. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Practices.ServiceLocation") | Out-Null
  2. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Practices.SharePoint.Common") | Out-Null
  3.  
  4. #EventSeverity Description
  5. # None Indicates no event entries are written.
  6. # ErrorCritical Indicates a problem state that needs the immediate attention of an administrator.
  7. # Error Indicates a problem state requiring attention by a site administrator.
  8. # Warning Indicates conditions that are not immediately significant but that may eventually cause failure.
  9. # Information Contains noncritical information provided for the administrator.
  10. # Verbose
  11.  
  12. #TraceSeverity Description
  13. # None Writes no trace information to the trace log file.
  14. # Unexpected Represents an unexpected code path and actions that should be monitored.
  15. # Monitorable Represents an unusual code path and actions that should be monitored.
  16. # High Writes high-level detail to the trace log file.
  17. # Medium Writes medium-level detail to the trace log file.
  18. # Verbose Writes low-level detail to the trace log file.
  19.  
  20. #Create a List<DiagnosticsArea> to collect the Diagnostics Area
  21. $areaCollection = New-Object 'System.Collections.Generic.List[Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsArea]'
  22.  
  23. #Add a DiagnosticsArea with the name Custom Area
  24. $customArea = New-Object 'Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsArea' -ArgumentList "Custom Area"
  25. #Add a DiagnosticsCategory, to the Custom Areas DiagnosticsCategories collection, with the name Business logic, EventSeverity set to Warning and TraceSeverity to Medium
  26. $customArea.DiagnosticsCategories.Add((New-Object 'Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsCategory'("Business logic", ([Enum]::Parse([Microsoft.SharePoint.Administration.EventSeverity], 'Warning')), ([Enum]::Parse([Microsoft.SharePoint.Administration.TraceSeverity], 'Medium')))))
  27. #Add a DiagnosticsCategory, to the Custom Areas DiagnosticsCategories collection, with the name Data layer, EventSeverity set to Error and TraceSeverity to Medium
  28. $customArea.DiagnosticsCategories.Add((New-Object 'Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsCategory'("Data layer", ([Enum]::Parse([Microsoft.SharePoint.Administration.EventSeverity], 'Error')), ([Enum]::Parse([Microsoft.SharePoint.Administration.TraceSeverity], 'Medium')))))
  29. #Add the Custom Area to the List<DiagnosticsArea> collection
  30. $areaCollection.Add($customArea)
  31.  
  32. #Create a new DiagnosticsArea witht the name Test Area and adds it to the List<DiagnosticsArea> collection
  33. $areaCollection.Add((New-Object 'Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsArea' -ArgumentList "Test Area"))
  34.  
  35. #Get a IConfigManager from the SharePointServiceLocator
  36. $configManager =[Microsoft.Practices.SharePoint.Common.ServiceLocation.SharePointServiceLocator]::GetCurrent().GetInstance([Microsoft.Practices.SharePoint.Common.Configuration.IConfigManager])
  37.  
  38. #Create a new DiagnosticsAreaCollection with the IConfigManager as parameter
  39. $configuredAreas = New-Object 'Microsoft.Practices.SharePoint.Common.Logging.DiagnosticsAreaCollection' $configManager
  40.  
  41. #loop through the newly collection of diagnosticsareas
  42. $areaCollection | % {
  43.  
  44. #get the instance from the current configuration
  45. $existingArea = $configuredAreas[$_.Name]
  46.  
  47. #if the area the is null, we add add the area from the List<DiagnosticsArea> collection
  48. #else we loop through the collection we got from current configuration
  49. if($existingArea -eq $null)
  50. {
  51. $configuredAreas.Add($_)
  52. }
  53. else
  54. {
  55. #loop through the collection we got from current configuration
  56. $customArea.DiagnosticsCategories | % {
  57.  
  58. #get the category instance from the $existingArea (DiagnosticsArea)
  59. $existingCategory = $existingArea.DiagnosticsCategories[$_.Name]
  60. #if the category is null, we add to the current configuration
  61. if($existingCategory -eq $null)
  62. {
  63. $existingArea.DiagnosticsCategories.Add($_);
  64. }
  65. }
  66. }
  67. }
  68. #save the current configuration
  69. $configuredAreas.SaveConfiguration()


The demo code.


Sunday, August 29, 2010

Proud to present the next release of SharePoint Manager

This release includes new features and a bug fix:

  • ServiceProxies
    • ApplicationProxies
  • ReadOnly mode
    • Feature
    • Delete
  • External list error handling
  • Site Subscriptions

Download the new release here: http://spm.codeplex.com/releases/view/51438

Querying for features with PowerShell

On my current project we have a feature with a model that adds some aspx files in a document library. My problem is when I add a new file to the model manifest the new aspx is not now added to the document library, on WSP solution upgrade. The solution for this is to “re-active” the feature with the model. This is easily done, either through the GUI or by PowerShell “Enable-SPFeature –Force”, but the problem is I don’t know where the feature is already activated and I don’t won to look through over all the webs to check if the feature is activated, so I need to query for where the feature is activated. This is done by calling a method called “QueryFeatures” http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfeaturequeryresultcollection.aspx. This method can be found on the SPContentDatabase, SPWebApplication, SPWebService, and the SPSite classes.

In my example I query the SPWebService for two features and then re-active by calling Enable-SPFeature –Force N.B. this don’t disable the feature and there for, if there are a feature receiver attach to the feature, with FeatureDeactivating method, this well not be call.

My PowerShell (Code update 11/9-2010)

$featureGuids = ("41ee3cfa-0863-4c1c-9f6b-41697ff44ee0", "7176542e-ec21-411e-86b3-24abb5251a0b")
$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$featureGuids | % { $contentService.QueryFeatures([Guid]$_) } | % {
    Enable-SPFeature -Identity $_.DefinitionId -Url $_.Parent.Url -Force
}