Compartir a través de


FIM XPath using .Net client: Escape single quote

Technorati Tags: FIM,Xpath,.Net

Hi There,

I am Syam Pinnaka, Dev in IAM services team at Microsoft.

Forefront Identity Manager (FIM) supports querying its data using XPath. XPath queries can be constructed using standard XPath 2.0 specification. FIM XPath queries are a great way to query and fetch the required data from FIM but they can become tricky sometime as explained below.

Lets consider a simple xpath query to read a group with a display name. The xpath for this will look like below.

     "/Group[DisplayName = 'My Group1']";

When querying programmatically, we typically would like to parameterize this query and so the query will like below.

     
  string group = "my group";
 "/Group[(DisplayName = '" + group + "')]";

”group” is a variable which will contain the group name that you would like to query FIM for.

This query will work perfectly for most of the data values until the group that you are searching for contains a single quote. Example below.

     string group = "I have a single quote(')";
    "/Group[(DisplayName = '" + group + "')]";

Notice a single quote in group name, i.e. "I have a single quote(')" and xpath query will try to enclose this name with two additional quotes before the query is dispatched to FIM. Obviously this query will not fetch the group and fails with an exception saying ‘Cannot filter as requested’. That’s because the extra single quote (‘) in the display name will make the total number of single quotes in the xpath query as 3. The system will detect this condition and fail saying ‘Cannot filter as requested”.

Its good to understand what the issue is but how do you fix\work around it? There could be multiple ways to work around this and here is what that has worked for me. Just enclose the display name with double quotes instead of single quotes. i.e. The xpath filter will need to look like this.

     "/Group[(DisplayName = \"I have a single quote(')\")]";

We can parameterize the above query as shown below.

     string group = "I have a single quote(')";
    "/Group[(DisplayName = \"" + group + "\")]"
 Looks pretty simple, right? Well its only after I know the work around. :- )