tags:

views:

650

answers:

3

I am using code like the following to get a list of all documents in a site with SPSiteDataQuery however, it is returning all sorts of documents that were not uploaded by the user. What is the best way to filter the list to only those documents that a user would have uploaded?

I am suspecting that there is a query that I can use but I am not sure what FieldRef I should be using to find only user uploaded documents (not hidden or system type document files).

see * below in the method

thanks

        protected DataTable GetListDataSPSiteDataQuery(string siteUrl, bool recursive, ref string error) {
            DataTable results = null;

            using (SPSite site = new SPSite(siteUrl)) {
                SPWeb web = site.OpenWeb();
                SPSiteDataQuery query = new SPSiteDataQuery();

                //query.Webs = "<Webs Scope='SiteCollection' />"; //query all web sites in site collection
                if (recursive)
                    query.Webs = "<Webs Scope='Recursive' />";


                query.Lists = "<Lists ServerTemplate='101' Hidden='FALSE' MaxListsLimit='0' />";
                //query.Lists = "<Lists BaseType='1' MaxListsLimit='0' />"; //document library only (0=generic list, 1=doc library,3=discussino forum,4-vote or survey,5=issues list)

                // *** can i provide a query here to filter for the documents i am interested in?
                query.Query = string.Empty; 

                //query.Query = "<Where>" + 
                //                    "<Gt>" +
                //                        "<FieldRef Name='File_x0020' />" +
                //                        "<Value Type='Number'>0</Value>" +
                //                    "</Gt>" +
                //                "</Where>";

                query.ViewFields = _viewFields;


                results = web.GetSiteData(query);
            }
            return results;
        }
A: 

Try:

query.Query = @"<Where>
      <Eq>
         <FieldRef Name='Author' />
         <Value Type='User'>Bob Smith</Value>
      </Eq>
   </Where>";
zincorp
I am sorry maybe I didn't explain correctly. I would like to get sharepoint documents that any user may have uploaded not the tons of hidden and documents that sharepoint uses. Thanks
David
Then don't downvote, if you didn't explain correctly.
Janis Veinbergs
Yeah what's up with the downvote for not explaining the question correctly?
zincorp
I just wanted to move it down the list not down vote. I did try to make 0 or 1 but I now can't change status unless you edit your response.
David
+1  A: 

Feature like CAML query builder will help you build valid queries.

Janis Veinbergs
Will the tool allow you do test queries across the sites? (i understand it will help develop the query which will be good however I am trying to build the query to return the documents across the sites and only those documents that have been uploaded/modified by any real user (not sharepoint). Thanks
David
Probably it will not allow to test queries accross sites. But i don't know for sure. And that is not important if you want to test your query - if your query is correct for 1 list, it will be correct for all lists of that type.
Janis Veinbergs
See reason and what happened regarding the downvote above.
David
+1  A: 

I'm not on the office right now to test but try adding the following fields to the result (so you can see if they create a filterable pattern)

  • ContentType (look for the obnoxious ones, like _Hidden)
  • CreatedBy (Author should be SHAREPOINT\system for non-user files)

I will try to get a test going if this doesn't help, but I do not think there's the big elephant in the room that says ONLY_USER_CREATED besides those properties. If the "all sorts of documents not uploaded by the user" are ONLY the /forms/ folder files (AllItems.aspx, EditItem.aspx etc) it can probably be worked around with a few Contains tests.

F.Aquino
Also you can just exclude system account from created by field by using <Neq></Neq> (Not equals).And also, as mentioned, use <BeginsWith><FieldRef Name='ContentTypeId'><Value Type='Text'>0x0101</Value></BeginsWith>. (use any content type id there, to query only specific type of documents, excluding system stuff like masterpages). CT Hierarchy can be seen here:http://msdn.microsoft.com/en-us/library/ms452896.aspx
Janis Veinbergs