protected override void ProcessRecord()
        {
            base.ProcessRecord();

            base.WriteVerbose(string.Format("Importing access team templates from: {0}", InputPath));
            ExportedTeamTemplates templates = new ExportedTeamTemplates();
            var serializer = new XmlSerializer(templates.GetType());

            using (TextReader reader = new StreamReader(InputPath))
            {
                templates = (ExportedTeamTemplates)serializer.Deserialize(reader);
            }

            foreach (ExportEntity exportedEntity in templates.TeamTemplates)
            {
                Entity entity = new Entity(exportedEntity.LogicalName);
                entity.Id = exportedEntity.Id;
                foreach (var attribute in exportedEntity.Attributes)
                {
                    entity[attribute.Key] = attribute.Value;
                }
                try
                {
                    //first try an update
                    OrganizationService.Update(entity);
                }
                catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
                {
                    //if update fails, try a create
                    OrganizationService.Create(entity);
                }
            }

            base.WriteObject("Import complete");
        }
Exemplo n.º 2
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            base.WriteVerbose(string.Format("Exporting access team templates to: {0}", OutputPath));

            string teamtemplateFetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>  
              <entity name='teamtemplate'>
                <attribute name='defaultaccessrightsmask' />
                <attribute name='description' />
                <attribute name='objecttypecode' />
                <attribute name='teamtemplateid' />
                <attribute name='teamtemplatename' />
              </entity>
            </fetch>";

            var retrievedRecords = OrganizationService.RetrieveMultiple(new FetchExpression(teamtemplateFetch));

            List <ExportEntity> exportEntities = new List <ExportEntity>();

            if (retrievedRecords.Entities.Count > 0)
            {
                foreach (Entity entity in retrievedRecords.Entities)
                {
                    ExportEntity exportEntity = new ExportEntity();
                    exportEntity.LogicalName     = entity.LogicalName;
                    exportEntity.Id              = entity.Id;
                    exportEntity.Attributes      = new List <SerializableKeyValuePair <string, object> >();
                    exportEntity.FormattedValues = new List <SerializableKeyValuePair <string, string> >();
                    foreach (var attribute in entity.Attributes)
                    {
                        SerializableKeyValuePair <string, object> serializedAttribute = new SerializableKeyValuePair <string, object>();
                        serializedAttribute.Key   = attribute.Key;
                        serializedAttribute.Value = attribute.Value;
                        exportEntity.Attributes.Add(serializedAttribute);
                    }
                    foreach (var attribute in entity.FormattedValues)
                    {
                        SerializableKeyValuePair <string, string> serializedAttribute = new SerializableKeyValuePair <string, string>();
                        serializedAttribute.Key   = attribute.Key;
                        serializedAttribute.Value = attribute.Value;
                        exportEntity.FormattedValues.Add(serializedAttribute);
                    }
                    exportEntities.Add(exportEntity);
                }
            }
            ExportedTeamTemplates templates = new ExportedTeamTemplates();

            templates.TeamTemplates = exportEntities;
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(templates.GetType());
            TextWriter templateWriter = new StreamWriter(OutputPath);

            x.Serialize(templateWriter, templates);

            base.WriteObject("Export complete");
        }