using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
public partial class TEMPLATE_LAYOUTS_News_manageSort : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getSort();
}
}
/// <summary>
/// 获取所有的新闻类别
/// </summary>
private void getSort()
{
SPWeb web = SPContext.Current.Web;//当前站点
SPList list = web.GetList(web.Url + "/Lists/News");
SPFolder rootFolder = list.RootFolder;
TreeNode rootNode = new TreeNode();
rootNode.Text = "<font color='gray'>新闻类别</font>";
rootNode.SelectAction = TreeNodeSelectAction.None;
tvwSort.Nodes.Add(rootNode);
SPFolderCollection folders = rootFolder.SubFolders;
foreach (SPFolder folder in folders)
{
if (folder.Name != "Item" && folder.Name != "Attachments")
{
TreeNode nd = new TreeNode();
nd.Text = "<font color='green'>" + folder.Name+ "</font>";
nd.NavigateUrl = SPContext.Current.Web.Url + "/_layouts/News/manageSort.aspx?sortId=" + folder.Item.ID.ToString();
nd.Target = "_self";
if (folder.Item.ID.ToString() == Request.QueryString["sortId"])
{
nd.Selected = true;
}
rootNode.ChildNodes.Add(nd);
getSubfolder(nd, folder);
}
}
}
/// <summary>
/// 获取子新闻类别
/// </summary>
/// <param name="node"></param>
/// <param name="folder"></param>
private void getSubfolder(TreeNode node, SPFolder folder)
{
SPFolderCollection folders = folder.SubFolders;
foreach (SPFolder subFolder in folders)
{
if (subFolder.Name != "Item" && subFolder.Name != "Attachments")
{
TreeNode nd = new TreeNode();
nd.Text = "<font color='green'>" + subFolder.Name + "</font>";
nd.NavigateUrl = SPContext.Current.Web.Url + "/_layouts/News/manageSort.aspx?sortId=" + subFolder.Item.ID.ToString();
nd.Target = "_self";
if (subFolder.Item.ID.ToString() == Request.QueryString["sortId"])
{
nd.Selected = true;
}
node.ChildNodes.Add(nd);
getSubfolder(nd, subFolder);
}
}
}
/// <summary>
/// 添加新闻类别
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnSave_Click(object sender, EventArgs e)
{
try
{
int id = 0;
bool flag = false;
if (Request.QueryString["sortId"] != null)
{
id = Convert.ToInt32(Request.QueryString["sortId"]);
}
string folderName = txtNewsSort.Text.Trim();
if (isExistsAddFolder(id, folderName))//文件存在
{
Page.ClientScript.RegisterStartupScript(GetType(), "sort_exists", "alert('新闻类别已经存在!');", true);
}
else
{
flag = AddFolder(id, folderName);
if (flag)
{
Page.ClientScript.RegisterStartupScript(GetType(), "add_sort_success", "alert('添加新闻类别成功!');window.location.href = window.location.href;", true);
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "add_sort_failure", "alert('添加新闻类别失败!');", true);
}
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
/// <summary>
/// 判断新闻类别是否存在
/// 返回true文件存在
/// </summary>
/// <param name="id"></param>
/// <param name="folderName"></param>
/// <returns></returns>
private bool isExistsAddFolder(int id, string folderName)
{
bool flag = false;
string webUrl = SPContext.Current.Web.Url;
using (SPWeb web = new SPSite(webUrl).OpenWeb())
{
SPList list = web.GetList(webUrl + "/Lists/News");
SPFolderCollection folders = null;
if (id == 0)
{
folders = list.RootFolder.SubFolders;
}
else
{
folders = list.GetItemById(id).Folder.SubFolders;
}
foreach (SPFolder folder in folders)
{
if (folder.Name == folderName)
{
flag = true;
break;
}
}
}
return flag;
}
/// <summary>
/// 判断新闻类别是否存在
/// 返回true文件存在
/// </summary>
/// <param name="id"></param>
/// <param name="folderName"></param>
/// <returns></returns>
private bool isExistsEditFolder(int id, string folderName)
{
bool flag = false;
string webUrl = SPContext.Current.Web.Url;
using (SPWeb web = new SPSite(webUrl).OpenWeb())
{
SPList list = web.GetList(webUrl + "/Lists/News");
SPFolderCollection folders = null;
if (id == 0)
{
folders = list.RootFolder.ParentFolder.SubFolders;
}
else
{
folders = list.GetItemById(id).Folder.ParentFolder.SubFolders;
}
foreach (SPFolder folder in folders)
{
if (folder.Name == folderName)
{
flag = true;
break;
}
}
}
return flag;
}
/// <summary>
/// 添加新闻类别
/// 返回true添加文件成功
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private bool AddFolder(int id, string folderName)
{
bool flag = false;
string webUrl = SPContext.Current.Web.Url;
using (SPWeb web = new SPSite(webUrl).OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.GetList(webUrl + "/Lists/News");
list.EnableFolderCreation = true;
SPListItem item = null;
if (id == 0)
{
item = list.AddItem(list.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderName);
}
else
{
SPFolder folder = list.GetItemById(id).Folder;
item=list.AddItem(folder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderName);
}
try
{
item.Update();
flag = true;
}
catch (Exception ex)
{
flag = false;
}
}
return flag;
}
/// <summary>
/// 删除新闻类别
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnDelete_Click(object sender, EventArgs e)
{
int id = 0;
if (Request.QueryString["sortId"] != null)
{
try
{
id = Convert.ToInt32(Request.QueryString["sortId"]);
}
catch (Exception ex)
{ }
}
if (id != 0)
{
if (DelFolderById(id))
{
Page.ClientScript.RegisterStartupScript(GetType(), "del_sort_success", "alert('删除新闻类别成功!');window.location.href = window.location.href;", true);
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "del_sort_failure", "alert('删除新闻类别失败!');", true);
}
}
}
/// <summary>
/// 根据id删除folder
/// true删除成功
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
private bool DelFolderById(int id)
{
bool flag = false;
string webUrl = SPContext.Current.Web.Url;
using (SPWeb web = new SPSite(webUrl).OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.GetList(webUrl + "/Lists/News");
SPFolder folder = list.GetItemById(id).Folder;
try
{
folder.Delete();
flag = true;
}
catch
{
flag = false;
}
}
return flag;
}
/// <summary>
/// 修改新闻类别
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnEditSave_Click(object sender, EventArgs e)
{
int id = 0;
string newFolderName = txtEditNewsSort.Text.Trim();
if (Request.QueryString["sortId"] != null)
{
try
{
id = Convert.ToInt32(Request.QueryString["sortId"]);
}
catch (Exception ex)
{ }
}
if (id != 0)
{
string folderName = txtEditNewsSort.Text.Trim();
if (isExistsEditFolder(id, folderName))//文件存在
{
Page.ClientScript.RegisterStartupScript(GetType(), "sort_exists", "alert('新闻类别已经存在!');", true);
}
else
{
if (EditFolderNameByIdAndNewName(id, newFolderName))
{
Page.ClientScript.RegisterStartupScript(GetType(), "edit_sort_success", "alert('修改新闻类别成功!');window.location.href = window.location.href;", true);
}
else
{
Page.ClientScript.RegisterStartupScript(GetType(), "edit_sort_failure", "alert('修改新闻类别失败!');", true);
}
}
}
}
/// <summary>
/// 修改新闻类别
/// </summary>
/// <returns></returns>
private bool EditFolderNameByIdAndNewName(int id,string newFolderName)
{
bool flag = false;
string webUrl = SPContext.Current.Web.Url;
using (SPWeb web = new SPSite(webUrl).OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.GetList(webUrl + "/Lists/News");
SPFolder folder = list.GetItemById(id).Folder;
string parentPath = folder.ParentFolder.ServerRelativeUrl;
string path = parentPath.Substring(1, parentPath.Length - 1);
Response.Write(path);
try
{
folder.MoveTo(path+"/"+newFolderName);
flag = true;
}
catch(Exception ex)
{
Response.Write(ex.ToString());
flag = false;
}
}
return flag;
}
}
转自:http://blog.csdn.net/fei2008002/article/details/6577354
方法二:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace FolderDemo
{
class Program
{
static void Main(string[] args)
{
string strParentFolderUrl = AddSPFolder("", "c1");//根目录添加文件夹
AddSPFolder(@"/" + strParentFolderUrl, "c2");//在c1中添加文件
AddSPFile("", "c3");//在根目录中添加文件
AddSPFile(@"/" + strParentFolderUrl, "c4");//在c1中添加文件
DelSPFolder(23);//删除文件夹
DelSPFile(25);//删除文件
}
private static string strWebUrl="http://dg-caozhenhua:81/";
private static string strListName="FolderTest";
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="strParentFolderUrl">父文件夹</param>
/// <param name="strFolderName">文件夹名</param>
/// <returns></returns>
private static string AddSPFolder(string strParentFolderUrl,string strFolderName)
{
string strFolderUrl = "";
using (SPWeb sPWeb = new SPSite(strWebUrl).OpenWeb())
{
sPWeb.AllowUnsafeUpdates=true;
SPList sPList = sPWeb.Lists[strListName];
sPList.EnableFolderCreation = true;
SPListItem sPListItem = sPList.Items.Add(strParentFolderUrl, SPFileSystemObjectType.Folder, strFolderName);
try
{
sPList.Update();
sPListItem.Update();
strFolderUrl = sPListItem.Url;
}
catch (Exception ex)
{ }
}
return strFolderUrl;
}
/// <summary>
///
/// </summary>
/// <param name="strParentFolderUrl"></param>
/// <param name="strFileName"></param>
/// <returns></returns>
private static void AddSPFile(string strParentFolderUrl, string strFileName)
{
using (SPWeb sPWeb = new SPSite(strWebUrl).OpenWeb())
{
sPWeb.AllowUnsafeUpdates = true;
SPList sPList = sPWeb.Lists[strListName];
SPListItem sPListItem = sPList.Items.Add(strParentFolderUrl, SPFileSystemObjectType.File, strFileName);
sPListItem["Title"] = strFileName;
try
{
sPListItem.Update();
}
catch (Exception ex)
{ }
}
}
/// <summary>
/// 删除SPFloder
/// </summary>
/// <param name="strSPFolderName"></param>
/// <returns></returns>
public static void DelSPFolder(int id)
{
using (SPWeb sPWeb = new SPSite(strWebUrl).OpenWeb())
{
SPList sPList = sPWeb.Lists[strListName];
try
{
sPList.Folders.DeleteItemById(id);
}
catch (Exception ex)
{
}
}
}