Migrating custom webpart containing Chart control

I got the following error after migrating from SharePoint 2007 a webpart containing chart controls.

System.Web.UI.DataVisualization.Charting.DefaultImageHandler.System.Web.UI.DataVisualization.
Charting.IChartStorageHandler.Save(String key, Byte[] data) +527.

[NullReferenceException: Object reference not set to an instance of an object.] System.Web.UI.DataVisualization.Charting.DefaultImageHandler.System.Web.UI.DataVisualization.Charting.IChartStorageHandler.Save(String key, Byte[] data) +527 System.Web.UI.DataVisualization.Charting.ChartHttpHandler.GetChartImageUrl(MemoryStream stream, String imageExt) +635Basically the Chart control couldn’t access the Session variable. After googling I found this Mark Arend’s blog, it explained that SharePoint 2010 by default disabled ASP.NET Session State. So as advised I run this command in PowerShell

Enable-SPSessionStateService -DefaultProvision

It fixed. I could see my chart. But when one of the chart did postback, I got following different error:

[HttpException (0x80004005): No http handler was found for request type 'POST']
System.Web.HttpApplication.MapIntegratedHttpHandler(HttpContext context, String requestType, VirtualPath path, …) +529
System.Web.HttpServerUtility.Execute(String path, TextWriter writer, Boolean preserveForm) +947

[HttpException (0x80004005): No http handler was found for request type 'POST'] System.Web.HttpApplication.MapIntegratedHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig, Boolean convertNativeStaticFileModule) +529

It is because the http handler configuration for the Chart Control doesn’t have POST in its verb by default. Having added POST in the verb attribute, my chart control work. Below is the handler setting in web.config. It is under httpHandler in the system.web configuration.

<add verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />

But as my farm consists of several web front-ends, I prefer to change the web.config using SharePoint API. In SharePoint 2007 we can use feature to modify web.config but in SharePoint 2010, PowerShell is better tool to do the job. Based on this good post from Scripting Guy, below is PowerShell script to do web.config modification using SPWebConfigModification and this is another post about how to use it.
function Enable-ChartingPostVerb{
param(
 [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
 [Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind]
   $WebApplication
)
begin{
  Enable-SPSessionStateService -DefaultProvision
}
process {     
   $WebApp = $WebApplication.Read()
   # SPWebConfigModification to enable BlobCache
   $configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
   $configMod1.Path = "configuration/system.web/httpHandlers/add[@path=`"ChartImg.axd`"]"
   $configMod1.Name = "verb"
   $configMod1.Owner = "ChartingConfig"
   ## SPWebConfigModificationType.EnsureChildNode -> 0
   ## SPWebConfigModificationType.EnsureAttribute -> 1
   ## SPWebConfigModificationType.EnsureSection -> 2
   $configMod1.Type = 1
   $configMod1.Value = "GET,HEAD,POST"
   
   # Add mods, update, and apply
   $WebApp.WebConfigModifications.Add($configMod1)
   $WebApp.Update()
   $WebApp.Parent.ApplyWebConfigModifications()
  }
}

To run the script, for example if the script file name is ChartConfig.ps1.
- on the Shell you can do (this will load the function) PS1> . ./ChartConfig.ps1
- then you can call the function Enable-ChartingPostVerb or call the function as part of a pipeline.

Microsoft SharePoint 2010,SharePoint Designer, SharePoint Workflow, Web Part

SharePoint 2010 Issue with migrating fields definition

Recently I got several issues when migrating a custom field definition and site column definitions from SharePoint 2007 to SharePoint 2010:

  • Problem: Field type $Resources:resource-name;… is not installed properly. Go to the list settings page to delete this field when trying to access mngfield.aspx or any list form that using the custom field
  • Solution: The field <Field Name=”TypeName”> in the Field definition cannot use resource and it must be the same name as the class name specified in the <Field Name=”FieldTypeClass”>Namespace.ClassName,Assembly</Field>
  • Problem: After fixing this issue the field type in the field definition, I notice that the site column internal name is using Resources:resource-name;… Somehow sharepoint 2010 doesn’t allow internal site column internal name to using resources.
  • Solution: Change the Name &  StaticName attributes values to the non-resource lookup in Elements.xml of site column and content type definitions  as well as those in Field element of Schema.xml for list definition

Conclusion:

It seems that SharePoint 2010 has stricker rule regarding the use of resource lookup for Field Definition and site column definition.

Microsoft SharePoint 2010,SharePoint Designer, SharePoint Workflow, Web Part