[{"id":1,"pId":0,"name":"test","furl":"administrator/test","iconSkin":"pIcon01"},
{"id":11,"pId":1,"name":"test1","furl":"administrator/test/test1","iconSkin":"icon03"},
{"id":111,"pId":11,"name":"test11","furl":"administrator/test/test1/test11","iconSkin":"icon03"},
{"id":2,"pId":0,"name":"dd","furl":"administrator/dd","iconSkin":"pIcon01"},
{"id":3,"pId":0,"name":"hlan","furl":"administrator/hlan","iconSkin":"pIcon01"},
{"id":31,"pId":3,"name":"123123","furl":"administrator/hlan/123123","iconSkin":"icon03"}]
上面为文件Json数据,简单的描述下pId为父级Id(当然对比id和pId的关系一目了然就不多说了),name为文件夹名称,furl为文件夹路径,下面直接贴代码
/// <summary>
/// 构建文档库文件树
/// </summary>
public void getDocLibFileTree()
{
try
{
string name = "";
string url = "";
string docLibName = SPContext.Current.Web.CurrentUser.LoginName;
string sitetitle = "mydocs";//文档库所在子网站
if (!String.IsNullOrEmpty(docLibName))
{
docLibName = docLibName.IndexOf("\\") >= 0 ? docLibName.Substring(docLibName.IndexOf("\\") + 1) : docLibName;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
Guid siteID = Web.Site.RootWeb.Site.ID;
using (SPSite spSite = new SPSite(siteID))
{
using (SPWeb spWeb = spSite.AllWebs[sitetitle])
{
//spWeb.AllowUnsafeUpdates = true;
url = spWeb.ServerRelativeUrl + "/" + docLibName;
SPFolder folder = spWeb.GetFolder(url);//确定要操作的文档库文件夹
bool isexist = folder.Exists;
if (isexist)
{
List<FDocLibtree> dltl = getFoldersBylist(folder);
this.JResponse.RetCode = 0;
this.JResponse.Message = Helper.JsonSerialize(dltl);
}
else
{
this.JResponse.RetCode = 2;
this.JResponse.Message = "file is not exist!";
}
}
}
});
}
}
catch (Exception ex)
{
this.DebugTrace("lsycHandler.ashx getDocLibFileTree failed: {0}", ex.Message);
this.JResponse.RetCode = 2;
this.JResponse.Message = ex.Message;
}
}
public List<FDocLibtree> getFoldersBylist(SPFolder folder)
{
List<FDocLibtree> rootdltl = new List<FDocLibtree>();
SPFolderCollection folders = folder.SubFolders;
int i = 1;
foreach (SPFolder sf in folders)
{
if (sf.Name!="Forms")
{
FDocLibtree dltone = new FDocLibtree()
{
id = i,
pId = 0,
furl = sf.Url,
name = sf.Name,
iconSkin = "pIcon01"
};
rootdltl.Add(dltone);
getFoldersByfolder(sf, i.ToString(), ref rootdltl);
i++;
}
}
return rootdltl;
}
public void getFoldersByfolder(SPFolder folder,string i,ref List<FDocLibtree> fdlt)
{
SPFolderCollection folders = folder.SubFolders;
FDocLibtree dltone = new FDocLibtree();
int j = 1;
foreach (SPFolder sf in folders)
{
dltone = new FDocLibtree()
{
id = int.Parse(i + j.ToString()),
pId = int.Parse(i),
furl = sf.Url,
name = sf.Name,
iconSkin = "icon03"
};
fdlt.Add(dltone);
getFoldersByfolder(sf, i + j.ToString(), ref fdlt);
j++;
}
}
另外一种树形结构数据,直接贴图吧,树结构一目了然,下面是构建的代码
/// <summary>
/// 根目录下的文件夹
/// </summary>
/// <param name="folder"></param>
/// <returns></returns>
public List<DocLibtree> EnumerateFoldersBylist(SPFolder folder)
{
List<DocLibtree> rootdltl = new List<DocLibtree>();
DocLibtree dlt = new DocLibtree();
dlt.url = folder.Url;
dlt.fname = folder.Name;
dlt.children = null;
SPFolderCollection folders = folder.SubFolders;
if (folders.Count > 0)
{
List<DocLibtree> rootdltlone = new List<DocLibtree>();
foreach (SPFolder sf in folders)
{
if (sf.Name != "Forms")
{
DocLibtree dltone = new DocLibtree()
{
url = sf.Url,
fname = sf.Name,
children = EnumerateFoldersByfolder(sf)
};
rootdltlone.Add(dltone);
}
}
dlt.children = rootdltlone;
}
rootdltl.Add(dlt);//树的根目录
return rootdltl;
}
/// <summary>
/// 递归获取文档库中的所有文件夹及其文件夹内的文件
/// </summary>
/// <param name="folder"></param>
/// <returns></returns>
public List<DocLibtree> EnumerateFoldersByfolder(SPFolder folder)
{
SPFolderCollection folders = folder.SubFolders;
List<DocLibtree> dltl = new List<DocLibtree>();
if (folders.Count > 0)
{
foreach (SPFolder sf in folders)
{
DocLibtree dltone = new DocLibtree()
{
url = sf.Url ,
fname = sf.Name,
children = EnumerateFoldersByfolder(sf)
};
dltl.Add(dltone);
}
}
return dltl;
}
public class DocLibtree
{
public string url;
public string fname;
public List<DocLibtree> children;
}
public class FDocLibtree
{
public int id;
public int pId;
public string name;
public string furl;
public string iconSkin;
}