最近有人说在知识库中迷路了。
随便打开一个文件夹或文章不知道他对应的路径。
于是这件事又被提交到我这里。
解决这个我费了些时间,这里和大家分享一下,希望大家不要在这里浪费时间。
我的思路是:
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的,为啥一个可以遍历一个不可以呢!
本文介绍了一种在SharePoint中获取文件或文件夹路径的方法。针对文件夹和文章两种情况,提出了不同的解决方案。对于文件夹,通过父级遍历获得路径;对于文章,则从URL中解析出路径。
1273

被折叠的 条评论
为什么被折叠?



