• SharePoint中的时间问题(转)


    CAML

    items due before 15 days after today.

    <SharePoint:SPDataSource
       ...
       selectcommand="<View><Query>
       <OrderBy><FieldRef Name='Date_Due' Ascending='TRUE'/></OrderBy>
       <Where><Leq><FieldRef Name='Date_Due'/><Value Type='DateTime'><Today OffsetDays='15'/></Value>
       </Leq></Where></Query></View>" 
       ...
    >

         

    [Today+3Day(s)]对时间的筛选

         caml查询的时候默认是不精确匹配时间的,需要加上一个属性,就是
    IncludeTimeValue='true'
    使得caml查询变成

    <Where>
                                               

                                                  
    <Eq>
                                                    
    <FieldRef Name='_ModerationStatus' />
                                                    
    <Value Type='ModStat'>已批准Value>
                                                 
    Eq>
                                                  
    <Leq>
                                                     
    <FieldRef Name='" + list.Fields["公告发布时间"].InternalName + @"' />
                                                     
    <Value Type='DateTime' IncludeTimeValue='true'>+ DateTime.Now.ToString() + @"ZValue>
                                                  
    Leq>
                                                

                                           
    Where>

    这样再查询的话,就是精确匹配时间的查询了。

    在页面的时间栏中填写的时间如果是
    2008-8-8 0:00
    然后保存页面,用程序访问刚才的列表项,然后显示刚才的时间,却显示为2008-8-8 8:00.
    刚开始我也很是奇怪呢,后来才发现,估计是时区在作怪,sharepoint内部存储时间都是用那个什么格陵时间吧,可能是为了统一。然后在页面显示的时候才显示为本地时间。

    用下面的函数可以转化本地时间为标准时间
    DateTime.Now.ToUniversalTime().ToString()

    还有就是容易搞混的
    8月24号的24:00就是8月25号的0:00
    这个问题把我弄混好多次了,几乎每次都中招。

    下面的代码是处理过期的新闻测试代码,在sharepoint中的过期新闻,被我从“页面(Pages)”列表库移动到了“过期新闻(DocLib)”列表库


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;

    namespace ConsoleApplication1
    {
        
    class Program
        {


            
    static void Main(string[] args)
            {
                SPSecurity.RunWithElevatedPrivileges(
                                                   
    delegate()
                                                   {
                                                       
    using (SPSite site = new SPSite(@"http://virus/sites/intranet"))
                                                       {
                                                           
    using (SPWeb web = site.AllWebs["team"])
                                                           {
                                                               web.AllowUnsafeUpdates 
    = true;

                                                               SPList list 
    = web.Lists["页面"];
                                                               
    foreach (SPListItem item in list.Items)
                                                               {
                                                                   Console.WriteLine(
    "名称:" + item.GetFormattedValue("名称"+ "结束日期:" + item.GetFormattedValue("结束日期"));
                                                                   Console.WriteLine(DateTime.Now.ToUniversalTime().ToString());
                                                               }

                                                              
    // SPList list = web.Lists["页面"];
                                                               SPListItemCollection items = null;
                                                               SPQuery query 
    = new SPQuery();
                                                                 query.Query 
    = @"<Where>
                                                                  <Leq>
                                                                     <FieldRef Name='
    " + list.Fields["结束日期"].InternalName + @"' />
                                                                     <Value Type='DateTime'>
    " + DateTime.Now.ToString() + @"Z</Value>
                                                                  </Leq>
                                                               </Where>
    ";
                                                               items 
    = list.GetItems(query);
                                                               
    foreach (SPListItem item in items)
                                                               {
                                                                   item.CopyTo(web.Url 
    + "/DocLib/" + item.Name);
                                                                   
    //item.Delete();
                                                               }

                                                               
    //#region 从Pages(中文名字:页面)列表库拷贝/移动文件到DocLib(中文名字:过期新闻)列表库
                                                               
    //SPList list = web.Lists["页面"];
                                                               
    //foreach (SPListItem item in list.Items)
                                                               
    //{
                                                               
    //    //列表项从一个列表库拷贝到另外一个列表库,列表项不支持移动(MoveTto)
                                                               
    //    item.CopyTo(web.Url + "/DocLib/" + item.Name);
                                                               
    //}
                                                               
    //#endregion

                                                               
    //#region 从Pages(中文名字:页面)文档库拷贝/移动文件到DocLib(中文名字:过期新闻)文档库
                                                        
    //       SPFolder oFolder = web.GetFolder("Pages");
                                                        
    //       SPFileCollection collFiles = oFolder.Files;

                                                        
    //       for (int intIndex = collFiles.Count - 1; intIndex > -1; intIndex--)
                                                        
    //       {
                                                        
    //           //文档从一个文档库拷贝到另一个文档库
                                                        
    //               collFiles[intIndex].CopyTo(web.Url+"/DocLib/" + collFiles[intIndex].Name);
                                                        
    //               //文档从一个文档库移动到另一个文档库
                                                        
    //           collFiles[intIndex].MoveTo(web.Url+"/DocLib/" + collFiles[intIndex].Name);

                                                        
    //       }

                                                        
    //#endregion
                                                               web.AllowUnsafeUpdates = false;
                                                           }
                                                       }
                                                   });

                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    899. Orderly Queue
    856. Score of Parentheses
    833. Find And Replace in String
    816. Ambiguous Coordinates
    770. Basic Calculator IV
    冒泡排序(Bubble Sort)
    C
    B
    A
    HDOJ-1391
  • 原文地址:https://www.cnblogs.com/ceci/p/1425911.html
Copyright © 2020-2023  润新知