Tuesday, May 1, 2012

Get items from multiple webs using SPSiteDataQuery

I know there is no easy way to get list items from multiple webs using SPSiteDataQuery. But I had to work on a similar situation for one of my projects. Looking deep into the list items attributes I came to know that there is a possibility to do the filtering based on the web itself (given some other small conditions)

Properties "ServerUrl" (server Relative Url) and "EncodedAbsUrl" (Encoded Absolute Url) cannot be used since the field values are of type "Computed" and SPSiteDataQuery does not filter based on "Computed" field values (there are some workarounds for some computed fields as well)

So I started looking into "FileRef" (URL Path) and "FileDirRef" (Path) for filtering. It seems to be working for me.

For a particular SPFile/SPListItem the above field values would give the exact location of the item.the values would look similar like below:


Publishing Content Types:
FileDirRef: SubSite1/SubSite12/Pages
FileRef: SubSite1/SubSite12/Pages/default.aspx

Non-Publishing Content Types: (normal SPListItems)
FileDirRef: SubSite1/SubSite12/Lists/{ListRootFolderUrl}
FileRef: SubSite1/SubSite12/Lists/{ListRootFolderUrl}/{ItemName}

Using the above field values and with proper CAML filter xml, items can be filtered from multiple webs.

In my case I was querying against non-publishing content types. So I was applying ContentTypeId filter primarily to filter based on content type. Above that, I was applying filters for "FileRef" values to get the items from multiple webs.

Sample CAML Query would look like this:
(The first one with value "Lists" is to filter the items in the root web)
In addition to the above filters, I was also using the "ContentTypeId" filter to get list items of particular type.

NOTE: If you are using just "SubSite1/SubSite12" to filter on the items, it would include the sub webs as well. Include the "/Lists" or "/Pages" at the end of web relative url to restrict to a single site.

If you have any questions, please add a comment.

Thanks
Senthil S

No comments:

Post a Comment