在SharePoint中对文档库(列表)进行的一些操作

以下是我在开发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;
        }

    }


    为TreeView添加节点
为TreeView添加节点

    绑定TreeView控件
绑定TreeView控件

    创建数据源
创建数据源

    绑定控件
#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>
    
/// <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(
9this.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);
            }

        }

    }

}



转自:http://www.cnblogs.com/jhobo/archive/2007/06/06/773647.html

你可能感兴趣的:(SharePoint)