Monday, 21 April 2008

LINQ (Language INtegrated Query)

LINQ is a very powerful feature brought to us by Microsoft in .net 3.0 or rather c# 3.0. When I first time knew about LINQ my understanding was that “Microsoft is trying to replace SQL with LINQ” but I was wrong. Here I will cover some basics of LINQ and few jargons which skirt around LINQ.

1. Till now we were able to query the tables but LINQ allows us to query Collection which has really made our life easier as well as it brings better understanding of the code.
For eg.

If you have a collection of products and your products are categorized into let’s say Electronic/Apparel/Toys etc.

Now let’s consider you want to retrieve one of the specific category product.

Now to achieve above task, probably we have to iterate through entire product collection then we have to out a check for our specific category and then we need to add it into our new collection.

Something similar to following code snippet.

List<Products> Toys = new List<Products>();
foreach (Product p in Products)
if (p.TYpe == "Toys")

Now you can achieve above task in much easier way something like..

var Toys = from p in Products
where p.Type == "Toys"
select new {p.ID,,p.cost,p.qty};

2. Till now, using the conventional methods we were just writing a lots of code for few task related with collection, now this can be reduced extensively using Lambda and Extension methods

public int GetSalesByAreaCode(int AreaCode)
int totalSales = 0;

foreach (Sale saleItem in this.Sales)
if (saleItem.AreaCode == AreaCode)

return totalSales;
But if you are aware of LINQ's Lambda and Extention method you can achive above task in few lines of code.

public int GetSalesByAreaCode(int AreaCode)
return this.Sales.Count(saleItem => saleItem.AreaCode == AreaCode);

If you add a reference of System.Linq then following method is automatically added in all the arrays,generic list or any of the "IEnumerable" class.


above functions are pretty much like our SQL functions.

I hope, It would have given you enough curiosity to read up about LINQ

Wednesday, 19 March 2008

How to include custom columns in SharePoint Search

SharePoint search has few default columns already mapped. In case you are creating your own List and added few columns and you want SharePoint Search engine to include that column in Search result, you have to do following settings.

  1. Go to Central Administration.
  2. Under Shared Services Administration click on your SSP name.
  3. Go to Search -> Search Settings
  4. On "Configure Search Settings" page click on the "Metadata property mappings" settings.
  5. Click on "New managed Property"
  6. Choose the data type of your column in the List (Text/Integer/Decimal/Date Time/YesNo)
  7. Click on the Add mapping and Give few characters of your column name so that SharePoint can search that particular column.
  8. Now choose the column and click Ok.
  9. Now you can find your column in "Managed Properties View" and you can use this Mapped name in your select query.

Note: I had faced one problem while performing above steps. In step 7 while adding the column I was not able to find my column and after struggling a lot I found the solution for the same. Actually when you add any column in the List and if that column doesn't have any data In step 7 that column will not be found so to overcome this issue , before doing above steps make sure you have some data in the field you are interested in mapping in search also make sure after adding the data you have ran the "Full crawl" the site once.

Thursday, 13 March 2008

Using dynamic web reference in sharepoint Search Web service

Microsoft Office SharePoint Server 2007 provides two ways to enable search in your application

1. Query object model
2. Query web service

You can learn about them from here

What is the need of Dynamic Web reference ?

In one of my project, we are using SharePoint and we have enabled search in our application. We are using SharePoint Search web service for Search functionality. While development we add the web reference of the Search Service from our own machine( In my case MOSS 2007 is installed on my machine) i.e. http:///_vti_bin/search.asmx and search proxy class will use this information only. So the search will work on my machine but it fails on Production machine/server since it couldn’t find this used machine name ie <> . So to avoid this situation we can have something called dynamic Web reference and we can pass this information (http:///vti_bin/search.asmx) dynamically to our proxy class . Following are few very simple steps –

1.After adding the Web reference of the Search.asmx in your application, change the “URL behavior” property to “Dynamic”.

2.Create the instance of the QueryService class.

3.Change the URL property of the QueryService class to the your actual Service location.

Code Snippet:

public DataSet GetSearchResult()
string qXMLString = "<QueryPacket xmlns='urn:Microsoft.Search.Query'><Query><SupportedFormats><Format revision='1'>urn:Microsoft.Search.Response.Document:Document</Format></SupportedFormats><Context><QueryText language='en-us' type='MSSQLFT'>SELECT Title,Path,SiteName,ITEMID FROM Scope() WHERE FREETEXT(DEFAULTPROPERTIES,'SearchText') ORDER BY Rank </QueryText></Context><Range><StartAt>1</StartAt><Count>5000</Count></Range></Query></QueryPacket>"// SearchService is the Web Reference Name
SearchService.QueryService Search = new SearchService.QueryService();
Search.Url = "http://ServerName:portName/_vti_bin/search.asmx";
// You can also pick above search reference from your web config file in
// order to make it flexible.
DataSet SearchResult = Search.QueryEx(qXMLString);
return SearchResult;


Query List/Document Library in Specific Folder

To query SharePoint List or Document Library in specific Folder “ FolderServerRelativeUrl ” as part of the CAML Query Code Snippet ...