Monday, June 23, 2014

Some time ago I got new PC with Windows 8.1. It was quite powerful (8 cores and 32Gb of memory), however I faced with unpleasant issue: over RDP it was very slow. Especially slow was Outlook 2013. It hang after each mouse click and working with it was quite painful.

I tried to check what may be wrong. Need to say that I use RDP over UAG, i.e. couldn’t change RDP properties like display size, color depth or optimize connection experience (those things which you may do when it is possible to save RDP icon to the desktop). So several possible directions of investigations were excluded automatically. The first thing which I tried was disabling of taskbar transparency which may cause problems with performance. In order to do it I used found Opaque Taskbar utility, however it didn’t give results. RDP still was very slow.

After that I tried another solution which I found in one of the forums: ran cmd as administrator and execute the following command:

netsh interface tcp set global autotuning=disabled

which supposed to disable TCP/IP auto tuning. And it solved the problem. After that Outlook 2013 started to work very fast over RDP and overall RDP performance was significantly improved.

Sunday, June 22, 2014

In the first part of the series we saw how users information which comes from SQL Server aspnetdb database can be retrieved by separate pages instead of retrieving all users and implement pagination in memory. Such approach gives us advantage in performance which will be crucial when amount of users is very big. In this part we will continue improving performance and will consider how we may attach additional information for each user returned from SQL Server from Sharepoint’s User information list (e.g. such data as Title, Created and Modified).

Let’s return to the code sample from the first part. The key part is those which retrieves appropriate page of information from the database:

In result we have DataTable object with Name, Email and Active columns and current page of users information. Now for each user in the current page we need to add additional information from Sharepoint: Title, Modified, Created. How to do it? The simples way is to perform CAML query for each user and add this data to the resulting DataTable. But it will lead to the classic n + 1 problem, when n users will cause n database queries plus optional 1 query for retrieving all users first. In turn it causes performance impact and all our efforts from the first part with retrieving paged information from SQL Server won’t have a lot of sense. Is there a better way to get data? Yes it is: instead of making many CAML queries for each user we may make single CAML query which will return data for all users returned from the database. This query is more complex: it should be built dynamically for each page of the users. E.g. if we have 3 users in the DataTable with names testuser1, testuser2 and testuser3 we need to built CAML query which will return data by the following conditions:
item[“Name”] == “testuser1” || item[“Name”] == “testuser2” || item[“Name”] == “testuser2”
which will be presented in CAML like this:

1:<Where>

2:<And>

3:<And>

4:<Eq>

5:<FieldRefName="Name"/>

6:<ValueType="Text">testuser1</Value>

7:</Eq>

8:<Eq>

9:<FieldRefName="Name"/>

10:<ValueType="Text">testuser2</Value>

11:</Eq>

12:</And>

13:<Eq>

14:<FieldRefName="Name"/>

15:<ValueType="Text">testuser3</Value>

16:</Eq>

17:</And>

18:</Where>

Also note that page size is not hardcoded parameter and may be also be different and that in the final page number of users may be less than page size. The bigger number of users will be retrieved from database, the bigger CAML tree will be.

In order to built CAML query we will use Camlex – open source library for building dynamic CAML queries. It is ideal for the mentioned requirements, i.e. when CAML query should be built in runtime base on some parameters. Before to use it we need to notice one thing: Name column in User information list in Sharepoint contains user name in form {membership provider name}:{user name}. Membership provider name is specified in web.config and can be retrieved by using SPIisSettings.MembershipProvider property. Let’s first see how CAML query is built:

At first we build CAML query (lines 19-35) with Camlex. E.g. if DataTable contains 20 users returned from SQL Server, it will build the following query:

1:<Where>

2:<And>

3:<Eq>

4:<FieldRefName="ContentType"/>

5:<ValueType="Text">Person</Value>

6:</Eq>

7:<Or>

8:<Or>

9:<Or>

10:<Or>

11:<Or>

12:<Or>

13:<Or>

14:<Or>

15:<Or>

16:<Eq>

17:<FieldRefName="Name"/>

18:<ValueType="Text">fba:testuser1</Value>

19:</Eq>

20:<Eq>

21:<FieldRefName="Name"/>

22:<ValueType="Text">fba:testuser2</Value>

23:</Eq>

24:</Or>

25:<Eq>

26:<FieldRefName="Name"/>

27:<ValueType="Text">fba:testuser3</Value>

28:</Eq>

29:</Or>

30:<Eq>

31:<FieldRefName="Name"/>

32:<ValueType="Text">fba:testuser4</Value>

33:</Eq>

34:</Or>

35:<Eq>

36:<FieldRefName="Name"/>

37:<ValueType="Text">fba:testuser5</Value>

38:</Eq>

39:</Or>

40:<Eq>

41:<FieldRefName="Name"/>

42:<ValueType="Text">fba:testuser6</Value>

43:</Eq>

44:</Or>

45:<Eq>

46:<FieldRefName="Name"/>

47:<ValueType="Text">fba:testuser7</Value>

48:</Eq>

49:</Or>

50:<Eq>

51:<FieldRefName="Name"/>

52:<ValueType="Text">fba:testuser8</Value>

53:</Eq>

54:</Or>

55:<Eq>

56:<FieldRefName="Name"/>

57:<ValueType="Text">fba:testuser9</Value>

58:</Eq>

59:</Or>

60:<Eq>

61:<FieldRefName="Name"/>

62:<ValueType="Text">fba:testuser10</Value>

63:</Eq>

64:</Or>

65:</And>

66:</Where>

After that we add data from Sharepoint to resulting DataTable (lines 46-62) by mapping rows by Name column and after that it contains data both form SQL Server and from Sharepoint. SPIisSettings object used in the example above can be retrieved with the following helper function (it is given from CKS.FBA open source project):

1:publicstatic SPIisSettings GetFBAIisSettings(SPSite site)

2: {

3: SPIisSettings settings = null;

4:

5:// try and get FBA IIS settings from current site zone

6:try

7: {

8: settings = site.WebApplication.IisSettings[site.Zone];

9:if (settings.AuthenticationMode == AuthenticationMode.Forms)

10:return settings;

11: }

12:catch

13: {

14:// expecting errors here so do nothing

15: }

16:

17:// check each zone type for an FBA enabled IIS site

18:foreach (SPUrlZone zone in Enum.GetValues(typeof(SPUrlZone)))

19: {

20:try

21: {

22: settings = site.WebApplication.IisSettings[(SPUrlZone)zone];

23:if (settings.AuthenticationMode == AuthenticationMode.Forms)

24:return settings;

25: }

26:catch

27: {

28:// expecting errors here so do nothing

29: }

30: }

31:

32:// return null if FBA not enabled

33:returnnull;

34: }

As you can see using of Camlex allowed us to solve the problem of retrieving data from Sharepoint quite gracefully. In our solution for optimizing performance we get only necessary page of users information from SQL Server and then perform single CAML query to the Sharepoint, which returns data for only selected users, and then merge information and show it in the listing. Without Camlex alternative way would be to use Linq 2 Sharepoint, but it would require more setup from us. From my point of view this is very nice example which shows how effective Camlex may be. I hope that this information will help you in your work and you will use Camlex in scenarios when need to build CAML queries dynamically.

About Me

I've created this blog for sharing my technical experience in software engineering. Most of posts will be dedicated to Sharepoint. But I will write also about another areas of software development for .Net platform. Hope it will be useful and will help you in your work.