Friday, December 23, 2011

In one of my previous posts I showed how to build dynamic CAML queries based on query string parameters with Camlex.Net – open source tool for creating CAML queries via C#. I used WhereAll() and WhereAny() methods of the IQuery interface which allows to combine several conditions using AND (&&) and OR (||) logical operations. But what if you need to build your query using more complex rules and it should contain both AND and OR operations? Of course you may combine expressions using different logical operations by yourself, but it will require more work from you. For such scenarios Camlex contains special class which you can use for building complex dynamic queries: ExpressionsHelper. It contains 2 useful methods CombineOr() and CombineAnd():

1:publicstaticclass ExpressionsHelper

2: {

3:publicstatic Expression<Func<SPListItem, bool>> CombineAnd(

4: IEnumerable<Expression<Func<SPListItem, bool>>> expressions)

5: {

6: ...

7: }

8:

9:publicstatic Expression<Func<SPListItem, bool>> CombineOr(

10: IEnumerable<Expression<Func<SPListItem, bool>>> expressions)

11: {

12: ...

13: }

14: }

They are very similar to WhereAll() and WhereAny() methods of IQuery. The difference is that IQuery methods return instance of IQuery (as we use fluent interfaces in Camlex), while methods of ExpressionsHelper return expression which is result of combining specified in parameter expressions using AND or OR. Actually WhereAll() and WhereAny() are implemented using CombineOr() and CombineAnd():

As you can see we use ExpressionsHelper in order to combine list of expressions passed to the method and then just call regular Where() method which receives single combined expression.

Ok, after we checked how it is implemented let’s see how it works in real life scenarios. Suppose that we need to retrieve all documents which with Russian or English languages (metadata field Language is equal to English or Russian), and of appropriate type which is specified dynamically – Word, Excel, etc. I.e. we don’t know what types will be specified at compile time – they will be passed in run-time and we need to build dynamic query based on them.

At lines 1-5 we create condition: Language = Russian or Language = English). Then on lines 7-15 condition for extensions. As you can see extensions are specified in the list, so they can be retrieved e.g. from settings storage and size of this list may change. In all cases Camlex will build correct CAML (the only case which you need to handle is when list will no contain any elements, but this exercise is out of scope of current post). And then we combine both expressions using AND operation – lines 17-21. As result we get the following CAML:

1:<Where>

2:<And>

3:<Or>

4:<Eq>

5:<FieldRefName="Language"/>

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

7:</Eq>

8:<Eq>

9:<FieldRefName="Language"/>

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

11:</Eq>

12:</Or>

13:<Or>

14:<Or>

15:<Contains>

16:<FieldRefName="FileLeafRef"/>

17:<ValueType="Text">.docx</Value>

18:</Contains>

19:<Contains>

20:<FieldRefName="FileLeafRef"/>

21:<ValueType="Text">.xlsx</Value>

22:</Contains>

23:</Or>

24:<Contains>

25:<FieldRefName="FileLeafRef"/>

26:<ValueType="Text">.pptx</Value>

27:</Contains>

28:</Or>

29:</And>

30:</Where>

If you add new extension (e.g. pdf) Camlex will rebuild query automatically:

1:<Where>

2:<And>

3:<Or>

4:<Eq>

5:<FieldRefName="Language"/>

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

7:</Eq>

8:<Eq>

9:<FieldRefName="Language"/>

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

11:</Eq>

12:</Or>

13:<Or>

14:<Or>

15:<Or>

16:<Contains>

17:<FieldRefName="FileLeafRef"/>

18:<ValueType="Text">.docx</Value>

19:</Contains>

20:<Contains>

21:<FieldRefName="FileLeafRef"/>

22:<ValueType="Text">.xlsx</Value>

23:</Contains>

24:</Or>

25:<Contains>

26:<FieldRefName="FileLeafRef"/>

27:<ValueType="Text">.pptx</Value>

28:</Contains>

29:</Or>

30:<Contains>

31:<FieldRefName="FileLeafRef"/>

32:<ValueType="Text">.pdf</Value>

33:</Contains>

34:</Or>

35:</And>

36:</Where>

This is how ExpressionsHelper helps you build complex dynamic CAML queries. Recently I used it by myself in one of the project in order to create very complex query – and it really simplified the task. So we use our project also very intensively in the every day development.

Wednesday, December 14, 2011

If you use cache (I’m talking about not only standard ASP.Net cache, but cache in general) in the Sharepoint web application, you should keep in mind that users with different permissions may see different content on the site. E.g. if you crawl site and all its sub sites for the documents which are tagged by particular managed metadata term in the custom web part and want to speed up the process by adding caching, you should wonder does current user have access to the particular document before to show the link to this document even if it comes from the cache. Often developers forget about it.

Consider the following example: user A with administrative permissions comes on site – web part crawls the sub sites, stores result in the cache and display them in UI. Then user B with reader permissions comes on the same site. If web part will use cache which was filled for user A, then user B may see documents which he can’t access. The issue can be solved by adding SPContext.Current.Web.CurrentUser.ID to the cache key, so different users will have own cache. If you wonder about amount of users, you may use more common identifier – e.g. group id for he users with similar permissions. In this case users within one group will use the same cache. For those users which belong to several groups – you should rather create cache for each possible combination of the groups or use cache of the most powerful group.

Sunday, December 11, 2011

In one my previous post I wrote how to use Telerik Rad Editor Lite for Firefox (and other than IE browsers). Here we will extend the control which we created in order to use it in all sites without additional actions from your side. In order to use Telerik Rad Editor you need to activate 2 features:

Use RadEditor to edit List Items (located in RadEditorFeature)

Use RadEditor to edit List Items in Internet Explorer as well (located in RadEditorFeatureIE)

First feature during activation copies new rendering template for RichTextField with Telerik control which replaces default Sharepoint editor for html fields (copying is done in feature receiver). Second feature doesn’t have feature receiver – instead it has activation dependency on 1st feature and used as a flag (see below).

Control checks that these features are activated in the OnLoad() method:

If we want to use Telerik editor on all sites without activating the features, we need to override ShowEditorOnPage() method and always return true.

In the previous post we already created custom control which inherits RadHtmlListField. Now we need extend it – override OnLoad() method. As it access private variables and methods we need to reuse reflection in order to simulate work of the RadHtmlListField. Mostly all places where it access private variables can be solved with basic reflection, but not all. See that it calls base.OnLoad() method (line 5), i.e. it calls OnLoad() method of the base class MOSSRadEditor. As we have our own class which inherits RadHtmlListField, then inheritance chain looks like this:

AllBrowsersHtmlListField –> RadHtmlListField –> MOSSRadEditor

In our AllBrowsersHtmlListField we want to call MOSSRadEditor.OnLoad(), but not RadHtmlListField.OnLoad() – it means that we can’t call base.OnLoad(). I.e. we need to skip one tier in the inheritance chain and call virtual method OnLoad(), but not from base class, but from base of base class. And this is not trivial task because it means that we need to avoid the rules of the underlying programming language.

Investigation of this problem showed that problem is really not simple. But in this forum thread I found interesting mention that DynamicMethod can be used for this. The idea is that we will build new method in runtime using IL instructions and then call it. In our case it will look like this:

At first we get a reference to the MOSSRadEditor.OnLoad() method (lines 3-6). Then we create new DynamicMethod (BaseBaseOnLoad) and specify in parameters type of AllBrowsersHtmlListField (type of object which is used for calling) and EventArgs which is passed to the OnLoad() method. Then using IL generator we create method’s body (lines 10-13). Note that on line 9 we construct call to the method which actually makes trick – because this call will go to the MOSSRadEditor type. Then we create delegate and call it with parameters which we specified earlier (lines 15-17).

It was complex part. The rest of things can be done with basic reflection. We will need extended ReflectionHelper class:

It works in FF (and you can extend it also for other browsers) and doesn’t require features activation. Note that it will be used on all sites in your farm because rendering template is used globally by Sharepoint. However you can easily to change this behavior – check url of the current web application in the NewShowEditorOnPage() method and decide whether or not you need to use Telerik control.

This post showed that how knowledge of advanced language features may help in applied Sharepoint development. Hope it will help you in your work.

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.