MOSS2007 之 SPListItem 路径问题

来源:互联网 发布:整站优化 编辑:程序博客网 时间:2024/06/05 02:19

 最近有人说在知识库中迷路了。

随便打开一个文件夹或文章不知道他对应的路径。

于是这件事又被提交到我这里。

解决这个我费了些时间,这里和大家分享一下,希望大家不要在这里浪费时间。

 

我的思路是:

     1、通过文件夹向上遍历,直到没又上层文件夹为止。

try
            {

          SPList list = web.Lists[new Guid(this._ms.ListID)];
                    SPListItem item = list.Folders[new Guid(this._ms.FolderID)];
                    strPosition = "" + item.Folder.Name;
                    SPFolder subfolder = item.Folder;
                    subfolder = subfolder.ParentFolder;
                    while(subfolder != null)
                    {
                        if (subfolder.Name.Contains("List"))
                        {
                            strPosition = list.Title + ">>" + strPosition;
                            break;
                        }
                        strPosition = subfolder.Name + ">>" + strPosition;
                        //this.Page.Response.Write("当前位置:" + strPosition);
                       
                        subfolder = subfolder.ParentFolder;                     
                      
                    }

 }
            catch
            {
                iReturnValue = 0;
            }
            finally
            {
                if (site != null) { site.Dispose(); }
                if (web != null) { web.Dispose(); }
            }

这个对于 SPListItem item 是文件夹的情况下是可以的,但是SPListItem item 为文章是 item.Folder为空的。

晕倒,微软这个做的也太不地道了吧,NND。

没办法,对于SPListItem item 为文章只能另外想办了。

试了试,item 的属性里好像没又关于路径的。

这是用鼠标点了一下SPListItem item 为文章的一片文章,浏览器的状态栏出现了一大串URL。

把这个URL 输出一下:Lists/Lists/AAA/BBB/CCC.Doc

眼前一亮,这个处理一下就是我要的路径了啊。

 SPSite site=null;
        SPWeb web=null;
        try
        {
           
            site = new SPSite(siteUrl);
            web = site.OpenWeb(new Guid(webID));
            web.AllowUnsafeUpdates = true;
            SPList list = web.Lists[new Guid(listID)];
            SPListItem item = list.GetItemById(itemID);
          
            try
            {               
                //this.Page.Response.Write("" + item.Folder.Name);
                this.Page.Response.Write("" + item.Url);
                string strPosition = "";
                string[] strArray = ((string)(item.Url)).Split("/".ToCharArray());
                strPosition = list.Title;
                for(int i=2;i<strArray.Length-1;i++)
                {
                    strPosition = strPosition + ">>" + strArray[i];
                }

                lblPosition.Text = "眤瞷竚:" + strPosition;
            }
            catch (Exception lblException)
            {
                this.Page.Response.Write(lblException.Message+":<br>"+lblException.StackTrace);
            }
}
 SPSite site=null;
        SPWeb web=null;
        try
        {
           
            site = new SPSite(siteUrl);
            web = site.OpenWeb(new Guid(webID));
            web.AllowUnsafeUpdates = true;
            SPList list = web.Lists[new Guid(listID)];
            SPListItem item = list.GetItemById(itemID);
          
            try
            {               
                //this.Page.Response.Write("" + item.Folder.Name);
                this.Page.Response.Write("" + item.Url);
                string strPosition = "";
                string[] strArray = ((string)(item.Url)).Split("/".ToCharArray());
                strPosition = list.Title;
                for(int i=2;i<strArray.Length-1;i++)
                {
                    strPosition = strPosition + ">>" + strArray[i];
                }

                lblPosition.Text = "当前位置:“+ strPosition;
            }
            catch (Exception lblException)
            {
                this.Page.Response.Write(lblException.Message+":<br>"+lblException.StackTrace);
            }
}
 catch
        {
        }
        finally
        {
            if(web != null){web.Dispose();};
            if(site != null){site.Dispose();};
        }

搞定了,在第二个上费了点时间啊。关键微软做的不好,明明那种情况都有相应的Folder的,为啥一个可以遍历一个不可以呢!

 

 

原创粉丝点击