Creating a custom view that filters on the current user or [Me]

 

Earlier this week I was creating custom list views for a customer solution. On of the specifications was that the view needed a filter that only showed the documents that were created by the current user. You can do this using the user interface by specifying that the Created By field needed to be equal to [Me].

 

Filter view by current user

 

I was trying to get this same behaviour by adding a CAML query to my view  using the following code:

 

StringCollection viewFields = new StringCollection();
viewFields.Add("Title");
viewFields.Add("Description");
SPView newView = myDocLib.Views.Add("My documents", viewFields, "<Where><Eq><FieldRef Name='Author' /><Value Type='User'>[Me]</Value></Eq></Where>", 100, true, false);

 

This didn't work, but when I opened the view and saved it again it did work. I tried to search the internet by looking for "sharepoint, spview, [Me]" and I couldn't find a soluton, which is the reason for this post.

Eventually I posted my question on the MSDN forums http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/a4c20e84-4201-4106-9568-df3cfbcd5d3b/ and after only a couple of minutes Dave Hunter  came back with the answer. The correct way to define a view that filters based on the current user using the object model is:

 

StringCollection viewFields = new StringCollection();
viewFields.Add("Title");
viewFields.Add("Description");
SPView newView = myDocLib.Views.Add("My documents", viewFields, "<Where><Eq><FieldRef Name='Author' /
><Value Type="Integer"><UserID Type="Integer"/></Value></Eq></Where>", 100, true, false);

 

Dave also gave the great tip that you can find the CAML for a specific view by doing the following:

  • Define the view using the user interface
  • Save the list as a template
  • Download the template
  • Rename the .stp file to .cab
  • Extract the manifest.xml from the .cab file and find the <Query> definition

 

Thanks again Dave!

Comments -
  1. Gravatar

    Hi,
    I've looking for information regarding document library views and you seem to know a fair bit about them :)
    Would you know what is the maximum number of views I can created for one document/list library?
    Thanks

      
  2. Gravatar

    Hi Lux,

    There are no official recommendation on the maximum amount of views per library. But how many would you like to use? I've used up to 10 and even that seems like a lot to be useful. 10 was no problem performance-wise..

    Mirjam

      
  3. Gravatar

    Hi,
    I have a document library "Resumes" for which a view is created with filter created by [Me], it works fine for specific users but if user other click on All Document view he/she are able to view the resumes uploaded by other users. I want to hide the All Document view to other than admin user in my site. How can I do it.

      
  4. Gravatar

    Hi Poonam,

    You can't secure a view in SharePoint, you can only secure a list. The only thing you could do is create an eventreceiver that breaks security on each item and makes sure that only the user who's resume it is and the admin had access to the item. If you have a large list you have to be careful with this though as the number of security scopes that you have on a list is limited to about 1000.

    Mirjam

      
  5. Gravatar

    Hi Mirjam,

    Thanks for the reply....
    As we can't have security on view I have created another document library which will have only admin rights & whenever other users add document to users library using event recevier it will get added to admin document library. Is it a right way to do? or any other option is there?

      
  6. Gravatar

    Hi Mirjam,

    can you send me the sample code for ItemAdded Event Receiver to add document from users to admin document library?



    Thanks

      
  7. Gravatar

    Hi Poonam,

    Creating a second library could be a valid solution, it all depends on the number of documents, the number of users and the exact requirements. It is hard to give solid advice based on information that you can post in a blog comment.
    I'm sure you can find plenty of examples on the internet that explain how to use the ItemAdded event receiver.

    Regards,
    Mirjam

      
  8. Gravatar

    Hi,

    I have a form library in SP 2010 for Time off requests where i have created a view for users to only see their requests. The problem is, using the created by column may fail in the event that a manager or another user submits the request on behalf of someone. I have a username column but when i try to use the [Me] filter with 'username' it does not allow it. How can i do this?

      
  9. Gravatar

    Thank you :-)

      
  10. Gravatar
    Ken

    Hello, very useful! Can you tell me how I can use this function in the default edit list form. I've tried everything and the form will not filter on this custom view (even though I have it set as the default edit form). Please help if possible.

      

Leave a Reply

 


Please add 7 and 2 and type the answer here: