Montag, 7. Mai 2012

NullReferenceException when deploying Sharepoint feature from Visual Studio...

... but not when you deploy it from the Sharepoint web interface. Its likely that you use SPContext somewhere. Instead you should use the SPFeatureReceiverProperties exclusively to obtain the current site or web.

Mittwoch, 11. April 2012

Items not showing in Sharepoint despite read permissions.

I was desperatly looking why some one else who had only Read permissions couldnt see certain items in the Style Library. As it turns out when the content approval is turned on then the items have to be published even after checking them in to make them available for everyone.  Its a smart feature so that you can review minor versions without everyone seeing drafts, but on this occassion I turned it off.

Mittwoch, 8. Februar 2012

ShowInPicker in BCS Model is ignored

Rather than trying to hide TypeDescriptors you need to only apply  ShowInPicker="true" to the ones you want to show. If none of the TypeDescriptors has ShowInPicker=true then all TypeDescriptor are displayed, even if some have ShowInPicker=false

Mittwoch, 21. Dezember 2011

Fixed ULS Viewer

On  MSDN Archive there is a great tool for viewing ULS files from Sharepoint but it was only working reliably on US machines because of date time issue. So far the problem has never been fixed although its only two lines of code causing the problem. 

The issue is that the program uses DateTime.Parse with the current UI culture when parsing Sharepoint Log entries but Sharepoint always uses the US date format for the time column. The source is not available and the exe is hosted on the archive, thus I have decided to write a small patcher. The patch program uses Cecil to change DateTime.Parse with current UI culture to DateTime.Parse with US settings. Cecil is really a very cool tool to use.

  1. Unpack the ZIP into the same directory as the ULS Viewer
  2. Run the patcher
  3. Now you will have a Fixed ULS Viewer which works under any locale and any day of the month. On german settings you could use the original only up to the 12th of each month :)
Download from:

Freitag, 25. November 2011

A brief post about async postbacks from javascript

Just in case you want to cause an async postback from javascript its easiest to use  ClientScriptManager.GetPostbackRef with client option set and put an invisible link button somewhere.

Mittwoch, 23. November 2011

Programmatically populating a BDC field in Sharepoint 2010

In this post I try to explain how to retrieve and store data in a external lookup column or business data field. For the code snippets I use PowerShell instead of C# because I can test my claims as I write.

Lets set up some pseudo vars


$listItem["bdcColName"] holds the value of the BDC field which was specified when setting up the column, which can be set in SPBusinessDataField.BdcFieldName. The data is always stored as a string.
The BCS Identity of the item is stored in $listItem[$bdcField.RelatedField]. 
(Still need to check if this is EntityInstanceReference or Identity
Check BCSFieldEditor and ItemPicker with .NET Reflector )

If you want to deserialize the EntityInstanceReference you need to get an instance to the matching IMetadataCatalog :
$bdcService=(Get-SPService).services|?{$_.typename -eq "Business Data Connectivity Service"}
$bdcCat=$bdcService.GetDatabaseBackedMetadataCatalog((Get-SPServiceContext http://yourdevsite))

For each additional external column which is stored with this field Sharepoint creates a field in the list. The additional fields names can be obtained with $bdcField.GetSecondaryFieldsNames(). To get a reference to the field you can use the display name which is in the  format "$($bdcField.Title): $secondaryFieldName"

Sharepoint doesnt set the secondary lookup values itself when you save the entry, so you have to retrieve all values for the secondary field names. 

Dienstag, 18. Oktober 2011

Programmatically change Sharepoint list forms

A very common request for a sharepoint developer is to customize the form of a list. With Sharpeoint Designer this is really easy to do but this has the downside that a) designer forms with Sharepoint Designer is harder than it looks and b) it comes with all the downsides of customization vs definition such as not having a deployable solution. Another option is to use InfoPath Designer but this is not cheap because you need Office Pro and the enterprise version of Sharepoint.
The same in object model, please

the same as SPList.Forms
Another, very attractive option is to use ASP.NET markup and code to define the design and functionality of a form.

Enumeration of Sharepoint list forms can be done via SPList.Forms. Using this you can find out which forms are registered for the list, which type they have (edit, new, display) and which one is the default form is several of one type exist.   

Since SPList.Forms is a read only collection you have to use another way of changing the forms. When a list is created, each form is provisioned as a web part page in the list itself. These pages are either stored in the hidden Forms folder of document libraries or in the RootFolder of standard lists and can be accessed as normal SPFile objects.

Each of these web part pages contains a WebPartManager with one ListFormWebPartUsing the TemplateName property you can specify a custom rendering template and thus create a completely individual form body while maintaining all the forms standard setup, such as buttons and toolbar.

To customize the form content whilst retaining the default setup of form page you can specify a custom rendering template for the ListFormWebPart. The rendering template is an ASCX file in TEMPLATES\CONTROLTEMPLATES and contains a <sharepoint:renderingtemplate id="[Your template name]"> tag. 

If you can assign a content type to the list you have a much easier life because you can use the NewFormTemplateName and EditFormTemplateName of the content type object. However its not always possible to assign content types, e.g. for external lists. Especially for these I find it very useful to be able to deploy a custom form.