Click here to Skip to main content
15,881,424 members
Articles / Productivity Apps and Services / Sharepoint

The Importance of SharePointWebPartCodeGenerator

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Mar 2012CPOL3 min read 14.4K   1   2
The importance of SharePointWebPartCodeGenerator

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 visual web parts in a sandbox solution. 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 is the “designer file” creator 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. This is automated so it doesn’t usually affect the developer. The wonder that is the Visual Studio 2010 SharePoint Power Tools 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:

‘..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.

How-To

Let me describe this process. It is quite straightforward.

Make sure the markup file (e.g., <filename>.ascx) is selected in the Visual Studio project as shown below:

Select the ASCX In VS Solution Explorer

View the Properties of the markup file and make sure the ‘Custom Tool’ field contains ‘SharePointWebPartCodeGenerator’ as shown below:

Add SharePointWebPartCodeGenerator as CustomTool in ASCX's File Properties

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
Caleb is a software development consultant specialized in creating web solutions for critical business problems. He has a passion for front-end development and helping other developers find their role. He enjoys making development easier to do, easier to learn and easier to improve upon. His days are pleasantly filled with TypeScript, HTML5 and C#.

Comments and Discussions

 
QuestionAmbiguous statement Pin
B. Clay Shannon25-Feb-15 8:18
professionalB. Clay Shannon25-Feb-15 8:18 
Caleb,

You wrote, "Honestly, I have yet to actually enjoy SharePoint development"

By this, do you mean you've done it, but didn't enjoy it, or that you haven't done it at all?
AnswerRe: Ambiguous statement Pin
Caleb McElrath2-Mar-15 6:35
professionalCaleb McElrath2-Mar-15 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.