zoukankan      html  css  js  c++  java
  • MOSS中对列表的一些操作(创建,查询等)

    1.查询列表的所有字段

    SPSite site = new SPSite("http://carysun");
    SPWeb web=site.OpenWeb();
    SPList list = web.GetList("/IT Infrastructure");
    foreach (SPField sf in list.Fields)
    {
        Console.WriteLine(sf.Title);
    }
    2.使用对象模型创建列表,SPListTemplateType.Announcements指定使用通知内容类型作为模板来创建。

    注意一定要调用Update()方法。

    string listName="AnnouList";                    
    foreach(SPList currList in web .Lists)
    {      
       if(currList.Title.Equals(listName,StringComparison.InvariantCultureIgnoreCase))
       {
          list=currList;
          break;
       }
    }
    if(list==null)
    {
         Guid listID=web.Lists.Add(listName,"New nnouncements",SPListTemplateType.Announcements);
         list=web.Lists[listID];
         list.OnQuickLaunch=true;
         list.Update();
     }

    3.使用对象模型给列表添加item,注意一定要调用Update()方法。

    SPListItem newItem = null;
    newItem = list.Items.Add();
    newItem["Title"] = "AnnouItem1";
    newItem["Body"] = "The first AnnouItem1 ";
    newItem["Expires"] = DateTime.Now + TimeSpan.FromDays(2);
    newItem.Update();
    
    newItem = list.Items.Add();
    newItem["Title"] = "AnnouItem2";
    newItem["Body"] = "The second AnnouItem2.";
    newItem["Expires"] = DateTime.Now + TimeSpan.FromDays(5);
    newItem.Update();

















    4.查询item的相关信息
    foreach (SPListItem listItem in list.Items)
    {
          foreach (SPField field in list.Fields)
          {
              if (!field.Hidden && !field.ReadOnlyField)
                    Console.WriteLine("{0} = {1}", field.Title, newItem[field.Id]);
           }
    }

    5. 如果你要想得到一个列表的item,你可以通过WebId, ListId, and ID来得到.

    SPWeb parentWeb = web.Site.OpenWeb(new Guid(row["WebId"].ToString()));
    SPList list = parentWeb.Lists[ new Guid(row["ListId"].ToString()) ];
    SPListItem item = list.GetItemById((int.Parse(row["ID"].ToString())));

     
    6.使用SPQuery来查询列表中item信息:

    SPQuery query = new SPQuery();
    query.ViewFields = @"<FieldRef Name='Title'/><FieldRef Name='Created'/>";
    query.Query = @"<Where>
           <Neq>
             <FieldRef Name='Created' />
             <Value Type='DateTime'>
             <Today /></Value>
           </Neq>
        </Where>";
    
    SPList list = web.Lists["AnnouList"];
    SPListItemCollection items = list.GetItems(query);
    foreach (SPListItem expiredItem in items)
    {
        Console.WriteLine(expiredItem["Title"]);
        Console.WriteLine(expiredItem["Created"]);
    }

    6.1. ViewFields 表示你查询后要返回的字段

    6.2. Query表示查询过滤的表达式,使用CAML语言

    7. 使用SPSiteDataQuery来查询列表中item信息

    SPSiteDataQuery query = new SPSiteDataQuery();
    query.Lists = @"<Lists ServerTemplate='104' />";
    query.ViewFields = @"<FieldRef Name='Title'/><FieldRef Name='Created'/>";
    query.Webs = "<Webs Scope='SiteCollection' />";
    string queryText =@"<Where>
             <Neq>
               <FieldRef Name='Created' />
               <Value Type='DateTime'>
               <Today /></Value>
             </Neq>
         </Where>";
    query.Query = queryText;
    DataTable table = web.GetSiteData(query);
    foreach (DataRow row in table.Rows)
    {
         Console.WriteLine(row["Title"].ToString() + row["Created"].ToString());
    }

    7.1. query.Lists = @"<Lists ServerTemplate='104' />";中104代表通知列表类型

    7.2 query.Webs = "<Webs Scope='SiteCollection' />";为查询的范围。

    7.3. CAML的基本格式是这样的:“<Where><operator><operand /><operand /></operator> </Where>”.

    7.4.使用SPQuery返回的是SPListItemCollection,而SPSiteDataQuery可以从不同的列表或是整个网站集查,实际上是返回了一个ADO.NET DataTable对象。

    7.5 下表是CAML查询的一些简单说明:

    元素 说明
    And 并且
    BeginsWith 以某字符串开始的
    Contains 包含某字符串
    Eq 等于
    FieldRef 一个字段的引用 (在GroupBy 中使用)
    Geq 大于等于
    GroupBy 分组
    Gt 大于
    IsNotNull 非空
    IsNull
    Leq 小于等于
    Lt 小于
    Neq 不等于
    Now 当前时间
    Or
    OrderBy 排序
    Today 今天的日期
    TodayIso 今天的日期(ISO格式)
    Where Where子句
  • 相关阅读:
    linux 学习笔记1
    IIS请求筛选模块被配置为拒绝超过请求内容长度的请求
    ipod锁定后的恢复
    HTTP报文
    数据仓库概念
    数据挖掘概念
    大数据处理工具
    eclipse 4.3 汉化
    在CentOS中安装输入法
    编译Hadoop1.1.2eclipse插件并测试
  • 原文地址:https://www.cnblogs.com/IsNull/p/1821064.html
Copyright © 2011-2022 走看看