Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
To help figure out what fields and values you need to be looking for in CAML, download SharePoint Manager and poke around to see if you can find something that fits your needs. spm.codeplex.com – ToddersLegrande Sep 10 '12 at 17:28
I did some better googling and found this. Does it help? sharepoint.stackexchange.com/questions/20541/… – ToddersLegrande Sep 10 '12 at 18:12
That is a similar issue; but I need to take arbitrary URLs (well, I do know they link to something in SharePoint) and do something if it is a list item. But it may be that there's nothing better I can do than build up a list of strings that indicate that something definitely isn't a list item... – lgaud Sep 10 '12 at 18:25
Well that issue seems to be that its failing for links outside of SharePoint so it should still work if you know for a fact these items are from SharePoint. Is the "SharePoint" you speak of a different site collection, web app, or farm from the one you are loading? It looks like you use the same URL variable all the way down in the sample. Is this true for the real code? – ToddersLegrande Sep 10 '12 at 18:34

2 Answers

In the examples I have seen of this it is usually done in a very dirty way similar to how you are attempting to do it there.

However it is designed to throw System.IO.FileNotFoundException in which case look here.

share|improve this answer

While I can't quite determine why your exception handling is not working for SPWeb.GetListItem there are two things you should note:

share|improve this answer
The URL is absolute. With GetFile, similar exceptions get thrown from this line: SPFile file = spWeb.GetFile(url); (i.e. before I can check if the file exists). – lgaud Sep 10 '12 at 17:42
Googling the issue pulls up a bunch of stuff about impersonation. Are you using impersonation in your app? It would seem to me the problem isn't related to the question you are asking and you are in fact doing everything correctly. – ToddersLegrande Sep 10 '12 at 18:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.