Pogo69's Blog

January 9, 2013

Converting Queries/Charts/Dashboards from User to System

Filed under: C#, CRM, Cutting Code — pogo69 [Pat Janes] @ 09:48

Overview

If you have created your own Saved Queries, Charts or Dashboards, you may have had occasion to share those objects with other CRM Users.  If you find that they become universally reusable to the point of wanting to “promote” them to System objects, there is no mechanism in the CRM Web Application to do so.

Code to the Rescue

You can however, convert such items using some relatively simple C# (or VB if you must) code.

Charts

// chart
var chart =
  (
    from uqv in xrm.UserQueryVisualizationSet
    where uqv.Name == "Pogo's Chart"
    select uqv
  ).FirstOrDefault();

Xrm2011.SavedQueryVisualization newChart = new Xrm2011.SavedQueryVisualization
{
  Name = chart.Name,
  DataDescription = chart.DataDescription,
  Description = chart.Description,
  IsCustomizable = new BooleanManagedProperty(true),
  IsDefault = false,
  IsManaged = false,
  PresentationDescription = chart.PresentationDescription,
  PrimaryEntityTypeCode = chart.PrimaryEntityTypeCode,
  WebResourceId = chart.WebResourceId
};

xrm.Create(newChart);

Views

// view
var view =
  (
    from uq in xrm.UserQuerySet
    where uq.Name == "Pogo's View"
    select uq
  ).FirstOrDefault();

Xrm2011.SavedQuery newView = new Xrm2011.SavedQuery
{
  Name = view.Name,
  AdvancedGroupBy = view.AdvancedGroupBy,
  CanBeDeleted = new BooleanManagedProperty(true),
  ColumnSetXml = view.ColumnSetXml,
  ConditionalFormatting = view.ConditionalFormatting,
  Description = view.Description,
  FetchXml = view.FetchXml,
  IsCustomizable = new BooleanManagedProperty(true),
  IsDefault = false,
  IsManaged = false,
  LayoutXml = view.LayoutXml,
  QueryType = view.QueryType,
  ReturnedTypeCode = view.ReturnedTypeCode
};

xrm.Create(newView);

Dashboards

// dashboard
var dashboard =
  (
    from d in xrm.UserFormSet
    where d.Name == "Pogo's Dashboard"
    select d
  ).FirstOrDefault();

Xrm2011.SystemForm newDashboard = new Xrm2011.SystemForm
{
  Name = dashboard.Name,
  CanBeDeleted = new BooleanManagedProperty(true),
  Description = dashboard.Description,
  FormXml = dashboard.FormXml,
  IsCustomizable = new BooleanManagedProperty(true),
  IsDefault = false,
  IsManaged = false,
  ObjectTypeCode = dashboard.ObjectTypeCode,
  Type = dashboard.Type
};

newDashboard.Id = xrm.Create(newDashboard);
PublishXmlRequest reqPublish = new PublishXmlRequest
{
  ParameterXml = string.Format(
    @"
      <importexportxml>
        <dashboards>
          <dashboard>{0}</dashboard>
        </dashboards>
      </importexportxml>",
    newDashboard.Id)
};
PublishXmlResponse respPublish = (PublishXmlResponse)xrm.Execute(reqPublish);

I had issues with privileges on the newly migrated Dashboard the first time I tried it.  The addition of the the PublishXmlRequest fixed it.  I had no such issue with the Views or Charts.

Reference Implementation

You can download a reference implementation of the code described above from the following:

http://sdrv.ms/W1z1Ra

It contains a CRM 2011 Managed Solution that allows Users to migrate User objects (Views, Charts, Dashboards) to System equivalents.  I am in the process of uploading the source code to Codeplex; upon completion you will be able to download and modify as you see fit.

NB: The current implementation will not migrate User Dashboards that contain User components.  A future version will migrate embedded components in addition to the Dashboard that houses them.

Update

You can now download the Managed Solution and/or all source code from the following Codeplex site:

http://userobjectmigration.codeplex.com

Advertisements

Create a free website or blog at WordPress.com.