0

I've got a CamlQuery that traverses the /public/ folder and returns all files that match a certain criteria and it works gorgeously. Now I need it to do the same for a separate library but have the results returned in the same instance.

Code

 private static SPListItemCollection retrieveDocs(SPWeb web, String meta)
        {
            SPQuery query = new SPQuery();
            query.ViewXml = "<View Scope='Recursive'><Query><Where><Contains><FieldRef Name='Key_x0020_Topics'/><Value Type='SPFieldLookupMulti'>" + meta + "</Value></Contains></Where></Query></View>";
            SPList docLib = web.Lists["public"];
            SPListItemCollection files = docLib.GetItems(query);

            return files;
        }

Can I make my query traverse both the public and secure folder and then return the files as one Collection?

Folders

7

Based on your description, it's possible only If there is a relation between the first and second library. so you will be able to use List Join CAML Query to return the related data from two libraries in one SPListItemCollection


But I don't think you have this case, so Alternatively, you can merge multiple SPListItemCollection to a Data Table as the following:

First, Define only one function that accepts three parameters with the SPList Object.

private static SPListItemCollection retrieveDocs(SPWeb web, String meta,SPList list)
        {
            SPQuery query = new SPQuery();
            query.ViewXml = "<View Scope='Recursive'><Query><Where><Contains><FieldRef Name='Key_x0020_Topics'/><Value Type='SPFieldLookupMulti'>" + meta + "</Value></Contains></Where></Query></View>";

            SPListItemCollection files = list.GetItems(query);

            return files;
        }

Second, call the function for each list and set each result in an independent site collection.

First Collection

SPList docLibpublic = web.Lists["public"];
SPListItemCollection  SPLCol1 = retrieveDocs(web, meta,docLibpublic);

Second Collection

SPList docLibsecure = web.Lists["secure"];
SPListItemCollection  SPLCol2 = retrieveDocs(web, meta,docLibsecure);

Now, Convert each SPListItemCollection to data table then merge the data tables

DataTable dtCol1 = SPLCol1.GetDataTable();
DataTable dtCol2 = SPLCol2.GetDataTable();
dtCol1.Merge(dtCol2);

Finally, you have data table (dtCol1) that contains the items from multiple SPListItemCollection


Note: You can use SPSiteDataQuery to combine two lists. but unfortunately, the returned collection cannot be parsed to SPListItemCollection

  • Thanks! is there an easy way now to convert dtCol1 back into a SPListItemCollection? – PT_C Nov 13 '17 at 13:46
  • @PT_C I stay 2 hours before adding my answer to convert data table to SPListItemCollection but unfortunately, no solution from me or suggested from internet worked for me. – Mohamed El-Qassas MVP Nov 13 '17 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.