none
get SPListItem reference from a url RRS feed

  • Question

  • Hi, I'm new to SharePoint and had a question that I believe deals with an SPListItem. 

     

    A little background on what we're doing - we have a Forms Based Authentication site.  We have a buncha different document libraries through a bunch of subsites and we have been asked to do is have the user "register" before they are able to view a document.  So when the user clicks on a document they are redirected to a registration page and the url of that registration page contains this as part of the url: "http://www.site.com?ReturnUrl=%2f_layouts%2fAuthenticate.aspx%3fSource%3d%252fDocuments%252fmyPDF%252etxt&Source=%2fDocuments%2fmyPDF%2etxt".

     

    What we have also been asked to do is create a log of the items users are trying to access - but the client would like the log to contain the documents title not the url to the document.

     

    So my thinking was take the value that is in Source and creating a reference to an SPListItem and get the id from that, but I'm having a hard time actually making that happen.  Is there a better way or does somebody know how to create an SPListItem when the url is given?

     

    Thanks for the help!

    Tuesday, July 1, 2008 8:41 PM

Answers

  • After a night of sleep we came back and started anew... we found this page http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getlistitem.aspx and came up with this code which works very nicely.  Thanks to all for your suggestions!

     

    Code Snippet

    using (SPWeb web = SPContext.Current.Site.RootWeb)

    {

        string returnURL = HttpUtility.UrlDecode(this.Page.Request.Params["Source"].ToString());

     

        using (SPSite site = new SPSite(web.Url + returnURL))

        {

            SPWeb myWeb = site.OpenWeb();

            SPListItem target = myWeb.GetListItem(returnURL);

            string itemTitle = target.Title;

            myWeb.Dispose();

        }

    }

     

    Wednesday, July 2, 2008 2:54 PM

All replies

  • In the code for Authenticate.aspx, check the values for SPContext.Current.  See if there is a valid reference to ListItem and ListeItem, ItemId, etc.  Here's the SDK page for SPContext (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontext_members.aspx).

    Tuesday, July 1, 2008 8:53 PM
  •  

    Hi,

    We are trying to do something similar, but its not a registration page.

     

    We have webpart in page that contains a URL of a document library file as a parameter in the url address. We want to get the "Title" column (if any) from the item referenced on the URL. How can I get the reference to that list item from a URL? Or is there a better way to get the title column on a SPListItem from the URL?

     

    Thanks!

    Tuesday, July 1, 2008 9:33 PM
  • There is probably a more efficient method I'm missing, but one way you can do this is to use the URL to get an SPWeb, then iterate over all the SPLists in the SPListCollection, then all the SPListItems in each SPListItemCollection for each SPList, and use the SPListItem.URL property to identify the SPListItem you need.  From there you can grab whatever properties of the Site, Web, List, or ListItem you need.

     

    Tuesday, July 1, 2008 11:38 PM
  • After a night of sleep we came back and started anew... we found this page http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getlistitem.aspx and came up with this code which works very nicely.  Thanks to all for your suggestions!

     

    Code Snippet

    using (SPWeb web = SPContext.Current.Site.RootWeb)

    {

        string returnURL = HttpUtility.UrlDecode(this.Page.Request.Params["Source"].ToString());

     

        using (SPSite site = new SPSite(web.Url + returnURL))

        {

            SPWeb myWeb = site.OpenWeb();

            SPListItem target = myWeb.GetListItem(returnURL);

            string itemTitle = target.Title;

            myWeb.Dispose();

        }

    }

     

    Wednesday, July 2, 2008 2:54 PM
  • Nice! Exactly what I needed.

     

    Thank you.

     

    Wednesday, July 2, 2008 4:54 PM