gpt4 book ai didi

sharepoint - SharePoint中的重复事件-“Duration”不正确

转载 作者:行者123 更新时间:2023-12-02 03:49:54 25 4
gpt4 key购买 nike

背景:
我工作的公司有一个常规SharePoint列表,该列表具有用于事件的自定义ContentType(不继承日历列表项)。然后使用日历 View 显示这些内容。似乎很简单。
我们需要允许用户为他们要添加的事件选择一个时区(与他们的区域设置不同),并将信息添加到共享点,这样它将为每个在世界范围内查看该事件的用户显示正确的时间(根据他们的地区设置而定)。
我向SharePoint添加了一个列表,该列表用于查找SystemTimeZones(基本上是TimeZoneInfo.GetSystemTimeZones()的SharePoint列表表示形式)

                SPList timeZonesList = thisWeb.Lists.TryGetList("SystemTimeZones");

if(timeZonesList == null)
{
string title = "SystemTimeZones";
string description = "SharePoint List representation of TimeZoneInfo.GetSystemTimeZones() used for lookup.";

Guid newListId = thisWeb.Lists.Add(title, description, SPListTemplateType.GenericList);

timeZonesList = thisWeb.Lists.GetList(newListId, true);
timeZonesList.Fields.Add("SystemTimeZoneId", SPFieldType.Text, true);
timeZonesList.Fields.Add("SystemTimeZoneName", SPFieldType.Text, true);

SPView defaultTimeZonesView = timeZonesList.DefaultView;
defaultTimeZonesView.ViewFields.Add("SystemTimeZoneId");
defaultTimeZonesView.ViewFields.Add("SystemTimeZoneName");
defaultTimeZonesView.Update();

foreach (TimeZoneInfo timeZone in TimeZoneInfo.GetSystemTimeZones())
{
SPListItem temp = timeZonesList.AddItem();
temp["SystemTimeZoneId"] = timeZone.Id;
temp["SystemTimeZoneName"] = timeZone.DisplayName;
temp.Update();
}
}

我在此列表的自定义添加和编辑表单中将此列表用于EventTimeZone的查找项目。表单是SharePoint Designer将创建的内容的直接副本(因为它们使用的是SharePoint:FormField),它们只是在我需要代码隐藏的Visual Studio中。我想允许用户在他们的区域时区中查看事件,但是当他们编辑事件时,我想在输入的时区中显示它们。 (即我所在的时区是中部地区,因此当我参加山区 session 时,它将显示我上午10点至11点,但是当我编辑同一 session 时,它将显示是上午9点至10点)。所以在编辑页面加载时,我会调整时间:
                        SPListItem thisEvent = eventsList.GetItemById(savebutton1.ItemId);

if (thisEvent != null)
{
bool isAllDayEvent = false;
if (thisEvent["fAllDayEvent"] != null)
{
isAllDayEvent = (bool)thisEvent["fAllDayEvent"];
}

if (!isAllDayEvent)
{
SPFieldLookupValue lookupValue = new SPFieldLookupValue(thisEvent["Event Time Zone"].ToString());
TimeZoneInfo eventTimeZone = GetEventTimeZoneByListItemId(lookupValue.LookupId, rootWeb);
SPTimeZone regionalTimeZone = GetRegionalTimeZone(rootWeb);

DateTime regionalStartDateTime = Convert.ToDateTime(thisEvent["StartDate"]);
DateTime originalStartDateTime = TimeZoneInfo.ConvertTimeFromUtc(regionalTimeZone.LocalTimeToUTC(regionalStartDateTime), eventTimeZone);

ff3.ListItemFieldValue = originalStartDateTime;

DateTime regionalEndDateTime = Convert.ToDateTime(thisEvent["EndDate"]);
DateTime originalEndDateTime = TimeZoneInfo.ConvertTimeFromUtc(regionalTimeZone.LocalTimeToUTC(regionalEndDateTime), eventTimeZone);

ff4.ListItemFieldValue = originalEndDateTime;
}
else
{
// for some reason with all day events, sharepoint saves them
// as the previous day 6pm. but when they show up to any user
// they will show as 12am to 1159pm and show up correctly on the calendar
// HOWEVER, when it comes to edit, the start date isn't corrected on the
// form, so continuing to save without fixing it will continue to decrease
// the start date/time by one day
DateTime regionalStartDateTime = Convert.ToDateTime(thisEvent["StartDate"]);
ff3.ListItemFieldValue = regionalStartDateTime.AddDays(1);
}

整天的事件都很奇怪,但是我能够通过编写测试用例并查看发生了什么来使其正常工作(如您从我的评论中看到的那样)。

然后,我将列表事件接收器ItemAdded和ItemUpdated绑定(bind)到“修复”时间,因为SharePoint将根据用户的区域设置(而不是用户选择的时区)保存它们。 (当然,我对SharePoint还是比较陌生的-不是C#-因此我可能对此非常复杂,但是我已经能够在线整理一些文档)。最后我最终设置:
                    addedItem["StartDate"] = regionalTimeZone.UTCToLocalTime(correctedEventStart.ToUniversalTime());
addedItem["EndDate"] = regionalTimeZone.UTCToLocalTime(correctedEventEnd.ToUniversalTime()); TADA!! It saves and display perfectly! I was so excited! Until... I tried to save a recurring event. All of my recurring events save wonderfully, it's not the recurring part that's messed up. For some reason, after I change the StartDate and EndDate on a recurring event and call addedItem.Update() it is recalculating the "Duration" as if it is a single even instead of a recurring event. Example: I have an event that happens for a week daily from 9-10. When I first enter ItemAdded my Duration is 3600 (1 hour) as it should be bc Duration is treated differently for recurring events. However after I adjust the times and call Update() the duration spans the entire week :( If I manually set the Duration:
if (isRecurrence)
{
addedItem["Duration"] = (correctedEventEnd.TimeOfDay - correctedEventStart.TimeOfDay).TotalSeconds;
}

它仍然在Update()上重置。因此,当您在“日历 View ”中查看重复项目时,该项目将跨越整个星期,而不是每天显示一次。

我几乎拔出头发试图解决这个问题。任何指导都是很棒的。我知道Duration是一个计算字段,但我不明白为什么调用listItem.Update()会忽略以下事实,即它确实被正确标记为重复事件,并且无法正确计算Duration。老实说,这似乎是SP 2010的错误。

提前致谢!

**

编辑:下面的评论后的其他信息...

**
此SharePoint env在太平洋时间有一个服务器,其用户遍及美国所有时区,伦敦,东京,阿布达比等。一个时区的用户需要能够在其他时区中创建事件。由于用户个人资料中的任何内容(无论如何对我们而言)都不会告诉我们他们希望看到的时区,因此我们在母版页中添加了代码以查看本地计算机的时区,并始终相应地设置其区域设置。

示例:我在纳什维尔,我想创建一个将在洛杉矶发生的事件:

ItemAdded中的数据显示StartDate是我早上9点输入的内容。因此,我正在创建一个以PST结尾的日期:
DateTime correctedEventStart = DateTime.Parse(addedItem["StartDate"] + " " + eventTimeZone.GetUtcOffset(DateTime.UtcNow).Hours + ":" + eventTimeZone.GetUtcOffset(DateTime.UtcNow).Minutes);
DateTime correctedEventEnd = DateTime.Parse(addedItem["EndDate"] + " " + eventTimeZone.GetUtcOffset(DateTime.UtcNow).Hours + ":" + eventTimeZone.GetUtcOffset(DateTime.UtcNow).Minutes);

然后,“欺骗” SharePoint,我将PST时间转换为用户的区域时间(这样,用户不必知道有关其区域设置的任何信息,也不必思考)。因此,太平洋标准时间(PST)上午9点是CST上午7点(BC是SharePoint期望的时间,因为这是我的区域设置)。这是从正确的时区+时区到用户区域时区的对话:
addedItem["StartDate"] = regionalTimeZone.UTCToLocalTime(correctedEventStart.ToUniversalTime());
addedItem["EndDate"] = regionalTimeZone.UTCToLocalTime(correctedEventEnd.ToUniversalTime());

我不知道这对我世界以外的人是否有意义。但是SharePoint显然希望时间在用户的区域(或网站)时区中。从单元测试来看,这是显而易见的。如果我有一个OOB方法,允许中央时间中的用户在太平洋时间上午9点至10点在自定义列表中创建 session ,我希望能够使用它。但是我什么也找不到。

同样,所有这些都很好用...直到您参加周期性 Activity 。实际上,它适用于重复发生的事件,直到您尝试在日历 View 中查看所说的事件为止。然后看起来像这样:

请注意,“重复发生8”每天应重复执行2次实例。但是,复发的“跨度”或“持续时间”是2天而不是1小时。正确显示“重复15次”的位置。输出到调试时,两者之间字段值的唯一区别是“Duration”字段。重复8在ItemAdded中已将开始日期和结束日期进行了更新,重复15在ItemAdded中进行了更新,但ListItem.Update()已被注释掉。对于每个文档,SharePoint计算的重复项目工期与单个项目的工期不同。使用对象模型更改开始日期和结束日期这一事实不应否认这一点。

最佳答案

好的,所以我最终处理此问题的方式如下。我决定退出列表事件接收器,因为它在重新计算持续时间方面确实确实是SharePoint错误,以使周期性事件现在可以正常工作。我选择绑定(bind)到表单上的save事件并在发送之前更改值。到目前为止,这似乎在所有情况下都有效。我所有的数学都和以前一样。所以在我的New2.aspx中(此列表的新项目形式)

    protected override void OnInit(EventArgs e)
{
base.OnInit(e);

if ((SPContext.Current.FormContext.FormMode == SPControlMode.New) || (SPContext.Current.FormContext.FormMode == SPControlMode.Edit))
{
SPContext.Current.FormContext.OnSaveHandler += new EventHandler(SaveHandler);
}
}

protected void SaveHandler(object sender, EventArgs e)
{
Page.Validate();

if (Page.IsValid)
{
// fix times
SPFieldLookupValue lookupValue = new SPFieldLookupValue(ff5.Value.ToString());
TimeZoneInfo eventTimeZone = GetEventTimeZoneByListItemId(lookupValue.LookupId, SPContext.Current.Web);
SPTimeZone regionalTimeZone = GetRegionalTimeZone(SPContext.Current.Web);

bool isAllDayEvent = Convert.ToBoolean(ff6.Value);
bool isRecurrence = Convert.ToBoolean(ff11.Value);
DateTime correctedEventStart = DateTime.MinValue;
DateTime correctedEventEnd = DateTime.MinValue;

if (!isAllDayEvent && eventTimeZone != null && regionalTimeZone != null)
{
correctedEventStart = DateTime.Parse(ff3.Value.ToString() + " " + eventTimeZone.GetUtcOffset(DateTime.UtcNow).Hours + ":" + eventTimeZone.GetUtcOffset(DateTime.UtcNow).Minutes);
correctedEventEnd = DateTime.Parse(ff4.Value.ToString() + " " + eventTimeZone.GetUtcOffset(DateTime.UtcNow).Hours + ":" + eventTimeZone.GetUtcOffset(DateTime.UtcNow).Minutes);

ff3.ItemFieldValue = regionalTimeZone.UTCToLocalTime(correctedEventStart.ToUniversalTime());
ff4.ItemFieldValue = regionalTimeZone.UTCToLocalTime(correctedEventEnd.ToUniversalTime());
}

SPContext.Current.ListItem.Update();
}
}

像我以前的方法那样更新时间,但也可以正确计算持续时间。

SharePoint会根据用户的区域设置(或网络(如果用户尚未设置))显示正确的时间,并在日历 View 中显示正确的时间。我确实必须更改“编辑”表单以在编辑时具有正确的值:
   protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
using (SPWeb rootWeb = SPContext.Current.Site.RootWeb)
{
SPList eventsList = rootWeb.Lists.TryGetList("Events");

if (eventsList != null)
{
SPListItem thisEvent = eventsList.GetItemById(savebutton1.ItemId);

if (thisEvent != null)
{
bool isAllDayEvent = false;
if (thisEvent["fAllDayEvent"] != null)
{
isAllDayEvent = (bool)thisEvent["fAllDayEvent"];
}

if (!isAllDayEvent)
{
SPFieldLookupValue lookupValue = new SPFieldLookupValue(thisEvent["Event Time Zone"].ToString());
TimeZoneInfo eventTimeZone = GetEventTimeZoneByListItemId(lookupValue.LookupId, rootWeb);
SPTimeZone regionalTimeZone = GetRegionalTimeZone(rootWeb);

DateTime regionalStartDateTime = Convert.ToDateTime(thisEvent["StartDate"]);
DateTime originalStartDateTime = TimeZoneInfo.ConvertTimeFromUtc(regionalTimeZone.LocalTimeToUTC(regionalStartDateTime), eventTimeZone);

ff3.ListItemFieldValue = originalStartDateTime;

DateTime regionalEndDateTime = Convert.ToDateTime(thisEvent["EndDate"]);
DateTime originalEndDateTime = TimeZoneInfo.ConvertTimeFromUtc(regionalTimeZone.LocalTimeToUTC(regionalEndDateTime), eventTimeZone);

ff4.ListItemFieldValue = originalEndDateTime;
}
else
{
// for some reason with all day events, sharepoint saves them
// as the previous day 6pm. but when they show up to any user
// they will show as 12am to 1159pm and show up correctly on the calendar
// HOWEVER, when it comes to edit, the start date isn't corrected on the
// form, so continuing to save without fixing it will continue to decrease
// the start date/time by one day
DateTime regionalStartDateTime = Convert.ToDateTime(thisEvent["StartDate"]);
ff3.ListItemFieldValue = regionalStartDateTime.AddDays(1);
}
}
}
}
}
catch (Exception ex)
{
DebugLogger.WriteLine(ex);
}
}
}

“编辑”表单具有与“新建”相同的OnInit和SaveHandler。

关于sharepoint - SharePoint中的重复事件-“Duration”不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14757293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com