在CRM 2013插件中获取错误时间的日期,但UI没有时间字段。如何从日期中删除时间

时间:2014-07-03 15:22:28

标签: c# dynamics-crm-2011 dynamics-crm dynamics-crm-2013

我目前正在通过以下

从CRM中检索日期字符串
Entity dateDeliveryRequiredImage = (Entity)context.PostEntityImages["DeliveryRequired"];

但是由于某种原因,它似乎正在检索我通过以下行创建CSV文件的时间

dateDeliveryRequiredImage["requestdeliverby"].ToString()

任何想法从哪里开始,因为没有时间输入时间(并且说它是晚上11点?)

更重要的是如何摆脱它

谢谢,肖恩

更新

在出现错误后输入所需代码

 if (!timeZoneCode.HasValue)
            return;

需要可转换为'system.timeanddate'类型的对象

var response = (LocalTimeFromUtcTimeResponse)_serviceProxy.Execute(request);

_serviceProxy在当前上下文中不存在

我的服务代码如下;

IPluginExecutionContext context = (IPluginExecutionContext)
            serviceProvider.GetService(typeof(IPluginExecutionContext));


        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

但输入服务或上下文没有做任何事情

最后      新的QueryExpression(UserSettings.EntityLogicalName)

当前上下文中不存在用户设置

现在正在使用的代码更新

private DateTime RetrieveLocalTimeFromUTCTime(DateTime utcTime, IOrganizationService service)
    {

        int? timeZoneCode = RetrieveCurrentUsersSettings(service);


        if (!timeZoneCode.HasValue)
            throw new Exception("Can't find time zone code");



        var request = new LocalTimeFromUtcTimeRequest
        {
            TimeZoneCode = timeZoneCode.Value,
            UtcTime = utcTime.ToUniversalTime()
        };

        var response = (LocalTimeFromUtcTimeResponse)service.Execute(request);

        return response.LocalTime;
        //var utcTime = utcTime.ToString("MM/dd/yyyy HH:mm:ss");
        //var localDateOnly = response.LocalTime.ToString("dd-MM-yyyy");
    }

    private int? RetrieveCurrentUsersSettings(IOrganizationService service)
    {
        var currentUserSettings = service.RetrieveMultiple(
        new QueryExpression("usersettings")
        {
            ColumnSet = new ColumnSet("localeid", "timezonecode"),
            Criteria = new FilterExpression
            {
                Conditions =
        {
            new ConditionExpression("systemuserid", ConditionOperator.EqualUserId)
        }
            }
        }).Entities[0].ToEntity<Entity>();

        return (int?)currentUserSettings.Attributes["timezonecode"];
    }

请注意以下代码的区别如下:

}).Entities[0].ToEntity<Entity>();

return (int?)currentUserSettings.Attributes["timezonecode"]; 

1 个答案:

答案 0 :(得分:3)

CRM正在设置您在写入CSV之前可以删除的默认时间,如下所示:

DateTime dateAndTime = (DateTime)dateDeliveryRequiredImage["requestdeliverby"];
var date = dateAndTime.ToString("dd-MM-yyyy");

如果您要将默认时间写入CSV。你可以使用如下:

var date = dateAndTime.Date;

Dynamics CRM 2011以UTC时间格式存储所有DateTime字段,因此通过使用“DateTime.UtcNow”方法,我们的日期将在点击数据库之前转换为UTC时间。例如,默认时间是12:00:00,在数据库中它将被存储为11:00:00。这就是你在插件中获得11PM的原因。当我们的用户通过CRM读取日期时,它将被转换回他们的本地时区,用户将正确阅读时间。

<强>更新

IPluginExecutionContext context = (IPluginExecutionContext)
        serviceProvider.GetService(typeof(IPluginExecutionContext));


IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// This retrieves the UTC time
DateTime dateAndTime = (DateTime)dateDeliveryRequiredImage["requestdeliverby"];

// This converts the UTC time to your local time
var localDate = RetrieveLocalTimeFromUTCTime(dateAndTime, service);

// It will give you the correct date
var date = dateAndTime.ToString("dd-MM-yyyy");

将UTC转换为本地时间。请检查以下代码:

    /// <summary>
    /// Retrive the local time from the UTC time.
    /// </summary>
    /// <param name="utcTime"></param>
    /// <param name="service"></param>
    private DateTime RetrieveLocalTimeFromUTCTime(DateTime utcTime, IOrganizationService service)
    {
        int? timeZoneCode = RetrieveCurrentUsersSettings(service);

        if (!timeZoneCode.HasValue)
            throw new Exception("Can't find time zone code");

        var request = new LocalTimeFromUtcTimeRequest
        {
            TimeZoneCode = timeZoneCode.Value,
            UtcTime = utcTime.ToUniversalTime()
        };

        var response = (LocalTimeFromUtcTimeResponse)service.Execute(request);

        return response.LocalTime;
        //var utcTime = utcTime.ToString("MM/dd/yyyy HH:mm:ss");
        //var localDateOnly = response.LocalTime.ToString("dd-MM-yyyy");
    }

    /// <summary>
    /// Retrieves the current users timezone code and locale id
    /// </summary>
    private int? RetrieveCurrentUsersSettings(IOrganizationService service)
    {
        var currentUserSettings = service.RetrieveMultiple(
        new QueryExpression("usersettings")
        {
            ColumnSet = new ColumnSet("localeid", "timezonecode"),
            Criteria = new FilterExpression
            {
                Conditions =
        {
            new ConditionExpression("systemuserid", ConditionOperator.EqualUserId)
        }
            }
        }).Entities[0].ToEntity<UserSettings>();

        return currentUserSettings.TimeZoneCode;
    }

参考:Sample: Retrieve time zone information