Skip to content

Querying models

takerusilt edited this page Sep 14, 2016 · 4 revisions

If a data model class is created for content type, queries can be done through the SPModelManager class.

CamlExpression query = Caml.Equals("Title", "Sample Item");
SPModelManager<MyItem> manager = new SPModelManager<MyItem>(SPContext.Current.Web);
SPModelCollection<MyItem> c1 = manager.GetItems(query);
SPModelCollection<MyItem> c2 = manager.GetItems(query, "keywords");

Query locations

Query locations (i.e. items in which lists to be queries) can be easily done through different constructors of the SPModelManager class.

Constructor Location(s)
SPModelManager(SPWeb) Lists that has associated content types under this web and all sub-webs
SPModelManager(SPList) -or-
SPModelManager(SPWeb, IList<SPList>)
Specified lists

Query mode

The manager automatically chooses the best API to query items:

Scenario Query method
Multiple lists of different base template type -or-
Using overloads of GetItem() or GetCount() with keyword parameter
Keyword Search
Multiple lists of the same base template type Cross List Query
Exactly one source list List Query
No source list is founded No Query

List item metadata

When a model is fetched from SharePoint database, apart from the defined properties, there is a numerous of metadata available for the information of the list item.

They are exposed by the ISPModelMetaData interface from SPModel.GetMetaData().

Property Description
ID ID of the list item in a particular list
UniqueId Unique ID of the list item throughout the site collection
SiteId ID of the site collection which the list item belongs to
WebId ID of the site which the list item belongs to
ListId ID of the list which the list item belongs to
FileRef Server-relative URL of the list item
FileLeafRef File name of the list item
LastModified Last modified time of the list item
ContentTypeId ID of the content type of the list item
Version Version number of the list item
EffectivePermissions Permission the user has on the list item
CheckOutUserID ID of the user who checked out this list item; 0 if it is not checked out
HitHighlightSummary Only available when the model is returned through a FAST Search query

Search integration

Integration with FAST Search is all done by the library. This is no extra efforts needed neither when defining model classes nor querying models.

public abstract class MyItem : SPModel {
    [SPTextField("MyDescription")]
    public abstract string Description { get; set; }
}

SPModelCollection<MyItem> items = manager.GetItems(query, "keywords");
// library knows it has to look for "MyDescriptionOWSTXT" column 
// because the item is from search result
Console.Write(items[0].Description);

See more information on Search integration.

LINQ support

You can even use the LINQ interface.

SPModelManager<MyItem> manager = new SPModelManager<MyItem>(SPContext.Current.Web);
manager.Query().Where(v => v.Title == "Sample Item");

See more details on LINQ support.