以下是我在开发SharePoint过程中写的对文档库和列表进行的一些基本操作,先写下来,日后在加。 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.IO; using Microsoft.SharePoint; using yesinda.yesindakms.sharepoint; using yesinda.yesindakms.sharepoint.List; using Microsoft.SharePoint.WebControls; using Microsoft.SharePoint.Utilities; public partial class usercontrol_CreateNewDoc : System.Web.UI.UserControl { 定义变量和属性 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //绑定DropDownList控件 BindControl(); //绑定TreeView控件 TreeViewBind(this.TreeView1); TreeViewBind(this.TreeView2); //AddUserTreeNode(this.TreeView3); //绑定GridView数据控件 BindGrid(); HostName = Request.UserHostName; } } #region 为TreeView添加节点 /// <summary> /// 为TreeView添加节点 /// </summary> /// <param name="parentFolder"></param> /// <param name="parentNode"></param> private void AddChild(SPFolder parentFolder, TreeNode parentNode) { if (parentFolder.SubFolders.Count == 0) return; foreach (SPFolder f in parentFolder.SubFolders) { if (f.Name != "Forms" && f.Name.IndexOf("_") != 0) { TreeNode child = new TreeNode(f.Name, f.ServerRelativeUrl); parentNode.ChildNodes.Add(child); AddChild(f, child); } } } #endregion #region 绑定TreeView控件 /// <summary> /// 绑定TreeView控件 /// </summary> protected void TreeViewBind(TreeView treeview) { SPSite sps = yesinda.yesindakms.sharepoint.List.ListLib.findParamSite("/dept/gsb/", this.Context); sps.AllowUnsafeUpdates = true; SPWeb spw = sps.OpenWeb(); spw.AllowUnsafeUpdates = true; SPWebCollection sites = sps.AllWebs; foreach (SPWeb site in sites) { SPListCollection lists = site.Lists; foreach (SPList list in lists) { if (list.BaseType == SPBaseType.DocumentLibrary && list.BaseTemplate != SPListTemplateType.ListTemplateCatalog && list.BaseTemplate == SPListTemplateType.DocumentLibrary) { list9 = spw.Lists[list.Title]; items = list9.Items; SPFolder root = list.RootFolder; TreeNode rootNode = new TreeNode(list.Title, root.Url); treeview.Nodes.Add(rootNode); AddChild(root, rootNode); } } } } #endregion #region 创建数据源 /// <summary> /// 创建数据源 /// </summary> /// <returns></returns> ICollection CreateDataSource() { DataTable dt = new DataTable("mytable"); //创建一个名为mytable的DataTable对象形 DataColumn dc = new DataColumn(); //创建一个列对象 dc.DataType = System.Type.GetType("System.String"); //指定该列的数据类型 dc.Caption = "DocID"; //设置列的标题 dc.ColumnName = "文档库ID"; //设置 列集合对象中的列的名称,datagrid中显示该列名. dt.Columns.Add(dc); //将该列对象加入到表mytable的列集合中 //普通列 DataColumn dc1 = new DataColumn(); dc1.DataType = System.Type.GetType("System.String"); dc1.AllowDBNull = false; dc1.Caption = "Path"; dc1.ColumnName = "路径"; dt.Columns.Add(dc1); DataColumn dc2 = new DataColumn(); dc2.DataType = System.Type.GetType("System.String"); dc2.AllowDBNull = false; dc2.Caption = "FullPath"; dc2.ColumnName = "完整路径"; dc2.DefaultValue = 25; dt.Columns.Add(dc2); SPSite sps = yesinda.yesindakms.sharepoint.List.ListLib.findParamSite("/dept/gsb/", this.Context); sps.AllowUnsafeUpdates = true; SPWeb spw = sps.OpenWeb(); spw.AllowUnsafeUpdates = true; SPWebCollection sites = sps.AllWebs; foreach (SPWeb site in sites) { SPListCollection lists = site.Lists; foreach (SPList list in lists) { if (list.BaseType == SPBaseType.DocumentLibrary && list.BaseTemplate != SPListTemplateType.ListTemplateCatalog && list.BaseTemplate== SPListTemplateType.DocumentLibrary) { if (dt != null) { DataRow dr = dt.NewRow(); dr[0] = list.Title; dr[1] = list.RootFolder.ServerRelativeUrl; dr[2] = "http://" + sps.HostName + list.RootFolder.ServerRelativeUrl; dt.Rows.Add(dr); } } } } DataView dv = new DataView(dt); return dv; } /// <summary> /// 根据路径,动态创建数据源 /// </summary> /// <returns></returns> ICollection CreateDataSource(string folderpath) { DataTable dt = new DataTable("mytable"); //创建一个名为mytable的DataTable对象形 DataColumn dc = new DataColumn(); //创建一个列对象 dc.DataType = System.Type.GetType("System.Int32"); //指定该列的数据类型 dc.AutoIncrement = true; //该列为自动增涨列 dc.AutoIncrementSeed = 1; //初始值 dc.AutoIncrementStep = 2; //增量 dc.Caption = "DocID"; //设置列的标题 dc.ColumnName = "文档库ID"; //设置 列集合对象中的列的名称,datagrid中显示该列名. dt.Columns.Add(dc); //将该列对象加入到表mytable的列集合中 //普通列 DataColumn dc1 = new DataColumn(); dc1.DataType = System.Type.GetType("System.String"); dc1.AllowDBNull = false; dc1.Caption = "Path"; dc1.ColumnName = "路径"; dt.Columns.Add(dc1); DataColumn dc2 = new DataColumn(); dc2.DataType = System.Type.GetType("System.String"); dc2.AllowDBNull = false; dc2.Caption = "FullPath"; dc2.ColumnName = "完整路径"; dc2.DefaultValue = 25; dt.Columns.Add(dc2); SPSite sps = yesinda.yesindakms.sharepoint.List.ListLib.findParamSite("/dept/gsb/", this.Context); sps.AllowUnsafeUpdates = true; SPWeb spw = sps.OpenWeb(); spw.AllowUnsafeUpdates = true; SPFolder f = spw.GetFolder(folderpath); foreach (SPFile file in f.Files) { if (dt != null) { DataRow dr = dt.NewRow(); dr[0] = file.Item.ID; dr[1] = file.ServerRelativeUrl; dr[2] = "http://" + sps.HostName + file.ServerRelativeUrl; dt.Rows.Add(dr); } } DataView dv = new DataView(dt); return dv; } /// <summary> ///读取所有为文档库类型的列表,创建数据源 /// </summary> /// <returns></returns> ICollection MyDataSource() { DataTable dt = new DataTable("mytable"); //创建一个名为mytable的DataTable对象形 DataColumn dc = new DataColumn(); //创建一个列对象 dc.DataType = System.Type.GetType("System.Int32"); //指定该列的数据类型 dc.AutoIncrement = true; //该列为自动增涨列 dc.AutoIncrementSeed = 1; //初始值 dc.AutoIncrementStep = 2; //增量 dc.Caption = "DocID"; //设置列的标题 dc.ColumnName = "文档库ID"; //设置 列集合对象中的列的名称,datagrid中显示该列名. dt.Columns.Add(dc); //将该列对象加入到表mytable的列集合中 //普通列 DataColumn dc1 = new DataColumn(); dc1.DataType = System.Type.GetType("System.String"); dc1.AllowDBNull = false; dc1.Caption = "Path"; dc1.ColumnName = "路径"; dt.Columns.Add(dc1); DataColumn dc2 = new DataColumn(); dc2.DataType = System.Type.GetType("System.String"); dc2.AllowDBNull = false; dc2.Caption = "FullPath"; dc2.ColumnName = "完整路径"; dc2.DefaultValue = 25; dt.Columns.Add(dc2); SPSite sps = yesinda.yesindakms.sharepoint.List.ListLib.findParamSite("/dept/gsb/", this.Context); sps.AllowUnsafeUpdates = true; SPWebCollection sites = sps.AllWebs; foreach (SPWeb site in sites) { SPListCollection lists = site.Lists; foreach (SPList list in lists) { if (list.BaseType == SPBaseType.DocumentLibrary && list.BaseTemplate != SPListTemplateType.ListTemplateCatalog && list.BaseTemplate == SPListTemplateType.DocumentLibrary) { SPDocumentLibrary docLibrary = (SPDocumentLibrary)list; if (!docLibrary.IsCatalog && list.BaseTemplate != SPListTemplateType.XMLForm) { SPListItemCollection docLibItems = docLibrary.Items; foreach (SPListItem docLibItem in docLibItems) { if (dt != null) { DataRow dr = dt.NewRow(); dr[0] = docLibItem.ID ; dr[1] = docLibItem.Url; dr[2] = "http://" + sps.HostName + docLibItem.File.ServerRelativeUrl; dt.Rows.Add(dr); } } } } } } DataView dv = new DataView(dt); return dv; } #endregion #region 绑定控件 /// <summary> /// 绑定DropDownList控件 /// </summary> protected void BindControl() { this.ddlDocTitle1.DataSource = CreateDataSource(); this.ddlDocTitle1.DataTextField = "文档库ID"; this.ddlDocTitle1.DataValueField = "路径"; this.ddlDocTitle1.DataBind(); this.ddlDocTitle2.DataSource = CreateDataSource(); this.ddlDocTitle2.DataTextField = "文档库ID"; this.ddlDocTitle2.DataValueField = "路径"; this.ddlDocTitle2.DataBind(); } /// <summary> /// 绑定GridView控件 /// </summary> protected void BindGrid() { if (ViewState["FromPath"] != null) { this.GridView1.DataKeyNames = new string[] { "文档库ID" }; this.GridView1.DataSource = CreateDataSource(ViewState["FromPath"].ToString()); this.GridView1.DataBind(); } this.GridView2.DataKeyNames = new string[] { "文档库ID" }; this.GridView2.DataSource = MyDataSource(); this.GridView2.DataBind(); } /// <summary> /// 显示系统用户 /// </summary> /// <param name="treeview"></param> protected void AddUserTreeNode(TreeView treeview) { SPSite sps = yesinda.yesindakms.sharepoint.List.ListLib.findParamSite("/dept/gsb/", this.Context); sps.AllowUnsafeUpdates = true; SPWeb spw = sps.OpenWeb(); spw.AllowUnsafeUpdates = true; SPUserCollection users = spw.AllUsers; SPGroupCollection groups = spw.Groups; foreach (SPGroup group in groups) { Response.Write(group.Name + " " + group.ID + "<br>"); } foreach (SPUser user in users) { Response.Write(user.Name + " " + user.ID + "<br>"); } } #endregion #region 发送文档 /// <summary> /// 发送文档 /// </summary> protected void SendDocument() { SPSite sps = yesinda.yesindakms.sharepoint.List.ListLib.findParamSite("/dept/gsb/", this.Context); sps.AllowUnsafeUpdates = true; SPWeb spw = sps.OpenWeb(); spw.AllowUnsafeUpdates = true; #region 将指定的文档发送到指定的文档库 ArrayList arylst = new ArrayList(); //选择行,获取行中文档的地址 for (int rowindex = 0; rowindex < this.GridView1.Rows.Count; rowindex++) { if (((CheckBox)this.GridView1.Rows[rowindex].Cells[0].FindControl("CheckBox1")).Checked == true) { arylst.Add(this.GridView1.Rows[rowindex].Cells[2].Text); } } SPFolder f = spw.GetFolder(ViewState["FromPath"].ToString()); for (int j = 0; j < arylst.Count; j++) { SPFile file = f.Files[arylst[j].ToString()]; byte[] bytes = f.Files[file.Name].OpenBinary(); //复制文档到指定的文件夹,适合如跨网站复制文件 SPFolder folder = spw.GetFolder(ViewState["ToPath"].ToString()); ; bool ex = false; if (folder.Exists) { try { ex = folder.Files[file.Name].Exists; Response.Write("<script language="javascript">alert('已有相同名称的文件存在');</script>"); return; } catch { ex = false; folder.Files.Add(file.Name, bytes, true); Response.Write("<script language="javascript">alert('上传成功!');</script>"); } } } #endregion #region 发送前给文档受权 //文档受权 #endregion } #endregion #region 权限控制 /// <summary> /// 给条目授与多个权限 /// </summary> /// <param name="itemId"></param> /// <param name="userOrGroupName"></param> /// <param name="types"></param> public void AssignRights(int itemId, string userOrGroupName, SPRoleType[] types) { SPSite sps = yesinda.yesindakms.sharepoint.List.ListLib.findParamSite("/dept/gsb/", this.Context); sps.AllowUnsafeUpdates = true; SPWeb spw = sps.OpenWeb(); spw.AllowUnsafeUpdates = true; SPList list = spw.Lists["文档模板"]; //取得定义的ID SPListItem item = list.GetItemById(itemId); //新的权限集合 SPRoleAssignment nrole = new SPRoleAssignment(list.ParentWeb.AllUsers[userOrGroupName]); //清空列表项原有权限 nrole.RoleDefinitionBindings.RemoveAll(); //取得站点的权限定义 SPRoleDefinitionCollection roleDefinitions = list.ParentWeb.RoleDefinitions; foreach (SPRoleType type in types) { SPRoleDefinition rdnew = roleDefinitions.GetByType(type); //绑定权限定义 nrole.RoleDefinitionBindings.Add(rdnew); } //添加角色 if (!item.HasUniqueRoleAssignments) { item.BreakRoleInheritance(true); } item.RoleAssignments.Add(nrole); item.Update(); } /// <summary> /// 给条目授与一个权限 /// </summary> /// <param name="itemId"></param> /// <param name="userOrGroupName"></param> /// <param name="types"></param> public void AssignRight(int itemId, string userOrGroupName, SPRoleType type) { SPSite sps = yesinda.yesindakms.sharepoint.List.ListLib.findParamSite("/dept/gsb/", this.Context); sps.AllowUnsafeUpdates = true; SPWeb spw = sps.OpenWeb(); spw.AllowUnsafeUpdates = true; SPList list = spw.Lists["文档模板"]; //取得定义的ID SPListItem item = list.GetItemById(itemId); //新的权限集合 SPRoleAssignment nrole = new SPRoleAssignment(list.ParentWeb.AllUsers[userOrGroupName]); //清空列表项原有权限 //nrole.RoleDefinitionBindings.RemoveAll(); //取得站点的权限定义 SPRoleDefinitionCollection roleDefinitions = list.ParentWeb.RoleDefinitions; SPRoleDefinition rdnew = roleDefinitions.GetByType(type); //绑定权限定义 nrole.RoleDefinitionBindings.Add(rdnew); //添加角色 if (!item.HasUniqueRoleAssignments) { item.BreakRoleInheritance(true); } item.RoleAssignments.Add(nrole); item.Update(); } #endregion #region 上传文档 /// <summary> /// 上传文档到文档库 /// </summary> /// <param name="filename"></param> protected void UpLoadDocument() { //上传附件添加到文档库,并返回ID,赋给model.Mattachment string filename = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("/") + 1); Stream filedataStream = FileUpload1.PostedFile.InputStream; int dataLen = FileUpload1.PostedFile.ContentLength; string fileType = FileUpload1.PostedFile.ContentType; byte[] fileData = new byte[dataLen]; filedataStream.Read(fileData, 0, dataLen); SPSite sps = yesinda.yesindakms.sharepoint.List.ListLib.findParamSite("/dept/gsb/", this.Context); sps.AllowUnsafeUpdates = true; SPWeb spw = sps.OpenWeb(); spw.AllowUnsafeUpdates = true; SPList list = spw.Lists[this.ddlDocTitle1.SelectedItem.Text]; SPFolder folder = list.RootFolder; bool ex = false; if (folder.Exists) { try { ex = folder.Files[filename].Exists; Response.Write("<script language="javascript">alert('已有相同名称的文件存在');</script>"); return; } catch { folder.Files.Add(filename, fileData, true); } } } #endregion #region 创建文件夹 /// <summary> /// 在指定的文档库中创建文件夹 /// </summary> /// <param name="folderUrl"></param> /// <param name="folderName"></param> protected void CreateFolder(string folderUrl,string folderName) { SPSite sps = yesinda.yesindakms.sharepoint.List.ListLib.findParamSite("/dept/gsb/", this.Context); sps.AllowUnsafeUpdates = true; SPWeb web = sps.OpenWeb(); web.AllowUnsafeUpdates = true; SPFolder parent = web.GetFolder(folderUrl); if (parent.Exists) { SPList l = web.Lists.GetList(parent.ParentListId, true); SPListItem item = l.Folders.Add(folderUrl, SPFileSystemObjectType.Folder); item["名称"] = "fsfd"; item.Update(); } } #endregion /// <summary> /// 执行文档上传 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { if (this.FileUpload1.FileName != "") { UpLoadDocument(); } } /// <summary> /// 选择文档库的路径 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void ddlDocTitle2_SelectedIndexChanged(object sender, EventArgs e) { Url = this.ddlDocTitle2.SelectedValue; } /// <summary> /// 将一个文档库中的文档发送到指定文档库 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnCopy_Click(object sender, EventArgs e) { SendDocument(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //e.Row.Cells[3].Attributes.Add("style", "display:none"); e.Row.Cells[2].Attributes.Add("style", "display:none"); } protected void Button2_Click(object sender, EventArgs e) { ArrayList arylst = new ArrayList(); for (int i = 0; i < this.CheckBoxList1.Items.Count; i++) { if (this.CheckBoxList1.Items[i].Selected) { arylst.Add(this.CheckBoxList1.Items[i].Value); } } //给每个文档库中的一条设置用户权限 SPRoleType[] types = new SPRoleType[] { SPRoleType.Administrator,SPRoleType.Contributor,SPRoleType.Reader,SPRoleType.WebDesigner }; for (int j = 0; j < arylst.Count; j++) { AssignRight(9, this.txtName.Text.Trim(), types[int.Parse(arylst[j].ToString())]); } } protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) { ViewState["FromPath"] = this.TreeView1.SelectedNode.Value; BindGrid(); } protected void TreeView2_SelectedNodeChanged(object sender, EventArgs e) { ViewState["ToPath"] = this.TreeView2.SelectedNode.Value; } protected void TreeView3_SelectedNodeChanged(object sender, EventArgs e) { //Session["DocLibName"] = this.TreeView3.SelectedNode.Value; } protected void lnkSendMsg_Command(object sender, CommandEventArgs e) { LinkButton lb = (LinkButton)sender; DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent; GridViewRow gvr = (GridViewRow)dcf.Parent; GridView1.SelectedIndex = gvr.RowIndex; doAddMessageSendReceive(301, DateTime.Now,gvr.Cells[2].Text,"301", gvr.Cells[3].Text); } /// <summary> /// 新增消息 /// </summary> protected void doAddMessageSendReceive(int receiveid,DateTime sendtime,string msgtitle,string to,string content) { Yesidea.Model.Message_SendList modelMessage_SendList = new Yesidea.Model.Message_SendList(); Yesidea.BO.Message_SendList boMessage_SendList = new Yesidea.BO.Message_SendList(); modelMessage_SendList.Sender = receiveid; modelMessage_SendList.SendTime = sendtime; modelMessage_SendList.Title = msgtitle; modelMessage_SendList.Ricivers = to; modelMessage_SendList.MContent = content; modelMessage_SendList.Notes = "无"; string[] arr = null; arr = GetSplitString(to, ","); boMessage_SendList.AddMessage(modelMessage_SendList, arr); Response.Write("<script language='javascript'>alert('发送成功');</script>"); } /// <summary> /// 拆分字符串成字符串数组 /// </summary> /// <param name="list"></param> /// <param name="gap"></param> /// <returns></returns> private string[] GetSplitString(string list, string gap) { char[] delimiter = gap.ToCharArray(); string[] report = null; for (int i = 1; i <= list.Length; i++) //把用逗号隔开的字符串拆分成字符串数组report { report = list.Split(delimiter, i); } return report; } protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GridView2.PageIndex = e.NewPageIndex; this.GridView2.DataKeyNames = new string[] { "文档库ID" }; this.GridView2.DataSource = MyDataSource(); this.GridView2.DataBind(); } /// <summary> /// 选择路径 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void ddlDocTitle2_SelectedIndexChanged1(object sender, EventArgs e) { Url = this.ddlDocTitle2.SelectedItem.Value; } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GridView1.PageIndex = e.NewPageIndex; if (ViewState["FromPath"] != null) { this.GridView1.DataKeyNames = new string[] { "文档库ID" }; this.GridView1.DataSource = CreateDataSource(ViewState["FromPath"].ToString()); this.GridView1.DataBind(); } } protected void Button1_ServerClick(object sender, EventArgs e) { SPSite sps = yesinda.yesindakms.sharepoint.List.ListLib.findParamSite("/dept/gsb/", this.Context); sps.AllowUnsafeUpdates = true; SPWeb web = sps.OpenWeb(); web.AllowUnsafeUpdates = true; // Create folder as subcolumn string folderUrl = ColumnTreeBox1.CurrentValue.Trim(); SPFolder parent = web.GetFolder("/dept/gsb/DocLib5/"); Label1.Text = Label1.Text + parent.Url.ToString() + "<br>"; if (parent.Exists) { SPList l = web.Lists.GetList(parent.ParentListId, true); SPDocumentLibrary doc = (SPDocumentLibrary)l; SPListItem item = l.Items.Add("/dept/gsb/DocLib5", SPFileSystemObjectType.Folder); Response.Write(siteUrl + folderUrl); item["名称"] = "fsfd"; item.Update(); string parentUrl = parent.Url; string currentUrl = parentUrl + "/" + name; try { SPList infoList = web.Lists["栏目信息表"]; SPListItem newItem = infoList.Items.Add(); newItem["全路径"] = currentUrl; newItem["栏目名"] = name; newItem["是否顶级"] = false; if (CheckBox2.Checked) newItem["是否汇总"] = true; if (CheckBox3.Checked) newItem["是否审核"] = true; if (!CheckBox4.Checked) newItem["是否显示"] = false; if (CheckBox5.Checked) newItem["URL"] = TextBox2.Text; //以上if语句省去else,因为列表项有默认值 newItem.Update(); } catch (Exception ex) { Response.Write(ex.Message); } } } } |
|
来自: 昵称5013180 > 《WorkStudy》