I am wondering if there is some sort of way to check a URL to see if it represents a list item before calling SPWeb.GetListItem that I haven't found.
I have code that needs to retrieve a list item for a given URL, and do something if it is a list item, but that URL may well not be a list item. SPWeb.GetListItem throws exceptions that even when caught mess things up at the SharePoint level in certain circumstances (log shows <nativehr>0x80070002</nativehr><nativestack></nativestack>, user gets "Cannot complete this action"). I tried using SPWeb.GetFile as well, that causes even more exceptions in the log and the same error message.
using (SPSite spSite = new SPSite(url))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
try
{
m_spListItem = spWeb.GetListItem(url);
if (m_spListItem != null)
{
return true;
}
}
catch (Exception e)
{
// Expected when not a list item
}
return false;
}
}
Edit: I've been thinking of CAML, but the following comes back with no results (for a known good URL in SP CAML Query Helper). Perhaps the Type needs to be different? But URL doesn't work either.
<Where>
<Eq>
<FieldRef Name="EncodedAbsUrl" />
<Value Type="Text">http://site/List/docName.docx</Value>
</Eq>
</Where>
Edit - Sept 26: Looks like you can query on EncodedAbsUrl - by giving it an UNENCODED URL. I was testing in Shared Documents and using "Shared%20Documents" in the path, which found nothing, but "Shared Documents" works.