Using SharePoint Search to display the documents of the current user

Posted Friday, December 2, 2011 11:43 AM by CoreyRoth

I see requests for this in the SharePoint forums a lot and I always figured it was pretty easy to do with a few lines of code.  Sometimes users want to display a list of documents that the worked on throughout the farm.  The CoreResultsWebPart has the ability to execute fixed static queries.  However, there is no way for you to specify the current user in the query.  I had a need for this myself soon so I thought I would try it out and see if I could implement it.  It turns our this can be done with two lines of code.

To implement this we need to create a class that inherits from CoreResultsWebPart.  I am doing this in the exact same way I built the Wildcard Search Web Part for SharePoint 2010.  You can download that web part from CodePlex if you want to use it as a starting point.  What we want to do is set the FixedQuery property.  Normally when we do query manipulation, we do it after it has been submitted by the user with GetXPathNavigator().  We don’t need to override that method in this case.  Instead, I just put the code I need in the constructor.

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;

Next, we write a query using the Author keyword.  I’ve explained its use before in my handy keywords post.  It’s syntax is usually something like this.

Author:”Display Name”

So for example, to search for my documents I would use:

Author:”Corey Roth”

We want to put the user’s name in the Author query dynamically, so we look it up using SPContext.Current.Web.UserQuery.  We then just use the keyword syntax above and assign it to the FixedQuery property.

this.FixedQuery = string.Format("Author:\"{0}\"", SPContext.Current.Web.CurrentUser.Name);

Compile your code and deploy it and then you are good to go.  Just use this web part in lieu of the regular CoreResultsWebPart.  Here’s what it looks like on my page.

SearchMyDocuments

Try it out and let me know if you have any questions.

@coreyroth

Comments

# SharePoint Daily » Blog Archive » IT Trends that Could Affect SharePoint; Microsoft Discovers Openness; Special Discount for O’Reilly Training Courses

Pingback from  SharePoint Daily  » Blog Archive   » IT Trends that Could Affect SharePoint; Microsoft Discovers Openness; Special Discount for O’Reilly Training Courses

# IT Trends that Could Affect SharePoint; Microsoft Discovers Openness; Special Discount for O'Reilly Training Courses

Monday, December 5, 2011 8:16 AM by SharePoint Daily

Do we really need a reason to go online ? - Dooley Top News Stories 3 Trends in IT That Could Affect

# IT Trends that Could Affect SharePoint; Microsoft Discovers Openness; Special Discount for O’Reilly Training Courses – Bamboo Solutions

Pingback from  IT Trends that Could Affect SharePoint; Microsoft Discovers Openness; Special Discount for O’Reilly Training Courses – Bamboo Solutions

# IT Trends that Could Affect SharePoint; Microsoft Discovers Openness; Special Discount for O’Reilly Training Courses – Bamboo Solutions

Pingback from  IT Trends that Could Affect SharePoint; Microsoft Discovers Openness; Special Discount for O’Reilly Training Courses – Bamboo Solutions

Leave a Comment

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