Sunday, January 23, 2011

How to create feature based list definition of a SP 2010 list/ library with the help of VS 2010

In the development environment, developer has the flexibility to create lists and libraries with out of the box SharePoint list/lib creation wizard. But, when a project moves from Dev env to QA env to Production env, development team generally don’t have the admin power on those environment. So, they are supposed to create the solution package and need to pass the same to the QA or Prod owner. This article will provide steps one need to follow to create a feature based list definition and list instance, which can be a part of the solution package.

Step I

First create a List named ‘MailingWFList’ in a SharePoint site and create the following column there:


So, our objective is to create a feature file activating which in specific site will create a ‘MailingWFList’ (i.e., a List Instance) named List in that site.

Step II

Open VS 2010 and Select New Project. Then navigate to SharePoint 2010 installed project templates and select List Definition Project. Give it a proper name and select Ok.


In the next page, specify the site where you would like to debug it and then select the ‘Deploy as a farm Solution’ option and select Next.


In the next screen, we need to define List Definition Settings. Give a proper name to your List Definition and as we are going to create feature of an custom list, select the ‘Custom List’ option from the type dropdown. Make sure ‘Add a list instance for this list definition’ option is checked out. Select Finish.


Step III

In the solution explorer, expand the ‘ListDefinition1’ node and open the highlighted Element.xml file (i.e., Element File of the List Definition).


Now we need to modify certain attribute of this xml file:


See the auto generated warning message before the starting of List Template and Name tag. Though I have changed it, I will suggest you not to change the Name attribute’s value. Change the type to some unique number greater than 10000. If you want to change the DisplayName or Description change it. BaseType = ‘0’ indicates a SharePoint List. In case you want to create a Doc Lib, change it to 1. So, after changing the List Definition Element File, our xml file looks like:


Save it and close the file.

Step IV

Now open the Element.xml file of the ListInstance1 Node.



Now the Title present here, is going to be the name of the List/ Lib in your site, so change it carefully. In our case, we want the name of the list should be ‘MailingWFList’. Copy and paste the new title after ‘Lists/’ value present in the ‘Url’ attribute. Change the TemplateType to the unique greater than 10000 number that you have already assigned in the List Definition’s element.xml file. In our case it is 15000.


Save and Close it.

Step V

Open the Schema.xml file


The first thing you need to do is delete the highlighted ‘ContentType’ section.


Next, inside the fields tag, starts putting the columns you want to be a part of MailingWFList.


Let’s look into more details


Certain points that developer need to keep in mind:

1. ID should be unique GUID for each field.

2. Display Name can be anything but, Name and Static Name should be same.

3. Type should be changed as per requirement, means ‘Text’ for string values, ‘Number’ for integer values and so on.

4. ColName should be on sequential, means first Text Type Column will be nvarchar1, next one should be nvarchar2 and so on. Similarly for the columns of type Integer, ColName should be int1 for the first column, int2 for the second column and so on.

Now, if you want the columns that you just added needed to be shown in the default view of the list, expand the ‘ViewFields’ node and insert the column names there as shown below:


So our final ViewFields tag looks like:


Now save it and rebuild the solution. Next open up your target site and delete the MailingWFList that you have created at very first step. Next, come back to the Visual Studio solution and right click on the MailingWFList project and select Deploy.Now again go to the target site and select View All Site Content. You should find out one new list – ‘MailingWFList’ – created using feature file.



Sunday, January 16, 2011

How To add custom menu item in SharePoint ECB Menu (Edit Control Block) via Feature

You can add a custom menu item for an entry control block to a list item or a document in Microsoft Windows SharePoint Services 3.0 by creating a Feature with a CustomAction element. In this way, you can add custom commands to the default SharePoint user interface. These menu commands allow users to perform custom operations on items and documents. For example, you can create a custom menu item for an entry control block for a list item or document that redirects the user to a custom application page. So, lets get started.

First create a new project in VS 2010.

Next select, Empty SharePoint Project and give it a proper name

In the next screen specify the Site and Security Level for Debugging. Here we have selected to deploy our solution as a farm solution.

Now, Select the ‘Feature’ option available in the Solution Explorer, right click onto it and add a new feature

If you want, you can rename the Feature folder to another name


Next define the Scope of your feature. In our case I would like to have this feature to one specific site, so I am selecting the scope as ‘Web’.

Now select the project file and right click onto it and select add a new item.

Select ‘Empty Element’ template available under SP 2010 category, give it a proper name and select Add.

Now, open the newly added Element.xml and replace its content with the content given below.

Code Snippet
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Elements xmlns="">
  3.   <CustomAction
  4.     Id="CA_WF_Init"
  5.     RegistrationType="List"
  6.     RegistrationId="101"
  7.     ImageUrl="/_layouts/1033/Images/RTEDCELL.GIF"
  8.     Location="EditControlBlock"
  9.     Sequence="301"
  10.     Title="My Custom ECB Menu Item" >
  11.       <UrlAction
  12.         Url="~site/default.aspx?ItemId={ItemId}&amp;ListId={ListId}"/>
  13.         <!--Url="<Custom Application Page URL>?ItemId={ItemId}&amp;ListId={ListId}"/>-->
  14.   </CustomAction>
  15. </Elements>

Save it and Select F5. The VS 2010 will deploy your solution and then it will open up the debugging sites default home page. Go to any of the Document Library and select any record, open its ECB Menu and Here comes your custom ECB Menu Item:

If you select this custom menu item, you will be redirected to the url you have specified in your Element.XML file. So, very easily you can redirect user to your applications custom page also!

Here certain things you need to know. In SP, everything has some ID associated with it. So, the registration ID specifies in which categories Edit Control Block, you want your custom menu to appear. The complete list of Registration ID in respect to SP 2010 can be found from Mike Smith’s blog post.

If you want to know about the different attributes present in the Custom Action Element, select the following url:
CustomAction Element in depth from MSDN.

Now, here comes another very interesting tricks: Sometimes in real life project the requirement will be to add Custom ECB Menu Item to SPecific List/ Libraries. How to do that? The answer is very simple, whenever you create any list/ library through feature, you provide some unique Template ID in that xml. Copy that ID and place it as your Registration ID. Bang! You are done.


Monday, January 3, 2011

How to use log4net in SharePoint based Applications

In my current project the requirement is to use log4net as the main logging mechanism to log the errors and exceptions generating from SharePoint based applications. Before this we have used log4net in many .Net based applications and that is pretty much simple to configure and to use. Unfortunately when we implemented log4net the same way in our SharePoint based projects, it failed miserably. So, we started googling and found this is a known issue but we are unable to find out a proper step by step solution to resolve the issue. Fortunately we find out some interesting facts and mechanism to overcome this issue in Mike Knowles blog So, we thought to collect all the information’s in a single place and to share with you guys. So, let’s start:

Step I

Create your own web app -> Site Collection -> Site.

Step II

Deploy the log4net DLL to the GAC.

Step III

Open your Site’s Web.Config file from Inetpub -> wwwroot -> wss -> virtual directories -> port-no -> web.config

Now, add an entry to the Web.config configSections to register the new configuration block:

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

Next, create a folder named ‘App_Data’ inside ‘Inetpub -> wwwroot -> wss -> virtual directories -> port-no’ folder. Inside App_Data create another folder named ‘Logs’ and inside ‘Logs’, create a text file named ‘RollingFileLog.txt’. Give full Control permission to these 3 file/ folders.

Now add the log4net configuration section to Web.config just prior to the system.web section.

<log4net debug="true">
<appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">
<file value="App_Data\\Logs\\RollingFileLog.txt" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="yyyyMMdd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d{dd MMM yyyy HH:mm:ss} [%p] %c - %m%n" />
<securityContext type="log4net.Util.WindowsSecurityContext">
<credentials value="Process" />
<level value="ALL" />
<appender-ref ref="GeneralLog" />

Now add log4net assembly information in the ‘SafeControl’ section:

<SafeControl Assembly="log4net, Version=, Culture=neutral, PublicKeyToken=1b44e1d426115821" Namespace="log4net" TypeName="*" Safe="True" />

Step IV

Now open your Visual Studio solution. Right Click on the Solution Node and add a new project of type Class Library. Give it a proper name like ‘log4netModule’ as we are actually in a process of creating one HTTP Module. Add reference to log4net dll. Add a strong key into the project.

Add a class file or use the default one. Place the following code inside that file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;
using log4net;
using log4net.Config;
using log4net.Util;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace XXX.log4netModule
public class Initlog4netModule : IHttpModule
public void Init(HttpApplication context)

public void Dispose()

Compile it and copy the error free assembly from the source location.

Step V

Place assembly copied in the last step, inside the ‘bin’ folder present inside the ‘Inetpub -> wwwroot -> wss -> virtual directories -> port-no’ folder. And deploy the same copied assembly into the GAC.

Now, again we need to update the web.config file. Place the following line inside the ‘<httpModules>’ section present inside the ‘<system.web>’ section and modify it as per the name you have given to your project.

<clear />
<add name="Initlog4netModule" type="AZL.log4netModule.Initlog4netModule, AZL.log4netModule, Version=, Culture=neutral, PublicKeyToken=4f97192e309c5502" />

Step VI

Open the IIS7 Administration tool via the Start menu, typing in inetmgr.exe in the start/search box and pressing Enter. In the tool, double-click on your server node in the left-hand treeview, then expand the “Sites” node, and double-click on the site or application to which you would like to add your module and handler.
Select the “Modules” feature icon, then click the “Add Managed Module …” action, and in the resulting dialog box type in the module name (arbitrary) and then select the type in the dropdown box, as the tool will automatically load your assembly in bin and discover types that implement the IHttpModule interface. Press OK to add the module.


Reset IIS.

Step VII

Now, open up your original solution in visual studio. In the desired page add a reference to the log4net assembly.

using log4net;

Next, to test if at all log4net is working or not, place the following lines inside your page load event:

ILog log = LogManager.GetLogger("TestLogger");
log.Debug("Error logged");

Save and compile and run the solution.

If you have followed all the steps perfectly, then the log file present inside the Logs folder should have the following entry:

“<Date Time>  [DEBUG] TestLogger - Error logged”

The whole source code can be downloaded from the following link: