Thursday, 5 February 2015

How to get single item from SharePoint document library using SPQuery (without using for loop) ?

Here is the C# code to achieve the above requirement:

public SPListItemCollection GetSpecificLibraryItem(fileName)
{
SPList list = web.Lists["MyDocName"];
SPQuery dQuery = new SPQuery();
dQuery.ViewAttributes = "Scope=\"Recursive\"";
string QueryString = "<Where>" +
                      "<Eq>" +
                        "<FieldRef Name=\"FileLeafRef\"/>" +
                        "<Value Type=\"Text\">" + fileName + "</Value>" +
                      "</Eq>" +
                     "</Where>";
         dQuery.Query = QueryString;
        SPListItemCollection collListItems = list.GetItems(dQuery);
  return collListItems;
}

No comments:

Post a Comment