Vs 2013 SharePoint visual webpart .ascx.g.cs file missing

During SharePoint 2013 visual webpart development, some time VS 2013 will miss or delete the .g.cs and during compilation, we may get the errors like “InitializeControl does not exist in current context” .

To generate a new webpartusercontrol.ascx.g.cs file we need to follow the below steps

1. Unload the SharePoint Project

2. Right click the project and Unload the project(Edit the .csproj file)

3. Find the webpart where the .g.cs file is missing

4. Check the below XML Tag in that place, if its missing or “LastGenOutput” is missing then add accordingly

<Content Include=”Webparts\WP_TestWebpart\WP_TestWebpart.ascx”>

5. Save the .csproj file and load the project

6. Now Right click on the .ascx file and Run the Custom Toll. (Make sure in the properties of ascx , customTool is SharePointWebPartCodeGenerator)

7. Now the webpart .g.cs file will be generated and we can compile without any errors

I hope this may help someone who struggle or delete the webpart again , to generate the .g.cs file 🙂

Update Panel with Required Field Validator Not Working

Recently i faced some strange problem with required field validator, inside a update panel its not working, since some controls are dynamically loaded with required field validattion.

But its not working properly (few buttons which has causes validation set to false also not triggering properly), then i found a solution that for any required field validation (or any validation controls), it has to load during the page load itself , so i have created a dummy text box and dummy required field validator to make sure that the validation controls emits the javascripts properly to browser.

<div style=”display:none”>

<asp:TextBox id=”txtDummy” runat=”server” Visible=”false”/>

<asp:RequiredFieldValidator ID=”reqDummy1″ runat=”server” ControlToValidate=”txtDummy” Text=”*” ValidationGroup=”DummyValgroup” ErrorMessage=”Please provide text”></asp:RequiredFieldValidator>


This helps to do the validation for dynamically created controls with validation.

 I hope this may helpful for

Property Bag in SharePoint

In SharePoint we can store a property value like a Hash table (key- value pair) using custom coding.
This property can be used to set and retrieve at various objects of SharePoint like (SPSite, SPWeb)

Following is a small sample piece of code to set the property value

//open the web and set the property
using (SPSite spSite = new SPSite(strWebUrl))
using (SPWeb spweb = spSite.OpenWeb())
spweb.AllowUnsafeUpdates = true;
bool isPropertyFound = spweb.Properties.ContainsKey(propertyKey);
if (isPropertyFound)
spweb.Properties[propertyKey] = propertyValue;
spweb.Properties.Add(propertyKey, propertyValue);
spweb.AllowUnsafeUpdates = false;
catch (Exception ex)
throw ex;

How to Remove automatic web.config backup files in SharePoint using PowerShell

During SharePoint solution deployment, SharePoint takes a backup copy of the web.config with the name as web_.bak. Since SharePoint is taking the backup everytime when we deploy a solution during development of any webparts/any solution package, the files in the IIS physical folder will be keep on going, and its no longer required (in case of development/test).

I have created a small script in Powershell to delete the web.config backup files. Please note that this script is intended for development/test environment and dont directly use it in production environments. Also note that i am considering the default zone url of the web application. This script can be saved as .ps1 file (like RemoveSharePointWebconfigbackup.ps1) and can be schedule with windows scheduler to remove automatically, may be every day at 12AM.

Caution: Please Take the backup of IIS Folder before running at first time and second time

PowerShell Script :

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction “SilentlyContinue”
$WebApplication =”http://webapplicationurl&#8221;
$SPSite = New-Object Microsoft.SharePoint.SPSite($WebApplication)
$webApp = $SPSite.WebApplication
#get the IIS path
$IISSettings = $webApp.IisSettings[[Microsoft.SharePoint.Administration.SPUrlZone]::Default]

Write-Host ‘Displaying IIS path $IISSettings.Path’
$stIISPath = $IISSettings.Path.FullName
Write-Host $stIISPath
#write $WebApplication
$WebConfigBackFiles = Get-ChildItem $stIISPath web_*.bak
#Write-Host ‘ Files count =’
#Write-Host $WebConfigBackFiles
foreach ($tempFile in $WebConfigBackFiles)
 $t = “$stIISPath\$tempFile”
   #Write-Host $t
    Remove-Item $t

SharePoint Custom webpart with SharePoint resource files


In SharePoint custom webpart we may need to display the message or label text from a congfiguration resource files, this article shows the how to read the resource files from the custom webpart.

The article shows the step by step with the help of visual studio 2010.

Step 1: First we need to create the visual webpart project in visual studio 2010.

Step 2: After adding the visual webpart project, in the solution explorer, right click and add new sharepoint mapped folder and refer the folder Resources.

Once the resource folder is mapped, right click and add the folder (since so many resource files are present in the 14\Resource folder used by sharepoint and we create our own folder and use it for the particular webpart /particular web application ). Lets call the folder as CustomResource folder.

Step 3: Now add the resource file by right click on the CustomResource folder and add new item, select c#, and select the resource file, name it as CustomResource.resx

Step4 : Add the key value in the resources file, say for e.g CustomSuccessMsg, and value as “Thanks for submitting the form “.

Step 5: Now we can use the resoure file in our custom webpart, by adding the below code.

uint currentLanguage = currentWeb != null ? currentWeb.Language : 1033;

string stSuccessMsg = SPUtility.GetLocalizedString(“$Resources:CustomSuccessMsg”, “CustomResource\\CustomResource”, currentLanguage );

Step 6: now compile the webpart and deploy using vs 2010 or using powershell.

Once its deployed if the site is running in english language then it will read it from CustomResource.resx file from the CustomResource folder of SharePoint Root folder.

If the language is differnet, then appropriate resource file is required. Some sample resource file name appropriate lanaguage




I hope this will help some one who try to use resource file to store the message or listname or any other configuration parameter according to the lanaguage of the site is going to be used.