Sunday 18 September 2011

Get all items in your sharepoint site using object model


I had a requirenment earlier to get all the items in my sharepoint site despite of the list or library they exist in.

I could do this by looping through various list\libraries in my site but since i only wanted to get the items or say item Titles, i choose to use SPSitedataquery with viewfields as 'Title'. Here if you do not specify the Lists tag you can retrieve items from the whole site as a Datatable.

An example -

SPSiteDataQuery query = new SPSiteDataQuery();

query.ViewFields = "<FieldRef Name='Title' />";

query.Query = "<Where><Contains>" +
"<FieldRef Name='Title' /><Value Type='text'></Value>"+
"</Contains></Where>";

query.Webs = "<Webs Scope='SiteCollection' />";

DataTable dt = SPContext.Current.Web.GetSiteData(query);

Ads by Google

No comments:

Post a Comment