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.

SearchRecentDocuments

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.

Comments

# SharePoint Daily » Blog Archive » Measuring the Success of Your SharePoint Implementation; Should Gates Return to Microsoft?; Save 10% on Bamboo Suites

Pingback from  SharePoint Daily  » Blog Archive   » Measuring the Success of Your SharePoint Implementation; Should Gates Return to Microsoft?; Save 10% on Bamboo Suites

# Measuring the Success of Your SharePoint Implementation; Should Gates Return to Microsoft?; Save 10% on Bamboo Suites

Friday, December 9, 2011 8:20 AM by SharePoint Daily

Friends don't let friends text and drive . - Dooley Top News Stories 5 Tips for Measuring the Success

# Sharepoint Updates December-09-2011 | SDavara's Sharepoint Knowledge Center

Pingback from  Sharepoint Updates December-09-2011 | SDavara's Sharepoint Knowledge Center

# Override Core Results Web Part With Custom Query « Sladescross's Blog

Pingback from  Override Core Results Web Part With Custom Query « Sladescross's Blog

# Override Core Results Web Part With Custom Query « Sladescross's Blog

Pingback from  Override Core Results Web Part With Custom Query « Sladescross's Blog

# re: Using search to find recently modified files in SharePoint 2010

Friday, December 16, 2011 2:07 AM by Mario

Hi Corey, we tried to create a similar page. But we have tried to use response headers as managed properties. We have found no solution to do so. Did you figured out something like this? Best regards. Mario

# re: Using search to find recently modified files in SharePoint 2010

Thursday, January 5, 2012 3:59 PM by CoreyRoth

@Mario I haven't tried it before.

# re: Using search to find recently modified files in SharePoint 2010

Wednesday, November 7, 2012 12:00 AM by Petro

Hi Corey,

Love the work you have done over the years!

I just have a very basic question for you... were the 2 additional lines of code -

this.QueryNumber = QueryId.Query2;

this.FixedQuery = string.Format("Write>=\"{0}\"", DateTime.Today.AddDays(-7));

In addition to the existing QueryManager code?

i.e.

                   this.QueryNumber = QueryId.Query2;

                   this.FixedQuery = string.Format("Write>=\"{0}\"", DateTime.Today.AddDays(-7));

                   QueryManager queryManager = SharedQueryManager.GetInstance(this.Page).QueryManager;

                   queryManager.UserQuery = string.Format("{0}*", queryManager.UserQuery);

I'm just not sure how to get this working.. ideally I'd like to hard code the query string via the 'this.FixedQuery' property, so if you could shed some light on what I'm doing wrong that would be great!

Note: The hard coded query could be as simple as this.FixedQuery = "john smith'

Thanks!

# re: Using search to find recently modified files in SharePoint 2010

Wednesday, November 7, 2012 10:40 AM by CoreyRoth

@Petro Thanks!  You can actually do it either way.   You can either set the QueryNumber and then use the FixedQuery property.  Or as an alternative you can get a reference to the QueryManager object and set the UserQuery property.  I hope that helps.

# SharePoint Core Results Web Part Changes « Tom Resing's SharePoint Blog

Pingback from  SharePoint Core Results Web Part Changes «  Tom Resing's SharePoint Blog

# Measuring the Success of Your SharePoint Implementation; Should Gates Return to Microsoft?; Save 10% on Bamboo Suites – Bamboo Solutions

Pingback from  Measuring the Success of Your SharePoint Implementation; Should Gates Return to Microsoft?; Save 10% on Bamboo Suites – Bamboo Solutions

Leave a Comment

(required)
(required)
(optional)
(required)