Subscribe to UMDTAOC        RSS Feed

The Importance of SharePointWebPartCodeGenerator

Icon Leave Comment
Honestly, I have yet to actually enjoy SharePoint development specifically but, I still let myself learn. I want to briefly describe an issue I had when creating a sandboxed visual web part. As the name of this entry implies, the SharePointWebPartCodeGenerator plays an important role in the web part creation. I will briefly describe what it is, where it is used, and why it is important. A basic knowledge of ASP.Net and developing in Visual Studio is necessary.

Huge name, small functional scope

The SharePointWebPartCodeGenerator is quite a self-descriptive name. It is a tool used to generate code for a web part. It creates the “designer file” for the visual aspect of the SharePoint web part. Let me explain by describing what this is in average ASP.Net. In ASP.Net, pages and controls (ASPX and ASCX respectively) have a corresponding designer file. This is where the generated UI code is placed when a developer adds such things as “<asp:Textbox..” or “<asp:Literal..” etc etc. This is automated so it doesn’t usually affect the developer. The wonder that is SharePoint 2010 requires the use of the tool SharePointWebPartCodeGenerator. I’ll explain how and where to use this tool in the following sections.

Case in Point

I recently created a web part to display items from a list in a certain way and to wrap some specific functionality around those items. I went with a Visual Web Part. I find it a lot easier to develop a web user interface when it can actually be seen in html while I’m developing it. As usual, I found a better way to implement the web part only AFTER I finished creating it. The change involved updating the controls used in the markup for the web part. After making the changes, I attempted to build the solution and got a nice error similar to:

Error: ‘..WebPart’ does not contain a definition for ‘userControlID’ and no extension method ‘userControlID’ accepting a first argument of type ‘..WebPart’ could be found (are you missing a using directive or an assembly reference?)

Looking at the project structure I noticed there was no designer file. Obviously that is a problem! No designer file means that there is no code-behind support for the management of controls in my web part. I finally found that I needed to use a tool to create this designer file. I had to add ‘SharePointWebPartCodeGenerator’ to the ‘Custom Tool’ field of the markup-file’s properties.


Let me describe this process. It is quite straightforward.
  • Make sure the markup file (e.g. <filename>.ascx) of the visual web part is selected in the Visual Studio project.
  • Then view the Properties of the markup file and make sure the ‘Custom Tool’ field contains ‘SharePointWebPartCodeGenerator’.

This was all I needed to fix my issue. Editing the markup was no problem after I made that entry into the properties. It now automatically runs the custom tool when I save my changes to the markup file.

0 Comments On This Entry


Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

May 2022

2223 24 25262728

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)

Looking for a short-term side project

Know of someone needing a custom solution to a technical problem? message me here or view my linkedin profile here.