SPList Url

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 (SPWebSPListItemSPFile) 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);
posted on 2012-06-21 17:53  EricLee007  阅读(512)  评论(0编辑  收藏  举报