Programming
How to fix slow Visual Studio + TFS
3Upon installing Visual Studio 2012, I found out that all operations related to TFS (such as adding a new file, check in/out, etc.) were incredibly slow and would freeze the application for quite some time. If this is the case for you, try this solution:
Edit the Visual Studio config file found in C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe.config
and locate the following line:
<system.net>
Right below (above settings, NOT under), insert the following line of code:
<defaultProxy enabled="false" />
Your visual studio should now be more responsive!
CSS/jQuery Selector – ID with a dot
0I had an issue today while trying to use a jQuery selector on simple div with ID “Ribbon.Read-Title”. This specific selector (#Ribbon.Read-Title) usually means that we are matching the ID “Ribbon” have the class “Read-Title”. But trying to match the ID “Ribbon.Read-Title” will simply not work with this selector. This is also true for a CSS selector.
The workaround is to escape the dot with a backslash. So in the above example, you would use selector #Ribbon\.Read-Title to match the specific ID “Ribbon.Read-Title”.
Note: Contrary to what some people believe, using a dot in an ID or Name attribute is totally valid and accepted, as specified by the W3C. However, it’s easy to confuse the default selector behavior for dots.
Sharepoint 2010 – Tableless webparts guide
8If you tried to create even simple page layouts, you probably noticed the mess Sharepoint creates with tables when placing webpartzones and webparts in the page. While searching about a way to remove completely these tables, I found an interesting solution by David Schneider, which removes tables completely but creates a new whole set of issues with Sharepoint. While the tables are removed around webparts and webpartzones, it breaks functionality of the ribbon in lists and some ajax functionnality on default Sharepoint controls. Not really convenient.
Fortunately, with the help of a colleague, we found a way to remove the tables only on defined webpartzones. Zones marked with a property called tableless=”true” will be stripped from their tables while all other Sharepoint controls won’t be affected at all.
So what needs to be done in order to get this functionnality? Follow these steps:
1) Copy the following modified C# code (notice the tableless property that was added to David Schneider’s solution) in a file called AKS_WebPartZone_Adapter.cs and place it in the AppCode directory under your server root directory (like C:\Inetpub\wwwroot or C:\Inetpub\wwwroot\wss\VirtualDirectories\80).
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
/// WebPartZone Adapter
/// Created by David Schneider
/// http://blog.sharepoint.ch
/// Based on AKS
namespace AKSAdapters
{
public class AKS_WebPartZone_Adapter : System.Web.UI.Adapters.ControlAdapter
{
protected override void Render(HtmlTextWriter writer)
{
bool inEditMode = false;
System.Web.UI.WebControls.WebParts.WebPartZone wpz = Control as System.Web.UI.WebControls.WebParts.WebPartZone;
if (wpz != null && wpz.Attributes["tableless"] != null)
{
SPWebPartManager swpm = (SPWebPartManager)SPWebPartManager.GetCurrentWebPartManager(wpz.Page);
inEditMode = !swpm.GetDisplayMode().AllowPageDesign;
}
if (inEditMode)
{
// Render the WebPartZone
writer.Indent++;
writer.AddAttribute(HtmlTextWriterAttribute.Id, wpz.ID);
if (!String.IsNullOrEmpty(wpz.CssClass))
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, wpz.CssClass);
}
if (wpz.LayoutOrientation == System.Web.UI.WebControls.Orientation.Horizontal)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "AspNet-WebPartZone-Horizontal");
}
else if (wpz.LayoutOrientation == System.Web.UI.WebControls.Orientation.Vertical)
{
writer.AddAttribute(HtmlTextWriterAttribute.Class, "AspNet-WebPartZone-Vertical");
}
writer.RenderBeginTag(HtmlTextWriterTag.Div);
writer.Indent++;
// Render the web parts
if (wpz.WebParts.Count > 0)
{
WebPartCollection wpColl = new WebPartCollection(wpz.WebParts);
foreach (System.Web.UI.WebControls.WebParts.WebPart wp in wpColl)
{
writer.WriteLine();
writer.AddAttribute(HtmlTextWriterAttribute.Class, "AspNet-WebPart");
writer.RenderBeginTag(HtmlTextWriterTag.Div);
wp.RenderControl(writer);
writer.RenderEndTag(); // Div
writer.Indent++;
}
}
writer.RenderEndTag(); // Div
writer.Indent--;
writer.WriteLine();
}
else
{
// If we are editing the page --> render the web part as usual.
base.Render(writer);
}
}
}
}
2) Edit the compat.browser file which in the AppBrowser directory located in your server directory and add the following piece of code after the opening tag:
3) In your page layouts, add the property tableless=”true” to all your WebPartZones.
Your pages will then be free of tables without affecting core Sharepoint functionality such as the ribbon and other editing functions. Enjoy.
C# – Keep user settings between versions
5Unless you’re using the ClickOnce deployment method for your applications, user settings are usually not transferred when updating to a new version of a C# desktop application. There’s in fact a quick and simple way to always keep the settings between versions.
The trick is to call the .upgrade() method your app settings object. The system will look for previous versions of your application in the App Data directory and copy the user settings to the new version automatically.
Properties.Settings.Default.Upgrade();
But now we have a problem. How do you know when the upgrade method should be called? As we don’t want to upgrade everytime the application is started, but only the first time after an upgrade. The solution is to create a new user settings variable that will be used to determine whether an upgrade is required. You can either make it a string and save the current application version, or make it a boolean and set it to True by default. I personally find the boolean method easier to use and this is the one I will explain below:
In your application load function (private void Window_Loaded in WPF), simply use this code:
if (Properties.Settings.Default.UpdateSettings)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpdateSettings = false;
Properties.Settings.Default.Save();
}
It is important to place this code before anything else that use the user settings in the load method. In the above example, UpdateSettings is the user setting variable I created and set to True by default. When the application launch, it will check whether the UpdateSettings variable is set to True. If it’s the case, it will perform an upgrade. We then set the variable to False so that it doesn’t perform an upgrade the next time. Finally, we save the new settings. Why does it work? Because we set the user setting to True by default which will only occur after a new installation of the application.
Enjoy!
Sharepoint – Masterpage User Controls Limit
0If you insert more than 10 user controls in a masterpage, you might get an error similar to this:
The page ‘/path/somefile.master’ allows a limit of 11 direct dependencies, and that limit has been exceeded.
You can easily increase that limit as it is actually set in your sharepoint web.config:
1) Browse to C:\inetpub\wwwroot\wss\VirtualDirectories\
2) Open the folder corresponding to your Sharepoint website.
3) Edit the file web.config
4) Do a search for “safemode“.
5) Change the property DirectFileDependencies from 10 to a value your choice.
6) Do an iisreset command and you’re done!
Sharepoint 2010 – Display compilation errors
3By default, Sharepoint displays a friendly error box that tell pretty much nothing about the error that just occurred. It is possible to display the default yellow screen of death with all the details, found in usual ASP applications.
1) Navigate to C:\inetpub\wwwroot\wss\VirtualDirectories\
2) Open the Sharepoint web application folder you wish to edit.
3) Edit the web.config file.
4) Find the property customErrors and set it’s mode value to Off.
5) Find the property compilation and set it’s debug value to true.
6) Find the property SafeMode and set it’s CallStack value to true.
7) Do a iisreset.
Done!
Sharepoint 2010 – Custom Error Pages
4Unless you love the default Sharepoint error pages for 404 (not found) and 401 (access denied), it is usually a good idea to change them. The steps are a bit different than a standard ASP.net application.
1) Browse to C:\inetpub\custerr\
2) You should now see a list of localization folders like en-US or fr-FR. Open the folder you want to edit and modify the error pages for 401,403,404 and 500.
3) Now browse to C:\inetpub\wwwroot\wss\VirtualDirectories\ and open the folder associated to the Sharepoint site you would like to affect.
4) Edit the file web.config and find the keyword customErrors. Replace the complete customErrors code block with:
<customErrors defaultRedirect="404.htm" mode="On"> <error redirect="401.htm" statusCode="401" /> <error redirect="403.htm" statusCode="403" /> <error redirect="404.htm" statusCode="404" /> <error redirect="500.htm" statusCode="500" /> </customErrors>
5) Now find the keyword <system.webServer>. Inside this block, place the following block code. (If the httpErrors block already exists, replace it completely!)
<httpErrors errorMode="Custom" existingResponse="Auto"> </httpErrors>
6) Save web.config and do the usual iisreset command if needed.
ASP.net – Using multiple forms on the same page
3One if the big issue with ASP.net programming is having multiple forms on the same page. You can only have 1 form with runat=”server” on your page or it will throw an exception. When you have a form on your page and search box in your header, it can cause some big issues when you press Enter. The wrong postback will be triggered in the second form.
The solution is to use an <asp:panel> with a defaultbutton around each of your “HTML forms”. There’s only 1 form containing the whole page with 1 panel for every “HTML form”. By “HTML form”, I mean a series of inputs that are part of the same logical group and not an actual <form> tag.
Pressing Enter in any textbox will now trigger the correct default button. Problem solved!
Sharepoint – Access QueryString from custom webpart
2I was trying to access the QueryString parameters inside a custom webpart coded in C#. I would usually use Request.QueryString but the Request object was simply not available in this case. Turns out it is found under the Page object:
string kquery = Page.Request.QueryString["kqry"];
Sharepoint Designer – “The server could not complete your request” error message
3I started getting this error message yesterday while working in Sharepoint Designer. I would get the message whenever I would open a Sharepoint website. The solution to this problem is quite simple:
1) Open Internet Information Services (IIS) Manager.
2) Expand your computer list on the left then click on Application Pools.
3) Locate the application pool used by your Sharepoint website.
4) Right-click on the application pool and select Recycle.
You should now be able to connect to your Sharepoint website using Sharepoint Designer as usual.