Search This Blog

2009-04-30

SPQuery

What is SPQuery
SPQuery class is used to instantiate objects which can are used to query the list and document libraries.The Query is prepared using a xml language called CAML.The Query returns all the records from the list which meets athe criteria.

Public Properties :-

Folder
Microsoft.SharePoint.SPFolder
Gets or sets the folder within a document library from which to return items in the query.
ListItemCollectionPosition
Microsoft.SharePoint.SPListItemCollectionPosition
Gets or sets an object that is used to obtain the next set of rows in a paged view of a list.
Query
String
Gets or sets the inner XML used in the query.
RowLimit
UInt32
Gets or sets a limit for the number of items returned in the query per page.
ViewAttributes
String
Gets or sets the attributes of the view used in the query.
ViewFields
String
Gets or sets the fields that are displayed in the query.

Code Snippet example:-

//****** SPQuery Example and Adding Atachments to List Item********//
SPSite site = new SPSite("http://gpchbs-s6137:92");
SPWeb web = site.OpenWeb();
SPQuery query = new SPQuery();
SPList dlist=web.Lists["Dlist"];
SPListItemCollection collection = null;
SPListItem item = null;
query.Query = "" +
"" +
"" +
"0"+
"
"+
""+
"" +
"
"+
"
" +
"
"+
"";
query.RowLimit = 10;
//query.ViewFields = "" +
// "";
query.ViewAttributes = "Scope=\"Recursive\"";

collection = dlist.GetItems(query);
if (collection != null)
{
if (collection.Count > 0)
{
dlist.EnableAttachments = true;
dlist.Update();
for (int i = 0; i < collection.Count - 1; i++)
{
item = collection[i];
byte[] barr1 = File.ReadAllBytes("C:\\Arijit\\PracticeDemos\\backupkmlist.txt");
byte[] barr2=File.ReadAllBytes("C:\\Arijit\\PracticeDemos\\Copybackupkmlist.txt");
item.Attachments.AddNow("backupkmlist.txt", barr1);
item.Attachments.AddNow("Copybackupkmlist.txt", barr2);
}
for (int j = 0; j < item.Attachments.Count; j++)
{
Console.WriteLine("Leaf Name: " + item.Attachments[j]);
}

Console.ReadLine();}}








//****** SPQuery Example With Forward only Pagination********//
SPSite site = new SPSite("http://gpchbs-s6137:92");
bool nextPage = true;
SPWeb web = site.OpenWeb();
SPQuery query = new SPQuery();
SPList dlist=web.Lists["Dlist"];
SPListItemCollection collection = null;
SPListItem item = null;
query.Query = "" +
"" +
"" +
"0"+
"
"+
""+
"" +
"
"+
"
" +
"
"+
"";
query.RowLimit = 2;
//query.ViewFields = "" +
// "";
query.ViewAttributes = "Scope=\"Recursive\"";

while (nextPage)
{
collection = dlist.GetItems(query);
if (collection != null)
{
if (collection.Count > 0)
{
Console.WriteLine("**********************");
Console.WriteLine("Title " + "\t" + "ID ");
//dlist.EnableAttachments = true;
//dlist.Update();
//for (int i = 0; i < collection.Count - 1; i++)
//{
// item = collection[i];
// byte[] barr1 = File.ReadAllBytes("C:\\Arijit\\PracticeDemos\\backupkmlist.txt");
// byte[] barr2=File.ReadAllBytes("C:\\Arijit\\PracticeDemos\\Copybackupkmlist.txt");
// item.Attachments.AddNow("backupkmlist.txt", barr1);
// item.Attachments.AddNow("Copybackupkmlist.txt", barr2);
//}
//for (int j = 0; j < item.Attachments.Count; j++)
//{
// Console.WriteLine("Leaf Name: " + item.Attachments[j]);
//}
for (int i = 0; i < collection.Count; i++)
{
Console.WriteLine(collection[i].Title + "\t" + collection[i].ID);
}

}


}
if (collection.ListItemCollectionPosition != null)
{
if (!string.IsNullOrEmpty(collection.ListItemCollectionPosition.PagingInfo))
{
query.ListItemCollectionPosition = collection.ListItemCollectionPosition;
Console.WriteLine("Press Enter for Next Page...");
Console.ReadLine();

}
else
{
Console.WriteLine("Last Page Reached");
Console.ReadLine();
}
}
else
{
nextPage = false;
Console.WriteLine("Last Page Reached");
Console.ReadLine();
}
}

No comments: