“The name ‘InitializeControl’ does not exist in the current context” – Visual Web Part (Sandboxed) bug and how to fix it

, , , , , ,

Visual Web Part (Sandboxed) designer file selected in Visual Studio 2010.
A while ago Microsoft provided us with Visual Studio 2010 SharePoint Power Tools – a set of productivity extensions for the Visual Studio 2010 SharePoint Developer Tools. One of the improvements shipped with the Power Tools is the Visual Web Part (Sandboxed) that – as the name says it – allows you to both benefit of the great design capabilities of Visual Web Part while still being able to deploy it to Sandbox. Unfortunately if your Sandboxed Visual Web Part will get too large things won’t be working as expected anymore.

Visual Web Part 101

One of the great things about Visual Web Parts is the simplicity of designing the UI. While in a regular Web Part you would have to code every single line of the UI, the Visual Web Part allows you to leverage the power of User Controls’ (ASCX) design surface to design the UI imperatively, what makes it very easy not only to design the UI but also to maintain in.

During the deployment process the User Control is being deployed to the {SharePoint Root}\TEMPLATE\CONTROLTEMPLATES folder. On runtime the Web Part loads the User Control and instantiates it and so the imperative UI becomes a part of the Web Part.

Visual Web Part (Sandboxed)

When working with Sandbox things look a little different. Sandboxed Solutions have no access to the file system and therefore cannot deploy any files, such as User Controls, to the SharePoint Root folder. Luckily, there are better alternatives for us than coding the UI in our custom Web Parts.

‘Add New Item’ Visual Studio 2010 dialog window with the Visual Web Part (Sandboxed) item template selected.

Last June Microsoft provided us with a great alternative to coding the UI – the Visual Web Part (Sandboxed). Provided as a part of the Visual Studio 2010 SharePoint Power Tools, the Sandboxed Visual Web Part is a great productivity extension every SharePoint developer should be familiar with. Just like its older brother, the Sandboxed Visual Web Part uses User Controls to imperatively design the UI. The biggest difference between those two Web Parts is that during the build process, the User Control of the Sandboxed Visual Web Part is being parsed and included in the assembly as a class. And so no trace of ASCX is to be found in the generated SharePoint Solution Package (WSP) and the Web Part can be deployed to Sandbox.

Sandboxed Visual Web Part offers you a great way to create custom Web Parts and become productive. Additional benefit that you get from using the Sandboxed Visual Web Part is the security: because you don’t need to load and instantiate the User Control on runtime the Sandboxed Visual Web Part requires less privileges. So even if your Solution won’t be deployed to Sandbox you should definitely consider using the Sandboxed Visual Web Part.

There is however one ‘however’…

The name ‘InitializeControl’ does not exist in the current context

Recently, while working with the Visual Web Part (Sandboxed), my fellow developer stumbled upon a weird issue. After implementing some UI in the Sandboxed Visual Web Part’s User Control, he got the following error while trying to build the project:

“The name ‘InitializeControl’ does not exist in the current context” error message displayed in Visual Studio 2010.

Surprisingly: the User Control’s designer file (.ascx.g.cs) was missing! What’s also surprising was that a newly added – empty – Visual Web Part (Sandboxed) didn’t give such error. So what was exactly going on?

After some research we found out that the error has to do with the length of the contents of the User Control. Once you exceed the limit of 8256 characters the User Controls won’t be parsed anymore. Compare the two images. With the contents length of 8256 characters the ascx.g.cs file is still present in the solution.

ascx.g.cs file present in Visual Studio 2010.

Once you add another character to the file and save it, the ascx.g.cs file will disappear and the familiar error will appear if you try to build the project.

ascx.g.cs file missing in Visual Studio 2010.

So does this mean we have to slice UI into multiple controls in order to use the Sandboxed Visual Web Part?

Fix, the

The great news is, that we don’t have to do anything with our code! Last December Microsoft published Visual Studio 2010 Service Pack 1 Beta which fixes this issue. After you install this Service Pack, the limit issue is solved and you can proceed with designing rich and complex UIs for Sandboxed Visual Web Parts.

Big thanks to the Visual Studio team for fixing this bug!

30 Responses to ““The name ‘InitializeControl’ does not exist in the current context” – Visual Web Part (Sandboxed) bug and how to fix it”

  1. Massimo Prota Says:

    Hi Waldek,
    I've had this problem in the past and I was not aware it was fixed, so thank you another time.
    I just want to add that in my case the error message was completely different and was:
    Warning 19 The custom tool 'SharePointWebPartCodeGenerator' failed. There was an error deserializing the object of type Microsoft.VisualStudio.SharePoint.Commands.CommandParameter. The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 197, position 21.

  2. Waldek Mastykarz Says:

    @Massimo: Hi Massimo, I've seen you thread on MSDN SharePoint Forums indeed and I was really surprised seeing that I had more chars to spend than you ;) The most important thing is that it's fixed and we can benefit of the Sandboxed Visual Web Part.

  3. Dave Says:

    I have installed Visual Studio SP1 (Version 10.0.40219.1 SP1Rel) and the Visual Studio 2010 SharePoint Power Tools.

    When I create a Visual Web Part (Sandboxed) from scratch in an Empty SharePoint Solution, the ascx.g.cs file is NOT created. So I end up with a newly created project that does not compile (InitializeControl does not exist in the current context).

    Am I missing an update or some other fix?

    Thanks

  4. Waldek Mastykarz Says:

    @Dave: that sounds like a bug somewhere on the way. I assume you provided a correct URL while creating the project, right?

  5. Ben Weeks Says:

    I also received this error, but when I was introducing AJAX functionality. I have installed the Visual Studio 2010 SP Power Tools and SP1 but still get the error unfortunately (no lengthy ASCX).

  6. Waldek Mastykarz Says:

    @Ben: You could try to see if enabling debugging for the Visual Studio 2010 SharePoint Developer Tools would provide you with some additional information about your issue. You can find more information about how to do this here: http://msdn.microsoft.com/en-us/library/ee471440.aspx

  7. SK Says:

    Hi Waldek, I still see the issue even with the newly added visual web part(sandboxed). designer file is missing. I also installed the service pack 1. Is there a way to track this issue.

  8. Waldek Mastykarz Says:

    @SK: Are you sure there's nothing else wrong that would keep your project from building and therefore from generating the designer file?

  9. SK Says:

    @Waldek: The issue is resolved, it is because I used a site URL which is not existing. I updated the site URL with a new one then when I add visual web part then I am seeing designer file.

  10. Salvatore Di Fazio Says:

    Hi guys,
    I still have the same problem with the sp1 installed…

  11. Rohit Says:

    I Have also same problem with Sp1 Installed.

  12. Rohit Says:

    what is this exactly issue. when i am going to add multiple control in visual webapart the designer file was hide. how to solved this problem.i have also installed visual studio 2010 sp1 please help

  13. NLV Says:

    You saved enormous amount of my time. Thank you.

  14. Marcin Robaszyński Says:

    I've been fighting with this whole day :/ In my case I've project which was working on another computer and not on the another. After rebuilding and packaging project .g.cs files were removed by VS.

    As it turned out it was because I've used fake site URL in project properties (just like SK said).

  15. Roman Says:

    Hello,

    still got this problem. And I think with a valid URL. Any other ideas?

  16. Waldek Mastykarz Says:

    @Roman: Are you using references to any external assemblies that are not deployed to GAC?

  17. Marcin Robaszyński Says:

    @Roman
    If this was a problem with valid URL to the SP site then, as far as I remember, after rebuilding the project *.g.cs files will disappear and they will be NOT restored after setting valid URL (and rebuilding). You have to add new webpart or fight with .csproj file ;)

  18. Satish Says:

    Does anyone get resolved this issue. Previously I use to have visual studio 2010, it allows me atleast 8256 characters and after that designer file disappears and it won't compile. Now after seeing this, I installed Visual studio 2010 sp1, it got more worse than previous. Now its not at all creating designer file(.ascx.g.cs). Any help would be appreciate.

  19. Waldek Mastykarz Says:

    Have you tried to check if it's the particular ASCX that isn't working or does Visual Studio 2010 not build ASCX anymore at all?

  20. remo Says:

    hi waldek,
    i am trying to convert a visual webpart into sandboxed visual webpart,i have sp power tools installed,i am using same html code from visual webpart,but still for textboxes,button,dropdownlist in source code its showing error"doesnot exist in current context".

  21. Lakshmikanth Reddy Says:

    Thanks a lot and fighting with this issue since long hours.

  22. Lakshmikanth Reddy Says:

    Hi Remo,

    This problem might be again with number of characters issue. You can try installing visualstudio 2010 SP1 or test it by removing some characters from your code files as Waldesk suggested.

  23. remo Says:

    Is there a method to use datetime controls in sandboxed visual webpart(sp power tools is installed).?

  24. Waldek Mastykarz Says:

    Declaratively you should be all good. Programmatically: if the namespace/control is available in Sandbox you should be able to do that, if not, then it's not possible.

  25. remo Says:

    thanks waldek.

  26. Brett Says:

    I also encountered this maximum length issue with my sandboxed visual web parts. I installed Visual Studio 2010 SP1, but I still had the same problem: the .ASCX.G.CS file was not being created so the build failed. SP1 did not immediately fix it.
    But, I found that if I took some characters out, so that it was below the limit, and I rebuilt it, the build would work and the relevant .ASCX.G.CS was recreated. Now with this file recreated, the SP1 fix takes and you can go past the initial limit.

  27. Manu Says:

    Hi all,
    Yes, I also had it. Then I installed SP1 while prayin it might do some magic. Then I restarted the server and told everyone it was for maintenance.
    Finally I reopened the sandboxed solution and deployed it, hopping not to get the same error. And I got the same error. The frustration was overwhelming. I lost faith in everything I knew and contemplated the ideea of a new career in organised crime.
    … but then I read Bretts comment and while clearing 1 markup line ata time I recompiled the project until the error dissapeared. And then I could finally write and deploy without Visual Studio going retarded on me.
    So … thanks Waldek, Brett, and all other folks who fought with this dark issue.

  28. Ricky Says:

    Hi,

    I still have a problem with disappearing code in my webpart's ascx.g.cs-file if I use a custom usercontrol like this in SharePoint 2013:

    The path is definitively correct, if I create a Visual Webpart in SP2010-Style manually without the "Add new item"-Template from VS2012, the code works.

    How can I use my old user controls referencing ascx with code-behind in this new Visual Webpart 2012-Style in my farm solution?

    Greetings and thanks a lot
    Ricky

  29. Promesses Says:

    Hi,
    I've got the problem too in Visual Studio 2012.
    Insert a usercontrol in a Visual WebPart

  30. Sam Says:

    facing same issue, any suggestion? Thanks in advance.

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS
Copyright © 2007 - 2013 Waldek Mastykarz

Creative Commons License