Sunday, April 27, 2008

Intellisense in onet.xml

Intellisense in onet.xml

This post is the first of several in which I will address issues that are asked during projects on which I work. Of course, to protect confidential information, any names or business related information are changed.


I am a big fan of knowing the nitti gritty details of how a system works. This enables me to piece together an efficient and effective solution that does not contain functionality I or the customer do not need or want.


In SharePoint development, this means wirting, or at least knowing how to write, a site defintion, and the first step is to work with onet.xml. I do prefer to write my own onet.xml since it gives me greater control, however this can be a tedious task unless you have good tools.


To ease the creation of onet.xml in Visual Studio you should use intellisense. By default, Visual Studio does not intellisense onet.xml, but you can make it do so either on a file-by-file basis or for every file, onet.xml or otherwise, that uses a given namespace.


To get intellisense for a single file, open the file i Visual Studio and go to the properties tab, often located on the bottom right:


In the Schema-field, clikc the three dots to browse for a schema file. Find the wss.xsd-file, usually located in 'C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\XML' and click add. That's it, Visual Studio should now give you intellisense. Test this by placing the cursor at the very beginning of the file and press CTRL+Space. You should see a drop-down such as this:




Now, to tell Visual Studio to use the correct xsd-file (wss.xsd) every time you create an onet.xml-file you need to link an xml-namespace to the wss.xsd file. To do this you need to create one more xml file and also remember to use the correct namespace for your onet.xml files.

First, create a new XML-file in C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas. Call it whatever you like, for instance SharePointCatalog.xml. Open the file and paste in the following:



<schemacatalog xmlns="http://schemas.microsoft.com/xsd/catalog">
<Schema
href="C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/XML/wss.xsd"
targetNamespace="http://schemas.microsoft.com/sharepoint/" />
</schemacatalog>




What this actually does is link the 'targetNamespace' with the schema located in 'href'. You can use the same technique to link your own schema to any namespace if you use those.


Now, to get Visula Studio to use the intellisense with the schema, add the namespace attribute to the root elemet of your XML file, for instance:

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://schemas.microsoft.com/sharepoint/">
</project>


If you now hit CTRL+Space you should get intellisense.

0 comments: