Getting started with WTL on Visual Studio 2010

WTL on Visual Studio 2010

Now that you have downloaded the latest WTL release (v8.1 at the time of this writing), you will find out that it doesn’t have any project wizards for Visual Studio 2010. That’s a relatively easily fix. Just go ahead and download the latest build from the repository at AppWiz.tar.gz-view=tar. Extract the .tar file and you will find the JavaScript install scripts for VS 2010:

image

I recommend you to extract the files to the same location were you installed the WTL files. In my case this would be c:\WTL.  Double click the setup100.js file (or setp100x.js for Visual Studio 2010 Express) to install the WTL Project Wizard.

Add the WTL include path to the default include directories

One thing you may have noticed in VS2010 (compared to earlier versions) is that there is no way to set the global paths of VC++ directories in Tools->Options:

image

Bummer. How to solve that? You really don’t want to have to include the WTL include path in every VC++ project. Here’s how to set the global paths and include the WTL include path. It’s really not that intuitive, if I would say.

First you will need to have a project open in VS 2010. Then go to View->Other Windows->Property Manager. In the Property Manager, expand the project and the platforms nodes. Multi-select the two Microsoft.Cpp.Win32.user items. (By holding down the Ctrl key and select them each by the mouse).

image

Right click and select properties

image

Now you finally can set the global default search paths for the VC++ include directories. In my case I have added the C:\WTL\Include path. Save the properties settings restart Visual Studio.

If you open one of the samples in the WTL folders, you may stumble upon a peculiar error that says: error CVT1100: duplicate resource.  type:MANIFEST, name:1, language:0x0409

image

This is because the way manifest files are handled in VS 2010 has changed. What you can do is to open the resource file (.rc), edit it by selecting “View Code” and remark out the line

//CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "res\\BmpView.exe.manifest"

Now, happy WTL’ing in Visual Studio 2010.

How to detect the dark or light theme in Windows Phone 7

If the user of the Windows Phone 7 device switches from the default dark theme to the light theme, how do you detect which one is in use? In the MSDN article Theme Resources for Windows Phone, there are two interesting resource keys that you can query. These are the PhoneDarkThemeVisibility and PhoneLightThemeVisibility keys. So based on these we can determine if we should use a dark or light background image in our application, like so:

var isLightTheme = (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"];
var imageBrush = new ImageBrush();
var imageUri = new Uri(isLightTheme == Visibility.Visible ? "background-light.jpg" : "background-dark.jpg", UriKind.Relative);
imageBrush.ImageSource = new System.Windows.Media.Imaging.BitmapImage(imageUri);
MainPanorama.Background = imageBrush;

How to use CDN links in your ASP.NET MVC application to serve static content from a cookieless domain

Here’s one way you can use CDN links in your ASP.NET MVC application.

Problem:
When you develop your ASP.NET MVC application and use a link to a static content, such as css, jpg’s etc, you may want to server these from a CDN or at least a domain that doesn’t server cookies with the request. For those of you who have been using the Google Page Speed add-in for FireFox, you know what I’m talking about:

Untitled1

So the question is, how do you get your markup that normally may look something like this:


to look something like this on your production server:


When you are developing using the local web server (Cassini), you don’t want the CDN links in your code, but after you have published the page to prod, you want it to use the CDN link. One way to make this work is to make an extension helper method that creates the correct link based on if you run in dev or prod. Here is my suggestion:

public static MvcHtmlString CDNLink(this HtmlHelper html,
string tagName,
string contentTag,
string contentUrl,
object htmlAttributes)
{
var contentLink = new TagBuilder(tagName);
var contentServerUrl = ConfigurationManager.AppSettings["ContentServerUrl"];
contentServerUrl = string.IsNullOrEmpty(contentServerUrl) ? "{0}" : contentServerUrl;
contentLink.MergeAttribute(contentTag, string.Format(contentServerUrl, contentUrl));
contentLink.MergeAttributes((IDictionary<string, string="">)htmlAttributes, true);
return MvcHtmlString.Create(contentLink.ToString());
}

 

It is pretty straightforward to use:

<%=html.cdnlink("link", new="" string="">
{
{"rel", "stylesheet"},
{"type","text/css"}
}) %>

<%=html.cdnlink(“link”, new=”” string=””>

<%=html.cdnlink(“link”, new=”” string=””>

Pass the tag name (“link”), the content attribute (“href”), the actual content link (“file.css”) and optional HTML attributes. In your web.config file, add an application settings for the content server url like this in dev


And in prod, use something like this:

There you go, hope you will find it useful.

 

 

How to simulate the SQL WHERE IN clause in a SPQuery

Simulate a WHERE IN clause in a SPQuery

If you ever have used something like

SELECT * FROM table WHERE field IN ('value1','value2','value3','value4')

 

in T-SQL, and wanted to mimic this functionality in the SharePoint SPQuery class, you probably have found out there is no IN statement in the CAML Query language.
So what do do? Here’s a way to simulate WHERE IN clause in a SPQuery statement.

One solution is to use a LinQ query that looks something like this:

var values = new List {"value1", "value2", "value3"};
var listItems = from SPListItem tItem in list.Items
orderby tItem.Title ascending
where values.Contains(tItem["Title"].ToString())
select tItem;
foreach (var spListItem in listItems)
{
Console.WriteLine(spListItem.Title);
}

However, this has a serious performance problem. Because the .AllItems property will do a full T-SQL select all items behind the scenes, it can get very slow if you have thousands and thousand of items in your list.
It do look elegant, however.

Another way is to build up a CAML query that does an OR statement for every item that you have in your list of values.
Essentially, you will have to build up something that looks similar to this CAML query:

value1

value2

value3

 

Notice that the CAML syntax for multiple OR statements is a little bit odd. You would at first glance think that is would look like this:

value1

value2

value3

But this will throw an error. You will need to have nest the OR statements within each other.

For this purpose I have written a small helper method that will build the CAML query for a given list of values in a List of strings.
You call it like this:

SPQuery query = new SPQuery();
query.RowLimit = 2000;
query.ViewFields = "";
var whereClause = GetWhereClause("Title", "Eq", new List
{
"value1", "value2", "value3"
});
query.Query = string.Format(@"{0}", whereClause);
// Get the list items
SPListItemCollection items = list.GetItems(query);

The first parameter is the field that you want to comapre on, the second parameter is the type (“Eq” makes most sense in this scenario), and the third parameter is of List of strings.
Here’s the code for the helper method:

private static string GetWhereClause(string fieldName, string whereElement, IEnumerable list)
{
var sb = new StringBuilder();
var makeFirstEntry = true;
foreach (var entry in list)
{
var savedQuery = sb.ToString();
sb = new StringBuilder();
if (!makeFirstEntry)
{
sb.Append("");
sb.Append(savedQuery);
}
else
{
sb.Append(MakeStatement(whereElement, fieldName, entry));
makeFirstEntry = false;
continue;
}
sb.Append(MakeStatement(whereElement, fieldName, entry));
sb.Append("");
}
return sb.ToString();
}
private static string MakeStatement(string whereElement, string fieldName, string entry)
{
var sb = new StringBuilder();
sb.Append(string.Format("<{0}>", whereElement));
sb.Append(string.Format("{1}", fieldName, entry));
sb.Append(string.Format("", whereElement));
return sb.ToString();
}