Working with SharePoint 2010 external lists

When working with SharePoint lists you need to go lookups and queries pretty much on a regular basis. One of the ways that you could do a query with SPList is using GetSiteData method, something that looks like this:

private DataTable GetData(SPWeb web, CrossListQueryInfo query)
        {
            DataTable resultTable = null;

            CrossListQueryCache queryCache = new CrossListQueryCache(query);

            switch (QueryWebScope)
            {
                case QueryWebScope.SiteCollection:
                    resultTable = queryCache.GetSiteData(web.Site, CrossListQueryCache.ContextUrl());
                    break;
                case QueryWebScope.Recursive:
                    resultTable = queryCache.GetSiteData(web);
                    break;
            }

            return resultTable;
        }

But this doesn’t seem to work for external lists. Luckily, good old GetItems(SPQuery) still works. Notice you will have to use SPQuery and not CrossListQueryInfo:

private DataTable GetData(SPWeb web, SPList list, SPQuery query)
        {
            DataTable resultTable = null;
            resultTable = list.GetItems(query).GetDataTable();
            return resultTable;
        }

Good luck with your querying!

This entry was posted in sharepoint, sharepoint 2010 and tagged , , , , , . Bookmark the permalink.

Comments are closed.