moss coding Library

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Security.Principal;

namespace SolutionList
{
    [Guid("21e88a39-300a-4421-a374-9a3efc1fa185")]
    public class SolutionList : System.Web.UI.WebControls.WebParts.WebPart
    {
        public SolutionList()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        #region     //控件变量声明
        Label lbl = new Label();
        Label lblname = new Label();
        Label lblsite = new Label();
        Label lblprincipal = new Label();
        Label lbltitle = new Label();
        TextBox txttitle = new TextBox();
        Button btnupdate = new Button();
        Button btnadd = new Button();
        #endregion  //end变量
        private string _username = null;
        public string UserName
        {
            get { return _username; }
            set { _username = value; }
        }

        #region //获取登录人员信息
        /// <summary>
        /// 获取当前网站登录用户
        /// </summary>
        private void getUser()
        {
            SPSite spSite = new SPSite("http://server163:4540/");           
            SPWeb web = spSite.RootWeb;
            SPUserCollection users = web.Users;
            //web.currentuser   这个有很多当前用户的属性   想要显示那个就用那个属性
            UserName = web.CurrentUser.Name;
            SPUser user = users[UserName];
            string sid = user.Sid;
            SPGroupCollection groups = user.Groups;
            string groupname = null;
            foreach (SPGroup group in groups)
            {
                groupname += group.Name;
                bool flag=group.CanCurrentUserManageGroup;
                group.AddUser("liudehua", "[email protected]", "jason", "everyone is ok");

            }
        }

        /// <summary>
        /// 获取当前登陆用户的登陆名
        /// </summary>
        private void getUserInfo()
        {

            WindowsPrincipal wp = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal;
            string wpname = wp.Identity.Name.ToString();
            int j = wpname.LastIndexOf("\\");
            string userName = wpname.Substring(j + 1);
            string domainName = wpname.Substring(0, j);
            //也可以获取登录名
            //string strUserName = this.Page.User.Identity.Name;   
        }
        #endregion  //end 获取登录人员信息
        //修改列表信息
        public void UpdateList(object sender ,EventArgs e)
        {
            SPSite spsite=new SPSite("http://server163:4540/");
            SPWeb sw = spsite.RootWeb;

            SPListCollection splist = sw.Lists;
            SPList stulist = splist["StuInfo"];
                       
            sw.AllowUnsafeUpdates = true;
            SPListItemCollection items = stulist.Items;
                       
            //修改所有item的title
            foreach (SPListItem item in items)
            {
                               
                item["remark"] = txttitle.Text;
                item.Update();
            }
                       
        }
        //修改Field的属性
        public void UpdateField(object sender, EventArgs e)
        {
            SPSite spsite = new SPSite("http://server163:4540/");
            SPWeb sw = spsite.RootWeb;
            SPList slist = sw.Lists["StuInfo"];
                      
            SPFieldCollection fieldcoll = slist.Fields;
            SPField sfield = fieldcoll["address"];
            sfield.DefaultValue = "address is ok";
            sfield.Update();

        }
        //添加列表信息
        public void AddList(object sender, EventArgs e)
        {
            SPSite spsite = new SPSite("http://server163:4540/");
            SPWeb sw = spsite.RootWeb;
            //SPSite site = SPContext.Current.Site;
            ////SPWebCollection swcoll = site.AllWebs;
            //SPWeb sw = site.AllWebs["公路实况"];

            SPListCollection splist = sw.Lists;
            SPList stulist = splist["StuInfo"];

            SPListItem newitem = stulist.Items.Add();
            newitem["name"] = txttitle.Text;
            newitem["sex"] = "男";
            newitem["age"] = 15;
            newitem["address"]="liaoning shenyang";
            newitem.Update();
        }
        //向列表添加一个新的Field
        public void AddField(object sender, EventArgs e)
        {
            getUser();
            SPSite spsite = new SPSite("http://server163:4540/");
            SPWeb sw = spsite.RootWeb;
            SPList slist = sw.Lists["StuInfo"];
          
            string newfield = slist.Fields.Add(txttitle.Text, SPFieldType.Text, false);
            slist.DefaultView.DefaultViewForContentType = true;
            SPFieldText field = (SPFieldText)slist.Fields[txttitle.Text];
            field.Title = txttitle.Text;
            field.Update();

        }
        //获取列表信息
        public void GetListInfo(object sender, EventArgs e)
        {
            SPSite spsite = new SPSite("http://server163:4540/");
            SPWeb sweb = spsite.RootWeb;
            SPQuery sq = new SPQuery();
            //sq.RowLimit = 5;
            //<Where>   | Where
            //<OrderBy> | orderby
            //<And>     | and
            //<Or>      | or
            //<Leq>     | <=
            //<Geq>     | >=
            //<Gt>      | >
            //<Lt>      | <
            //<Neq>     | !=
            //<Eq>      | =
            //<IsNull>  | is null
            sq.Query="<Where><Leq><FieldRef Name='age'/><Value Type='number'>20</Value></Leq></Where>";
            SPList stulist = sweb.Lists["StuInfo"];
            SPListItemCollection items = stulist.GetItems(sq);
            lbl.Text = "一个有"+items.Count.ToString() +"条记录!";
            string name = "";
            foreach (SPListItem item in items)
            {
                name += item["name"].ToString()+"--";
            }
            lblname.Text = name;
        }
        //利用SPFolder和SPFile 获取文件
        public void GetDoc(object sender, EventArgs e)
        {
            SPSite spsite = new SPSite("http://server163:4540/");
            SPWeb sw = spsite.RootWeb;                   
            //获取网站的一级文档目录    
            SPFolder sFolder = sw.GetFolder("info");
            //获取子文件夹
            SPFolderCollection subfolder = sFolder.SubFolders;
            SPFileCollection collFiles = sw.GetFolder("liaoningdocumentlib").Files;
            //读取子文件夹的文件          
            foreach (SPFolder folder in subfolder)
            {
                //排出系统文件夹
                if (folder.Name != "Forms")
                {
                    if (folder.Name == "文档")
                    {
                        folder.Delete();
                        continue;
                    }
                    string url = folder.Url;
                    string parname = folder.ParentFolder.Name;
                    //循环读出文件
                    foreach (SPFile file in folder.Files)
                    {
                        bool indoc = file.InDocumentLibrary;//判断是否在文档库里                
                        string newurl = collFiles.Folder.Url + "/" + file.Name;
                        byte[] bytefile = file.OpenBinary();
                        collFiles.Add(newurl, bytefile, true);
                    }
                }
            }
            //读取文件夹根目录的文件          
            foreach (SPFile sFile in sFolder.Files)
            {
                if (sFile.TimeLastModified < Convert.ToDateTime("10/7/2008 12:00:00 AM"))
                {
                    string strDestURL = collFiles.Folder.Url + "/" + sFile.Name;
                    byte[] binsFile = sFile.OpenBinary();

                    collFiles.Add(strDestURL, binsFile, true);
                }
            }
                                             
        }
           
        protected override void CreateChildControls()
        {
            #region //登录信息
            SPSite spSite = new SPSite("http://server163:4540/");
            SPWeb web = spSite.RootWeb;
            lblsite.Text = "name:" + web.CurrentUser.Name + ";email:"
                + web.CurrentUser.Email + ";ID:" + web.CurrentUser.ID
                + ";SID:" + web.CurrentUser.Sid + ";roles:"
                //+ web.CurrentUser.Roles + ";LoginName:"
                + web.CurrentUser.LoginName + ";owngroup:"
                + web.CurrentUser.OwnedGroups;

            WindowsPrincipal wp = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal;
            string wpname = wp.Identity.Name.ToString();
            int j = wpname.LastIndexOf("\\");
            string userName = wpname.Substring(j + 1);
            string domainName = wpname.Substring(0, j);
            lblprincipal.Text = domainName + ";" + userName+"  \r";
            //this.Controls.Add(lblprincipal);
            //this.Controls.Add(lblsite);
            #endregion

            lbltitle.Text = "输入修改标题:";
            btnupdate.Text = "UPDATE";
            btnupdate.Click += new System.EventHandler(UpdateField);
            btnadd.Text = "Add";
            btnadd.Click += new System.EventHandler(AddField);
           
            Button btnget = new Button();
            btnget.Text = "Query";
            btnget.Click += new EventHandler(GetDoc);

           
            this.Controls.Add(lbltitle);
            this.Controls.Add(txttitle);
            this.Controls.Add(btnadd);
            this.Controls.Add(btnupdate);
            this.Controls.Add(btnget);
            this.Controls.Add(lbl);
            this.Controls.Add(lblname);

            base.CreateChildControls();
        }
    
    }
}

你可能感兴趣的:(library)