public void Duplicate(IPersonalArtefact artefact, User owner)
        {
            var viewArtefact = (PersonalView)artefact;
            var viewEntity   = viewArtefact.Entity;

            _pluginContext.WorkAsync(new WorkAsyncInfo
            {
                Message = $"Duplicating userquery {viewEntity.GetAttributeValue<string>("name")} ({viewEntity.Id.ToString()}) for user {owner.Id}...",
                Work    = (worker, args) =>
                {
                    var client       = _pluginContext.ConnectionDetail.GetCrmServiceClient();
                    var userIdBefore = client.CallerId;
                    client.CallerId  = owner.Id;

                    try
                    {
                        // Define list of attributes that can safely be copied
                        var attribsToBeCopied = new string[]
                        {
                            "advancedgroupby", "columnsetxml", "conditionalformatting", "description", "fetchxml",
                            "layoutxml", "name", "parentqueryid", "querytype", "returnedtypecode", "statecode",
                            "statuscode"
                        };

                        var newViewEntity = new Entity(viewEntity.LogicalName);
                        newViewEntity.Attributes["ownerid"] = owner.Entity.ToEntityReference();
                        foreach (var key in attribsToBeCopied)
                        {
                            if (viewEntity.Contains(key))
                            {
                                newViewEntity.Attributes[key] = viewEntity.Attributes[key];
                            }
                        }

                        var response = _pluginContext.Service.Create(newViewEntity);
                        args.Result  = response;
                    }
                    catch (Exception exc)
                    {
                        args.Result     = null;
                        client.CallerId = userIdBefore;
                        throw exc;
                    }
                },
                PostWorkCallBack = (args) =>
                {
                    if (args.Error != null)
                    {
                        ErrorHelper.ShowExceptionMessageDialog(args.Error);
                    }
                    else
                    {
                        var newRecordId = (Guid)args.Result;
                        Clipboard.SetText(newRecordId.ToString());
                        MessageBox.Show($"Personal View successfully assigned to user \"{owner.Fullname}\" ({owner.Email} / {owner.Id}). New record Id was copied to clipboard ({newRecordId.ToString()})", "Info", MessageBoxButtons.OK, MessageBoxIcon.None);
                    }
                }
            });
        }
Esempio n. 2
0
        public void Duplicate(IPersonalArtefact artefact, User owner)
        {
            var diagramArtefact = (PersonalDiagram)artefact;
            var diagramEntity   = diagramArtefact.Entity;

            _pluginContext.WorkAsync(new WorkAsyncInfo
            {
                Message = $"Duplicating userqueryvisualization {diagramEntity.GetAttributeValue<string>("name")} ({diagramEntity.Id.ToString()}) for user {owner.Id}...",
                Work    = (worker, args) =>
                {
                    var client       = _pluginContext.ConnectionDetail.GetCrmServiceClient();
                    var userIdBefore = client.CallerId;
                    client.CallerId  = owner.Id;

                    try
                    {
                        // Define list of attributes that can safely be copied
                        var attribsToBeCopied = new string[]
                        {
                            "datadescription", "isdefault", "name", "presentationdescription", "primaryentitytypecode", "webresourceid"
                        };

                        var newDiagramEntity = new Entity(diagramEntity.LogicalName);
                        newDiagramEntity.Attributes["ownerid"] = owner.Entity.ToEntityReference();
                        foreach (var key in attribsToBeCopied)
                        {
                            if (diagramEntity.Contains(key))
                            {
                                newDiagramEntity.Attributes[key] = diagramEntity.Attributes[key];
                            }
                        }

                        var response = _pluginContext.Service.Create(newDiagramEntity);
                        args.Result  = response;
                    }
                    catch (Exception exc)
                    {
                        args.Result     = null;
                        client.CallerId = userIdBefore;
                        throw exc;
                    }
                },
                PostWorkCallBack = (args) =>
                {
                    if (args.Error != null)
                    {
                        ErrorHelper.ShowExceptionMessageDialog(args.Error);
                    }
                    else
                    {
                        var newRecordId = (Guid)args.Result;
                        Clipboard.SetText(newRecordId.ToString());
                        MessageBox.Show($"Personal Diagram successfully duplicated and assigned to user \"{owner.Fullname}\" ({owner.Email} / {owner.Id}). New record Id was copied to clipboard ({newRecordId.ToString()})", "Info", MessageBoxButtons.OK, MessageBoxIcon.None);
                    }
                }
            });
        }
        public void Assign(IPersonalArtefact artefact, User newOwner)
        {
            var dashboardArtefact = (PersonalDashboard)artefact;
            var dashboardEntity   = dashboardArtefact.Entity;

            _pluginContext.WorkAsync(new WorkAsyncInfo
            {
                Message = $"Assigning userform {dashboardEntity.GetAttributeValue<string>("name")} ({dashboardEntity.Id.ToString()})...",
                Work    = (worker, args) =>
                {
                    var client       = _pluginContext.ConnectionDetail.GetCrmServiceClient();
                    var userIdBefore = client.CallerId;
                    client.CallerId  = dashboardEntity.GetAttributeValue <EntityReference>("ownerid").Id;

                    try
                    {
                        var response = (AssignResponse)_pluginContext.Service.Execute(new AssignRequest()
                        {
                            Assignee = newOwner.Entity.ToEntityReference(),
                            Target   = dashboardEntity.ToEntityReference()
                        });
                    }
                    catch (Exception exc)
                    {
                        args.Result     = null;
                        client.CallerId = userIdBefore;
                        throw exc;
                    }
                },
                PostWorkCallBack = (args) =>
                {
                    if (args.Error != null)
                    {
                        ErrorHelper.ShowExceptionMessageDialog(args.Error);
                    }
                    else
                    {
                        MessageBox.Show($"Personal Dashboard successfully assigned to user \"{newOwner.Fullname}\" ({newOwner.Email} / {newOwner.Id})", "Info", MessageBoxButtons.OK, MessageBoxIcon.None);
                    }
                }
            });
        }
        /// <summary>
        /// Delete `PersonalView` artefact
        /// </summary>
        /// <param name="artefact" type="PersonalView"></param>
        public void Delete(IPersonalArtefact artefact)
        {
            var dashboardArtefact = (PersonalDashboard)artefact;
            var dashboardEntity   = dashboardArtefact.Entity;

            _pluginContext.WorkAsync(new WorkAsyncInfo
            {
                Message = $"Deleting userform {dashboardEntity.GetAttributeValue<string>("name")} ({dashboardEntity.Id.ToString()})...",
                Work    = (worker, args) =>
                {
                    var client       = _pluginContext.ConnectionDetail.GetCrmServiceClient();
                    var userIdBefore = client.CallerId;
                    client.CallerId  = dashboardEntity.GetAttributeValue <EntityReference>("ownerid").Id;

                    try
                    {
                        _pluginContext.Service.Delete(dashboardEntity.LogicalName, dashboardEntity.Id);
                        args.Result = true;
                    }
                    catch (Exception exc)
                    {
                        args.Result     = false;
                        client.CallerId = userIdBefore;
                        throw exc;
                    }
                },
                PostWorkCallBack = (args) =>
                {
                    if (args.Error != null)
                    {
                        ErrorHelper.ShowExceptionMessageDialog(args.Error);
                    }

                    OnPersonalDashboardsListUpdated();
                }
            });
        }