C#CSOM CamlQuery日历返回错误的EventDate时间 [英] C# CSOM CamlQuery Calendar Returning Wrong EventDate Time

查看:125
本文介绍了C#CSOM CamlQuery日历返回错误的EventDate时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在从外部应用程序查询我们的在线SharePoint网站的日历列表。 为了使这与安全性一起使用,我使用的是Oauth客户端/令牌和服务器端CSOM。 无论如何,我可以抓住列表,创建查询并获取结果。 
但是,由于某种原因,默认的EventDate字段时间不正确。 我不知道我错过了什么,但是非常感谢任何帮助:

 using(var clientContext = TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken))
{
Web currentWeb = clientContext.Web;

var spList = clientContext.Web.Lists.GetByTitle(" Class Offerings");

CamlQuery query = new CamlQuery();

var queryString ="< View>" +
"< Query>" +
"< Where>" +
"< Eq>" +
"< FieldRef Name ='Published'/>" +
"< Value Type ='Bool'> True< / Value>" +
"< / Eq>" +
"< / Where>" +
"< OrderBy>" +
"< FieldRef Name ='Category'/>" +
"< FieldRef Name ='Title'/>" +
"< FieldRef Name ='EventDate'/>" +
"< FieldRef Name ='EndDate'/>" +
"< / OrderBy>" +
"< / Query>" +
"< / View>" ;;

query.ViewXml = queryString;

ListItemCollection items = spList.GetItems(query);
//从List.GetItems(Query)中检索ListItemCollection中的所有项目。
clientContext.Load(items);

clientContext.ExecuteQuery();
foreach(项目中的ListItem项目)
{
// item [" EventDate"]具有正确的日期,但错误/奇怪的时间。例如
//我将列表项的事件日期设置为11/15/2017下午1:00。当我在调试模式下查看项目
//它显示11/15/2017 9:00 pm
}
}

我添加了将日期时间字段添加到日历列表,然后将项目上的时间设置为上午7:00。 我打了电话,项目的时间显示为下午3点。 我注意到的一件事是,这些字段全部关闭了8个小时。


 





解决方案


SharePoint将日期时间存储为UTC,我们需要使用下面的代码片段将EventDate转换为本地时间:

 var localTime = ctx.Web.RegionalSettings.TimeZone.UTCToLocalTime(DateTime.Parse(item [" EventDate"]。ToString())); 
ctx.ExecuteQuery();
DateTime eventDate = localTime.Value;

更多信息:


https://blogs.u2u.be/u2u/post/Watch-out-with-calculated-DateTime- field-in-CSOM


使用的客户端对象模型-C -sharepoint场-https://piyushksingh.com/2014/02/27/get-the-utc-datetime-of-a-sharepoint-field-using-client -object-model-c /


最好的问候,


Dennis


Hi,

I'm querying a calendar list from our Online SharePoint site from an external application.  To get this to work with security, I'm using Oauth client/token and server-side CSOM.  Anyway, I can grab the list, create the query and get back results.  However, for some reason, the default EventDate field time is not correct.  I don't know what I'm missing, but any help would be greatly appreciated:

            using (var clientContext = TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken))
            {
                Web currentWeb = clientContext.Web;

                var spList = clientContext.Web.Lists.GetByTitle("Class Offerings");

                CamlQuery query = new CamlQuery();

                var queryString = "<View>" +
                                    "<Query>" +
                                        "<Where>" +
                                            "<Eq>" +
                                                "<FieldRef Name='Published' />" +
                                                "<Value Type='Bool'>True</Value>" +
                                            "</Eq>" +
                                        "</Where>" +
                                        "<OrderBy>" +
                                            "<FieldRef Name='Category' />" +
                                            "<FieldRef Name='Title' />" +
                                            "<FieldRef Name='EventDate' />" +
                                            "<FieldRef Name='EndDate' />" +
                                        "</OrderBy>" +
                                    "</Query>" +
                                  "</View>";

                query.ViewXml = queryString;

                ListItemCollection items = spList.GetItems(query);
                // Retrieve all items in the ListItemCollection from List.GetItems(Query). 
                clientContext.Load(items);

                clientContext.ExecuteQuery();
                foreach (ListItem item in items)
                {
                    // item["EventDate"] has the correct Date, but wrong/odd time.  For example
                    // The I set the Event Date on a List Item to 11/15/2017 1:00pm.  When I view the item
                    // in debug mode it shows 11/15/2017 9:00pm  
                }
            }

I added an additional DateTime field to the Calendar list then Set the time on the item to 7:00am.  I made my call and the item's time shown as 3:00pm.  One thing I noticed is that the fields are all off by 8 hours.

 


解决方案

Hi,

SharePoint store the date time as UTC, we need convert the EventDate to local time using the code snippet below:

var localTime = ctx.Web.RegionalSettings.TimeZone.UTCToLocalTime(DateTime.Parse(item["EventDate"].ToString()));
ctx.ExecuteQuery();
DateTime eventDate = localTime.Value;

More information:

https://blogs.u2u.be/u2u/post/Watch-out-with-calculated-DateTime-fields-in-CSOM

https://piyushksingh.com/2014/02/27/get-the-utc-datetime-of-a-sharepoint-field-using-client-object-model-c/

Best Regards,

Dennis


这篇关于C#CSOM CamlQuery日历返回错误的EventDate时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆