Wednesday, 6 May 2015

How to resolve .ascx.g.cs missing in SharePoint 2013


Normally this issue happened in the Visual Webpart (refer below screen shot).


The structure of this webpart is look likes below.


If you missed the site url in the project property or VS failed to communicate SP then the g.cs file is removed from the project.   This will not give any error or warning message but your updates in the ascx will not reflect in the ascx.cs file and webpart.

Steps to Resolve

                Below steps for the people who are not using any SVN because in SVN we can restore to the previous version.

1.       Add new item and select text file Name it as CalculatorWebpart.ascx.g.cs




        2.    Move the file inside “CalculatorWebpart”.
      3.   Set valid site URL in the project property by select the property then hit F4 key



         4.  Create a dummy visual webpart to get sample entries in the project file.  I named it as “Dummy          Webpart”.  If you have another properly working webpart then skip this step.
        5.  "Save All” the project file
        6. Open project in the File Explorer.
        7.  Move the “CalculatorWebpart.ascx.g.cs” to the “CalculatorWebpart” (webpart) folder.
        8.  Right click the project file(.proj) and open in the “Notepad”.
        9.  Find “DummyWebpart.ascx.g.cs” you have two entries make similar entries for
           “CalculatorWebpart.ascx.g.cs” in the same area.


    <Compile Include="CalculatorWebpart\CalculatorWebpart.ascx.g.cs" />
Become
<Compile Include="CalculatorWebpart\CalculatorWebpart.ascx.g.cs" >
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>CalculatorWebpart.ascx</DependentUpon>
    </Compile>

And
                <Content Include="CalculatorWebpart\CalculatorWebpart.ascx">
      <Generator>SharePointWebPartCodeGenerator</Generator>
</Content>

Become
                    <Content Include="CalculatorWebpart\CalculatorWebpart.ascx">
<Generator>SharePointWebPartCodeGenerator</Generator>
                                <LastGenOutput>CalculatorWebpart.ascx.g.cs</LastGenOutput>
    </Content>

    10.   Save the .proj file.
    11.   Go the VS and refresh the project.
    12.   Open “CalculatorWebpart.ascx” do any modification and retract it.
    13.   Now you can see the updated “CalculatorWebpart.ascx.g.cs”

Happy Coding :)
-
Sugunthan Shanmuga Sundaram.