Saturday, July 31, 2010

A bit more about custom field types and XSL

The following are the attributes you can use in XSL to identify the field you want to change (or access the value of):

  • Name (example: "First_x0020_Column" - the internal name of the column - not the field type!)
  • Type (example: "Text" - the base type of the field type)
  • FieldType (example: "MyCustomFieldType" - the type name of the custom field)
  • DisplayName (example: "First Column" - the title of the column
  • ID (example: "1b858cea-4306-4cf9-91e8-8bb8674dcdf4" - the GUID for the current column

Applying a XSL stylesheet to a custom field

In the MSDN walkthrough and examples on using XSL to create a custom rendering style for a custom field type, the sample XSL all use the field's name as the reference. This is a bit silly - since it means the users need to create the new fields with the exact same name.

While the option to create a custom rendering based on the field name is welcome and will be very useful (if for example I am deploying a column to a farm and I want it to have a unique rendering template), the articles do not explain how to create a XSL rendering template for a field type - regardless of what the fields created from that type are called.

The solution is simple. Lets say your field type is "MyFieldType", then instead of the following line that uses the title of an instance of the field ("my field type"): <xsl:template match="FieldRef[@Name = 'My Field Type']" mode="Text_body"> use the following line instead, which uses the type name of the field: <xsl:template match="FieldRef[@FieldType = 'MyFieldType']" mode="Text_body">

Monday, July 26, 2010

5 Days SharePoint Development Training - Canberra, 16th August, 2010

Are you in Canberra on the week of the 16th, and is just itching for some sharepoint 2010 developer training? look no further!
I have teamed up with my friends from Synergy to deliver the first ever Synergy SharePoint 2010 Development course. For more details on the content of the course and how to register, head to Dimension Data's web site!

Tuesday, July 13, 2010

New look for the blog

Do you like it? Blogger has some new templates, and I figured its time for a change.

Update: following some comments, I reverted to white background again - and removed the width restriction that pissed me off. Is this better?

Monday, July 05, 2010

"The local device name is already in use" error when using AddFieldAsXml

I had the following exception thrown at me today when trying to add a site column using AddFieldAsXml method: "The local device name is already in use".
The reason was that my XML contained a field ID - which is what the XML for a field is supposed to have when it is used as an element file in a feature, but not when you are adding the field using code. So - beware!

Thursday, July 01, 2010

Read my book on Rough Cuts

Do you want a preview of my upcoming book - the 'SharePoint 2010 How To'? simply go to Safari books online and read away! There is also a purchase option if you like what you see...

Sunday, June 20, 2010

SharePoint Conference Australia Presentation Notes

Thanks to everyone who came to see me and Brian present in the Australian SharePoint Conference last week!.
To answer the question that we got during the demo, the silverlight web part project template can be downloaded from this site: http://code.msdn.microsoft.com/vsixforsp.
Also, to see the code for Brian's twitter map web part, see his blog post on the subject: http://blog.brianfarnhill.com/2010/05/21/twitterbing-maps-web-part-how-i-did-it/.
Finally - for everyone who asked for the slide deck, it is now available from my company's (Extelligent Design) web site: http://www.extelligentdesign.com/in-the-news/sharepointconferenceaustraliapresentationnowavailable where you can also purchase KWizCom products (for australian\new Zealand customers only) or contact me about sharepoint training or consulting.

Tuesday, June 01, 2010

Validating Web Part Properties

Nothing is more annoying than configuring a web part by changing its properties and then hitting ok only to see the web part display an error that a property is invalid - and having to open the properties pane again to fix the problem.
To avoid this, best practice is to validate the data entered in the "set" of the web part property. For example, if I have a web part property that needs a comma delimited array of numbers (for example 1,2,3,4) and I don't want to build a tool part just for that, I can still build a property like this:

public string NumberArray
{
    get{return _numberArray;}
    set{_numberArray=value;}
}
The problem with the code above is that it is not validating that the string entered is indeed an array of numbers. To do that, I could change the code to something more like this:
public string NumberArray
{
    get{return _numberArray;}
    set{
          string [] arr = value.split(',');
          foreach (string item in arr)
          {
             int i;
             if(!int.TryParse(item,out i))
                throw new Exception("The item \""+item+"\" is not a valid number");
          }

_numberArray=value;}
}
This will do what I want - preven the user from closing the web part properties pane before fixing the error in the property, but it will not display the nice informative error to the user. Instead, it will show a generic error "An error has occurred" despite the fact that I specified what the error was when I threw the error!
Why? because to do that the exception must be of type WebPartPageUserException.
So the correct code for validating my sample property would be:
public string NumberArray
{
    get{return _numberArray;}
    set{
          string [] arr = value.split(',');
          foreach (string item in arr)
          {
             int i;
             if(!int.TryParse(item,out i))
                throw new WebPartPageUserException("The item \""+item+"\" is not a valid number");
          }

_numberArray=value;}
}

Now - when the users put an invalid value in my property they will be notified that it is invalid, and which value it was.

Sunday, May 30, 2010

Assembly stuck in memory despite being replaced

I had a wonderful time this weekend trying to find out why is it that when I deployed a new version of my solution - one that included more debugging information than the previous one, nothing would come out - it would still print out the old data.
I went as far as to remove the solution entirely, make sure the GAC is clear from the assembly and then manually drag and drop my new assembly (thinking it is my WSP that is stuck with an old assembly) and still I get the old code running instead of the new one.
What didnt I do? I tried IISreset and all sort of tricks, but I could not reboot since it was not my server (partner company asking me to help out with some dev work on their server).
It turns out that if I could reboot it would have saved me all the time I spent - but then I would not have figured out the reason that was happening!

Curious? Well, here it is - I had a windows application running that used sharepoint object model to triger the timer job I was trying to redeploy. The windows application did not have a reference to the timer job code - it just used the OM to query the server what timer jobs are there and then let me choose one and execute it. So I never suspected it was the culprit - especsially since I used it to confirm the timer job was deleted after I removed the old solution.
But it was.
For some reason, just having it running, connected to sharepoint objects, somehow caused the old assembly to remain in memory\cache (I will not pretend to know why or how) and once I closed it I suddenly got the new code running!

There - if I saved someone the same frantic research I was doing this weekend, I am a happy man.

Wednesday, May 19, 2010

Last chance to register to the Australian SharePoint Conference

It is next week in Sydney! didn't you hear? I will be there, speaking about web parts and sharepoint 2010 with my friend Brian Farnhil. Dare you miss it? it is Australia's biggest SharePoint event of the year after all...

There will be:

  • 20 sessions of end user/power user/business content including some great case studies including Telstra, Volvo and a number of Industry verticals such as finance, insurance, legal and engineering
  • 20 sessions of pure technical content for IT Pro, Administrators, Developers and Analysts
  • Content is relevant to those using WSS, MOSS 2007, and new 2010 products

Special offer!
Come see me during the conference and get a 10% discount on all products by KWizCom (offer only valid for Australian and New Zealand customers).