chaosxin 发表于 2015-9-27 08:57:16

最近在做SharePoint开发时积累的几点知识

  1、操作列表批量删除列表项 

代码

#region 批量删除
                  //查询出所要删除的列表项
                  query.Query = @"<Where><Eq><FieldRef Name='" + list.Fields["RoleID"].InternalName + @"'/>
                                        <Value Type='Text'>" + this.RoleID + "</Value></Eq></Where>";    //查询条件
                  SPListItemCollection items = list.GetItems(query);

                  StringBuilder sbDelete = new StringBuilder();
                  sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
                  foreach (SPListItem item in items)
                  {
                        sbDelete.Append("<Method>");
                        sbDelete.Append("<SetList Scope=\"Request\">" + list.ID + "</SetList>");
                        sbDelete.Append("<SetVar Name=\"ID\">" + Convert.ToString(item.ID) + "</SetVar>");
                        sbDelete.Append("<SetVar Name=\"Cmd\">Delete</SetVar>");
                        sbDelete.Append("</Method>");
                  }
                  sbDelete.Append("</Batch>");
                  //删除操作
                  sweb.ProcessBatchData(sbDelete.ToString());   
                  
                  #region 注释循环删除列表项
                  //for (int i = 0; i < items.Count; i++)
                  //{
                  //    //aa += items["ModuleID"].ToString() + "<br>";
                  //    //items.Delete();
                  //    items.de();
                  //}
                  #endregion
                  #endregion  
  2、循环添加列表项
  


代码

#region 批量添加
                  string strModuleID = this.txt_ModuleID.Value.Trim();
                  if (!strModuleID.Equals(""))
                  {
                        SPListItem sListItem=null;
                        string[] moduleIDList = strModuleID.Split(',');
                        foreach (string moduleID in moduleIDList)
                        {
                            sListItem = list.Items.Add();         
                            sListItem["aa"] = this.aa;      
                            sListItem["bb"] = bb;      
                            sListItem.Update();                     
                        }
                  }
                  #endregion  
  3、Linq查询几点(多表联合查询、In子查询、过滤重复记录)
  

代码

this.PersonNameCollection = "小李,小王";
            DataTable dtPerson = web.Lists["Person"].Items.GetDataTable();
            DataTable dtCustion = web.Lists["Custion"].Items.GetDataTable();
            DataTable dtPersonCustion = web.Lists["PersonCustion"].Items.GetDataTable();
            var qList = (from Person in dtPerson.AsEnumerable()
                         join PersonCustion in dtPersonCustion.AsEnumerable() on Person.Field<string>("PersonID") equals PersonCustion.Field<string>("PersonID")
                         join Custion in dtCustion.AsEnumerable() on PersonCustion.Field<string>("CustionID") equals Custion.Field<string>("CustionCode")
                         where this.PersonNameCollection.Contains(Person.Field<string>("Title"))   //实现In子查询
                         orderby Custion.Field<string>("CustionCode")
                         select new
                         {
                           ParentCustionID = PersonCustion.Field<string>("CustionID").Substring(0, 2),
                           CustionID = PersonCustion.Field<string>("CustionID"),
                           CustionName = Custion.Field<string>("Title"),
                           CustionURL = Custion.Field<string>("URL")
                         }).Distinct();   //并返回不重复记录  
  
页: [1]
查看完整版本: 最近在做SharePoint开发时积累的几点知识