从SDK更新SavedQuery(View)

时间:2013-10-16 15:06:59

标签: dynamics-crm-2011

我正在尝试将解决方案导入到Acceptance环境中的所有业务单元引用。

QueryExpression ViewQuery = new QueryExpression("savedquery");
String[] viewArrayFields = { "name", "fetchxml" };

ViewQuery.ColumnSet = new ColumnSet(viewArrayFields);


ViewQuery.PageInfo = new PagingInfo();
ViewQuery.PageInfo.Count = 5000;
ViewQuery.PageInfo.PageNumber = 1;
ViewQuery.PageInfo.ReturnTotalRecordCount = true;

EntityCollection retrievedViews = service.RetrieveMultiple(ViewQuery);
//iterate though the values and print the right one for the current user
int oldValues = 0;
int accValuesUpdated = 0;
int prodValuesUpdated = 0;
int total = 0;
foreach (var entity in retrievedViews.Entities)
{
    total++;
    if (!entity.Contains("fetchxml"))
    { }
    else
    {
        string fetchXML = entity.Attributes["fetchxml"].ToString();

        for (int i = 0; i < guidDictionnary.Count; i++)
        {
            var entry = guidDictionnary.ElementAt(i);

            if (fetchXML.Contains(entry.Key.ToString().ToUpperInvariant()))
            {
                Console.WriteLine(entity.Attributes["name"].ToString());
                oldValues++;
                if (destinationEnv.Equals("acc"))
                {
                    accValuesUpdated++;
                    Console.WriteLine();
                    Console.WriteLine("BEFORE:");
                    Console.WriteLine();
                    Console.WriteLine(entity.Attributes["fetchxml"].ToString());
                    string query = entity.Attributes["fetchxml"].ToString();
                    query = query.Replace(entry.Key.ToString().ToUpperInvariant(), entry.Value.AccGuid.ToString().ToUpperInvariant());
                    entity.Attributes["fetchxml"] = query;
                    Console.WriteLine();
                    Console.WriteLine("AFTER:");
                    Console.WriteLine();
                    Console.WriteLine(entity.Attributes["fetchxml"].ToString());
                }
                else
                {
                    prodValuesUpdated++;
                    string query = entity.Attributes["fetchxml"].ToString();
                    query = query.Replace(entry.Key.ToString().ToUpperInvariant(), entry.Value.ProdGuid.ToString().ToUpperInvariant());
                    entity.Attributes["fetchxml"] = query;
                }
                service.Update(entity);
            }

        }
    }

}
Console.WriteLine("{0} values to be updated. {1} shall be mapped to acceptance, {2} to prod. Total = {3} : {4}", oldValues, accValuesUpdated, prodValuesUpdated, total, retrievedViews.Entities.Count);

我看到新值已更正,但未保存。更新记录时我没有收到任何错误,并且在CRM中发布更改也无济于事。

任何提示?

1 个答案:

答案 0 :(得分:2)

根据您的评论,这听起来像您保存实体的价值,是您想要的价值。我猜你的问题是不发布你的更改。如果你不发布它,它仍然会给你我相信的FetchXml的旧值。

尝试调用此方法:

    PublishEntity(service, "savedquery");

    private void PublishEntity(IOrganizationService service, string logicalName)
    {
        service.Execute(new PublishXmlRequest()
        {
            ParameterXml = "<importexportxml>"
          + "    <entities>"
          + "        <entity>" + logicalName + "</entity>"
          + "    </entities>"
          + "</importexportxml>"
        });
    }