четверг, 18 марта 2010 г.

Code Generator From Xml Fields Defenition

Hi everyone

I continue write my articles about SharePoint development. Today article will about how to write correct code when you use SharePoint field’s name and ID. I present small online tool right on this page. I think that it help you to increase speed of writing code and help to make one part of code more independent from another parts. So let's get down from word to business.

During creating your SharePoint solution you often create .NET assembly with some classes and SharePoint features included your custom list templates, for example, and each list template contain several fields and content types. Then you have some very useful lists from your custom template. And some part of code from your solution assembly gets access to SPListItem of your custom list. This logic analyze fields installed with custom list template, read it values, update it and so on. So it is very common situation and it’s ok. But every time you get or set the field value you must know field name or field id. In case when you want access to standard SharePoint fields you can use SPBuiltInFieldId class and be happy but if you have custom fields you have several ways for naming fields. And first I want say: in any case don’t write something like this:

Because, if the field name has changed, you must change this name in all the pieces of code. And it is very hard and stupid work. And second I want say: let’s use constants or read only fields. So for this let’s do following actions

1) Create class FieldData that will contain information about your custom field:

2) And create static class container Fields for example that will be contain all you needed FieldData instances:

But now after you add new fields in list template you have to create FieldData instance and add this to the class container. Agrees, it is very tedious work. And I want propose to you small online application that can generate C# code from fields xml definition. This tool based on the xslt transformation that browsers are support. I test it in IE 8 and Firefox 3.6 but I think in early version of Firefox my tool will be work fine too. Just paste Field xml definition or FieldRef xml definition to the following text area:

My small application based on jQuery XSLT Plugin. Thanks authors for this usefully plugin.

Thanks for reading.

Комментариев нет:

Отправить комментарий