Skip Ribbon Commands
Skip to main content

Robin | zevenseas | SharePoint Blog

:

The zevenseas Community > Blogs > Robin | zevenseas | SharePoint Blog > Posts > SPList has no Url property?
February 01
SPList has no Url property?

At first I thought I didn’t look good when I was looking for the Url property on the SPList class because it’s child and parent classes has them (SPWeb, SPListItem, SPFile) and thus, so I thought, the class itself should have it as well. But.. it doesnt! Aaargh.. Which properties are there to use then to you might wonder? Well first let me explain why I want to have the Url property. I’m trying to replace the default ListFormWebPart on the editform.aspx pages on certain lists and/or document libraries. To do this I use the SPLimitedWebPartManager and this thing requires a server-relative or absolute url to the page are you trying to access.

So at my first go I tried the following bit :

SPLimitedWebPartManager webPartManager = 
    web.GetLimitedWebPartManager(web.Url + "/lists/" + list.Title + "/editform.aspx", 
    System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

But since the title of a list could be very different from the actual Url this was not really an option so I had to look for other properties and found the following:

  • RootFolder (thanks Ton :)
    • Simply replacing the list.Title with list.RootFolder
      SPLimitedWebPartManager webPartManager = 
          web.GetLimitedWebPartManager(web.Url + "/lists/" + list.RootFolder + "/editform.aspx", 
          System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
  • DefaultViewUrl
    • Some string manipulation at first and then you have this beautifully crafted server-relative url ;)
      string listUrl = list.DefaultViewUrl;  
      int index = listUrl.IndexOf("Forms") + 6;
      listUrl = listUrl.Remove(index);
      
      if (listUrl.EndsWith("/"))
      {
          listUrl = listUrl.Remove(listUrl.LastIndexOf("/"));
      }
      
      SPLimitedWebPartManager webPartManager =
          web.GetLimitedWebPartManager(listUrl + "/editform.aspx", 
          System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

Taking this a step for further and using the technique that Adam Buenz blogged about at Writing Extension Methods for SharePoint it’s incorporated in the SPList class like so:

public static class Extensions
    {
        /// <summary>
        /// Returns a server relative url of the list
        /// </summary>
        /// <param name="typeToTarget"></param>
        /// <returns>string</returns>
        public static string Url(this SPList typeToTarget)
        {
            string listUrl = typeToTarget.DefaultViewUrl;
            if (typeToTarget is SPDocumentLibrary)
            {

                int index = listUrl.IndexOf("Forms") + 6;
                listUrl = listUrl.Remove(index);

                if (listUrl.EndsWith("/"))
                {
                    listUrl = listUrl.Remove(listUrl.LastIndexOf("/"));
                }
            }
            else
            {
                int indexSlash = listUrl.LastIndexOf("/");
                listUrl = listUrl.Remove(indexSlash);
            }
            return listUrl;
        }
    } 

And the end result being :

SPList list = SPContext.Current.Web.Lists[new Guid(id)];

SPLimitedWebPartManager webPartManager =
    web.GetLimitedWebPartManager(list.Url + "/editform.aspx", 
    System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

 

Technorati Tags: ,

Comments

Keith Dahlby

SPList exposes its URLs through its Forms property:

public static string GetFormUrl(this SPList list, PAGETYPE formType)
{
  return list.Forms[formType].ServerRelativeUrl;
}

var editUrl = list.GetFormUrl(PAGETYPE.PAGE_EDITFORM);

Cheers ~
Keith
System Account on 01/02/2009 12:16

Robin

Ah.. thanks Keith!
System Account on 01/02/2009 22:39

Jeremy Thake

Thanks Robin and Keith that's awesome. I've actually added Keith's snippet to the SharePoint Dev Wiki and referenced this page.

http://www.sharepointdevwiki.com/display/public/Accessing+the+List+Url+property

System Account on 01/02/2009 22:43

Selvi

Perfect timing. Exactly what I was looking for.

Thanks!
System Account on 24/02/2009 20:02

Internet Business Review

Really Nice Post. More and more businesses are indulge with online presence, it is costly if you find a wrong company to do your job
System Account on 10/10/2009 11:43

Conciant

I like your post, it’s a really good stuff, keep it up! Office Conceirge has another benefit that it is economic too
System Account on 25/11/2009 00:31

Sérgio Fernandes

foreach (SPWeb web in SPContext.Current.Site.AllWebs)
            {
                foreach (SPList l in web.Lists)
                {
                    string listUrl = SPContext.Current.Site.Url + l.DefaultViewUrl;
                }
            }
System Account on 18/03/2010 03:40

Volker Lange

 Hello! Google gerade im Web und bin auf dieser Homepage gelandet.
Respekt, haben Sie toll gemacht.
Ich wünsche Ihnen weiterhin viel Erfolg mit Ihre Seite und natürlich viele Besucher.
Hier habe ich mich sehr wohl gefühlt und werde wieder hierher zurückkommen.
Freue mich auf einen Gegenbesuch auf meiner Homepage
System Account on 21/03/2010 20:40
 

 Statistics

 
Views: 11566
Comments: 8
Tags:
Published:1573 Days Ago