SharePoint CAML Query with FileLeafRef

When we try to use LinkFilename in CAML query which is the internal name for column “Name” we are getting an error and CAML query fail.

But we can use LinkFilename column in a OrderBy clause

if you can’t use LinkFilename in a Where clause, you can alternatively use the FileLeafRef column which contains the document name part of the document URL prefixed with it’s list ID

i.e. ows_ID

sample Code to use FileLeafRef in CAMl Query

string currentPageName = “default.aspx”;

string strCat = string.Empty;

try
{
using(SPSite oSite = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb oweb = oSite.OpenWeb())
{
SPList oLlist = oweb.Lists.TryGetList(“Pages”);

SPQuery query = new SPQuery();
query.Query = “<Where><Contains><FieldRef Name=’FileLeafRef’ /><Value Type=’Text’>” + currentPageName + “</Value></Contains></Where>”;

SPListItemCollection oLlistItem = oLlist.GetItems(query);

foreach (SPListItem item in oLlistItem)
{
strCat = Convert.ToString(item[“ColumnName”]);
}

lblMessage.Text = strCat;
}

}

}
catch (Exception ex)
{
throw ex;
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s