Using search to find recently modified files in SharePoint 2010
Posted
Thursday, December 8, 2011 2:15 PM
by
CoreyRoth
Last week, I posted about how we can extend CoreResultsWebPart to show documents created by the current user. Using this same technique, we can also use search to find recently modified documents. If you are a long time follower of my blog, you might remember that I posted how to do with using the Wildcard Search Web Part in SharePoint 2007. In an effort to keep posts updated, I thought I would post a follow up on how we can do the same thing in SharePoint 2010. Since the CoreResultWebPart has changed quite a bit, the solution is quite a bit cleaner and still only requires two lines of code. Again, we are going to create a web part that inherits from CoreResultsWebPart as described in my Wildcard Search Web Part for SharePoint 2010 post.
The first thing we need to do is change the QueryNumber. By default it is set to Query1 (or UserQuery back in 2007) which meant that it was expecting input from the user. In this case, I just change it to Query2.
this.QueryNumber = QueryId.Query2;
Then we just need to construct a query using the Write keyword. I’ve explained its use before in my handy keywords post. It’s syntax is usually something like this. Note that there are no spaces anywhere in the string below.
Write>=”12/1/2011”
Of course this would hard code the date and we don’t want that. So what I do instead is use DateAdd and subtract a number of days. In my case, I want to show all documents from the last seven days. We assign this query to the FixedQuery property. Here is what it looks like.
this.FixedQuery = string.Format("Write>=\"{0}\"", DateTime.Today.AddDays(-7));
Compile your code and deploy it and then you are good to go. Just use this web part in lieu of the regularCoreResultsWebPart. Here’s what it looks like on my page.
That’s all there is to it. One addition, you might want to make to your code is to allow the number of days to be user configurable. You can easily make a web part property that allows the user to specify the number of days.