Wednesday, September 22, 2010

SharePoint 2010 Development with Visual Studio 2010: Add Resources (like Elements.xml) to Feature

We all SharePoint developers are familiar with Feature.xml and Elements.xml files. We usually defines feature’s resources in Element files. However, in Visual Studio 2010 to add Elements file to Feature we need to do few extra works. You can’t just add the Elements files directly in a feature folder in Visual Studio. You may still need to add the Feature element manifest file, for example in cases where you want to add a custom menu or hide existing menu for site actions. The following steps show how to add Elements.xml file in a SharePoint Feature from Visual Studio 2010.

Step 1: Create a new Feature

Create a feature for which you want to add the Element manifest file. If you have the feature already, then you can the feature to be used for attaching Element.xml file.

Step 2: Add Empty Element File

You need to add an empty element file in the project. To add Element.xml file, create a Empty Elements file in the project from ‘Add new Item’ window as shown below:

image

For manageability, you can keep all Element files in a single folder, say “FeatureElements” folder.

 

Step 3: Add ElementFile to elements.xml

Now, let’s say you want to add a configuration xml file (say MyConfig.xml) to element file. To do so right click on the newly created empty element and add a new item or existing item. Then open the properties of the new/existing item (so, MyConfig.xml) and set the ‘Deployment Type’ to ElementFile.

image

 

Step 4: Attach Elements file to Feature

Now you need to attach the Element file to a feature. To do so open the feature designer by double clicking on the feature name from Visual Studio’s solution explorer. Then make sure your Element file is on the right side (Items in the feature) of the designer. The following figure shows the Element file MyFeatureElement is attached with the feature whereas YourFeatureElemet is not attached.

image

6 comments:

  1. Thanks. It helped me out.
    I think if the scope of the feature is web/site we can even create modules(Add a new item -> Module)and then associate the element.xml file of the module to any feature(scoped at web/site level).

    ReplyDelete
  2. Cheers, very helpful details on element.xml

    ReplyDelete
  3. Then how to read this custom xml into the code. I mean with the relative path? How would we call this xml?

    ReplyDelete
  4. I’d have to check with you here. Which is not something I usually do! I enjoy reading a post that will make people think. Also, thanks for allowing me to comment!

    ReplyDelete