SPQuery Examples
// Get SiteColl
SPSite curSite = new SPSite("http://myPortal");
//Get Web Application
SPWeb curWeb = curSite.OpenWeb();
// Create a SPQuery Object
SPQuery curQry = new SPQuery();
//Write the query (I suggest using U2U Query Bulider
curQry.Query = "<Where><Eq><FieldRef Name='Category' />
<Value Type='Text'>
SP2007 </Value></Eq></Where>";
// Set the Row Limit
curQry.RowLimit = 100;
//Get the List
SPList myList = myWeb.Lists["ListName"];
//Get the Items using Query
SPListItemCollection curItems = myList.GetItems(curQry);
// Go through the resulting items
foreach (SPListItem curItem in curItems)
{
string ResultItemTitle = curItem["Title"].ToString();
}
Query on DateTime Field - Query to Find Items in the List with Today's date.
// Create a SPQuery Object
SPQuery DateFieldQuery = new SPQuery();
//Write the query (I suggest using U2U Query Bulider
DateFieldQuery.Query = “<Where><Eq><FieldRef Name=\ ”TodaysDate\” />
<Value Type=\ ”DateTime\”> + DateTime.Now.ToString("yyyy-MM-ddTHH:\\:mm \\:ssZ") + </Value>
</Eq></Where>”;
//Get the List
SPList myList = myWeb.Lists["ListName"];
//Get the Items using Query
SPListItemCollection ResultItems = myList.GetItems(DateFieldQuery);
Query Using Yes\No Columns - Query to Retrieve all the Items from a list where a Yes\NO type Field, named "AreYouCool?" is "Yes".
// Create a SPQuery Object
SPQuery CheckBoxQuery = new SPQuery();
//Write the query (I suggest using U2U Query Bulider
CheckBoxQuery .Query = “<Where><Eq><FieldRef Name=\ ”AreYouCool?\” />
<Value Type=\ ”bit\”>1</Value>
</Eq></Where>”;
//Get the List
SPList myList = myWeb.Lists["ListName"];
//Get the Items using Query
SPListItemCollection ResultItems = myList.GetItems(CheckBoxQuery);
SPQuery, SPSiteDataQuery, SPUser
SPQuery
The SPQuery object allows you to construct queries to return specific items in a list.Use CAML.
you have to come up with a Query string and the language the query is defined in is a SharePoint specific language called CAML
E.g., search our announcements lists for items with a title that contains “Test” :
<OrderBy>
<FieldRef name=”Title”>
</OrderBy>
<Where>
<Contains>
<FieldRef name=”Title”>
<Value Type=”Text”/>Test</Value>
</Contains>
</Where>
The <Contain> can be replaced with:
<EQ>, <NEQ>, <GT>, <GEQ>, <LT>, <IsNull> and <BeginsWith>
Sample code:
SPQuery query = new SPQuery();
Query.Query=”{The above mentioned CAML code}”SPListItemCollection items = list.Getitems(query);
SPSiteDataQuery
SPSiteDataQuery is like a powered up version of SPQuery. It can execute across multiple lists.
SPQuery can only execute in one particular list.
Sample code:
SPSiteDataQuery query = new SPSiteDataQuery();
query.Lists = “<Lists BaseType=’0′>”;
query.Query ==”{The above mentioned CAML code}”query.Webs=”<Websope=’SiteCollection’/>”;
query.ViewFields=”<FieldRef Name=”Title”/>;
query.RowLimit=10;
using (SPWeb web=SPContext.Current.Web) {
System.Data.DataTable table=web.getSiteData(query);
//… other things
}
SPUser
SPUser.UserToken and SPUser.Sid not availble in sanbox solution.
public override void ItemUpdating(SPItemEventProperties properties) {
SPWeb = properties.Web;
SPUser user=web.AllUsers[@"DOMAIN\ecrater"];
user.email=”xxxxxxx”;
user.Notes = “CCCCCC”;
User.Update();
Base.ItemUpdating(properties);
}
The SPQuery object allows you to construct queries to return specific items in a list.Use CAML.
you have to come up with a Query string and the language the query is defined in is a SharePoint specific language called CAML
E.g., search our announcements lists for items with a title that contains “Test” :
<OrderBy>
<FieldRef name=”Title”>
</OrderBy>
<Where>
<Contains>
<FieldRef name=”Title”>
<Value Type=”Text”/>Test</Value>
</Contains>
</Where>
The <Contain> can be replaced with:
<EQ>, <NEQ>, <GT>, <GEQ>, <LT>, <IsNull> and <BeginsWith>
Sample code:
SPQuery query = new SPQuery();
Query.Query=”{The above mentioned CAML code}”SPListItemCollection items = list.Getitems(query);
SPSiteDataQuery
SPSiteDataQuery is like a powered up version of SPQuery. It can execute across multiple lists.
SPQuery can only execute in one particular list.
Sample code:
SPSiteDataQuery query = new SPSiteDataQuery();
query.Lists = “<Lists BaseType=’0′>”;
query.Query ==”{The above mentioned CAML code}”query.Webs=”<Websope=’SiteCollection’/>”;
query.ViewFields=”<FieldRef Name=”Title”/>;
query.RowLimit=10;
using (SPWeb web=SPContext.Current.Web) {
System.Data.DataTable table=web.getSiteData(query);
//… other things
}
SPUser
SPUser.UserToken and SPUser.Sid not availble in sanbox solution.
public override void ItemUpdating(SPItemEventProperties properties) {
SPWeb = properties.Web;
SPUser user=web.AllUsers[@"DOMAIN\ecrater"];
user.email=”xxxxxxx”;
user.Notes = “CCCCCC”;
User.Update();
Base.ItemUpdating(properties);
}
No comments:
Post a Comment