Take the 2-minute tour ×
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
add comment

3 Answers 3

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
add comment

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
add comment

The number at the end of the DisplayForm.aspx or EditForm.aspx is the (int) ID of the list item. You should be able to extract that number from the query string and then get the number. From there, if you already have the other list item you want to compare in memory, you can just call its own ID value and compare...

int listItemID = (int)yourListItem["ID"];
if(queryStringID == listItemID) { (insert code here) }

You'll want to watch out that you're pulling the integer item ID, not the GUID "unique ID". I think that's the field with the internal name of ID but if you want to be 100% sure, you can just pull up the list in Powershell and ask it for the types...

$list = $yourWebSite.Lists["Your List Name"]
$list.fields | select title, internalname, type | sort-object title
share|improve this answer
add comment

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.