private void brd_Org_DeleteLink_MouseDown(object sender, MouseButtonEventArgs e)
 {
     Trace.WriteLine(DateTime.Now + " : " + "Enter Delete void");
     try
     {
         if (System.Windows.MessageBoxResult.OK == MessageBox.Show("Delete RelationShip?", "Warning!", MessageBoxButton.OKCancel, MessageBoxImage.Warning))
         {
             TreeViewItem TR = (TreeViewItem)OrganizationTreeView.SelectedItem;
             // Get an object by GUID
             Guid G = new Guid(TR.Tag.ToString());
             //EnterpriseManagementObject treeviewEMO = emg.EntityObjects.GetObject<EnterpriseManagementObject>(G, ObjectQueryOptions.Default);
             IncrementalDiscoveryData instance = new IncrementalDiscoveryData();
             foreach (EnterpriseManagementRelationshipObject <EnterpriseManagementObject> obj in emg.EntityObjects.GetRelationshipObjectsWhereTarget <EnterpriseManagementObject>(G, relationClass, DerivedClassTraversalDepth.None, TraversalDepth.OneLevel, ObjectQueryOptions.Default))
             {
                 Trace.WriteLine(DateTime.Now + " : " + "Remove Relationship " + obj.TargetObject.DisplayName);
                 instance.Remove(obj);
                 instance.Commit(emg);
             }
         }
     }
     catch (Exception exc)
     {
         Trace.WriteLine(DateTime.Now + " : " + "Error in Remove Relationship void " + exc.Message);
     }
     FillTreeView.Now(emg, organizationClass, relationClass, OrganizationTreeView);
 }
        private void brd_Org_Add_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (OrganizationTreeView.SelectedItem != null)
                {
                    Guid guid = organizationClass.Id;
                    Collection <IDataItem> ItemsCol = new Collection <IDataItem>();
                    Microsoft.EnterpriseManagement.GenericForm.FormUtilities.Instance.LaunchAddInstancePickerDialog(ItemsCol, guid);
                    if (ItemsCol.Count() == 1)
                    {
                        TreeViewItem TR = (TreeViewItem)OrganizationTreeView.SelectedItem;
                        // Get an object by GUID
                        Guid G = new Guid(TR.Tag.ToString());
                        EnterpriseManagementObject treeviewEMO = emg.EntityObjects.GetObject <EnterpriseManagementObject>(G, ObjectQueryOptions.Default);

                        IDataItem targetInstanceIDataItem = ItemsCol[0];
                        //IDataItem targetInstanceIDataItem = this.LocPiker.Instance;
                        Guid gId = (Guid)targetInstanceIDataItem["$Id$"];
                        Trace.WriteLine(DateTime.Now + " : " + "Id : " + gId); //+ " ID : " + targetInstanceIDataItem["ID"]);
                        EnterpriseManagementObject pickerEMO = emg.EntityObjects.GetObject <EnterpriseManagementObject>(gId, ObjectQueryOptions.Default);
                        Trace.WriteLine(DateTime.Now + " : " + "Values for create Rel : " + treeviewEMO.DisplayName + " : " + pickerEMO.DisplayName + " picker.Id : " + pickerEMO.Id.ToString());
                        CreatableEnterpriseManagementRelationshipObject relationship = new CreatableEnterpriseManagementRelationshipObject(emg, relationClass);
                        relationship.SetSource(treeviewEMO);
                        relationship.SetTarget(pickerEMO);
                        relationship.Commit();

                        FillTreeView.Now(emg, organizationClass, relationClass, OrganizationTreeView);
                    }
                    else if (ItemsCol.Count() > 1)
                    {
                        MessageBox.Show("Please select only ONE organization!", "Multiselection!", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                    else if (ItemsCol.Count() == 0)
                    {
                        MessageBox.Show("Please select organization!", "No organization selected!", MessageBoxButton.OK, MessageBoxImage.Warning);
                    }
                }
                else
                {
                    MessageBox.Show("Please select organization!", "No organization selected!", MessageBoxButton.OK, MessageBoxImage.Warning);
                    Trace.WriteLine(DateTime.Now + " : " + "Not have Values for create Rel");
                }
            }
            catch (Exception exc)
            {
                Trace.WriteLine(DateTime.Now + " : " + "Error in Picker Dialog " + exc.Message);
            }
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                GetSession();
                ManagementPack mp       = emg.GetManagementPack("SMCenter.AssetManagement.Core", "75b45bd6835084b1", new Version());
                ManagementPack mpSystem = emg.GetManagementPack("System.Library", "31bf3856ad364e35", new Version());
                organizationClass = emg.EntityTypes.GetClass("SMCenter.Organization", mp);
                UserClass         = emg.EntityTypes.GetClass("System.Domain.User", mpSystem);
                relationClass     = emg.EntityTypes.GetRelationshipClass("SMCenter.OrganizationContainsChildOrganization", mp);
                userrelationClass = emg.EntityTypes.GetRelationshipClass("SMCenter.UserHasOrganization", mp);
                FillTreeView.Now(emg, organizationClass, relationClass, OrganizationTreeView);

                this.AddHandler(FormEvents.SubmittedEvent, new EventHandler <FormCommandExecutedEventArgs>(this.OnSubmitted));
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(DateTime.Now + " : " + "UserControl_Loaded Error : " + ex.Message);
            }
        }
        private void brd_Org_Create_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (OrganizationTreeView.SelectedItem != null)
            {
                CreatableEnterpriseManagementObject newOrganization = new CreatableEnterpriseManagementObject(emg, organizationClass);

                //Add some property values
                newOrganization[organizationClass, "DisplayName"].Value = "Add some info";
                newOrganization.Commit();

                //Treeview Item - Parent Item
                TreeViewItem TR = (TreeViewItem)OrganizationTreeView.SelectedItem;
                // Get an object by GUID
                Guid G = new Guid(TR.Tag.ToString());
                EnterpriseManagementObject treeviewEMO = emg.EntityObjects.GetObject <EnterpriseManagementObject>(G, ObjectQueryOptions.Default);

                CreatableEnterpriseManagementRelationshipObject relationship = new CreatableEnterpriseManagementRelationshipObject(emg, relationClass);
                relationship.SetSource(treeviewEMO);
                relationship.SetTarget(newOrganization);
                relationship.Commit();

                FillTreeView.Now(emg, organizationClass, relationClass, OrganizationTreeView);

                //Convert EnterpriseManagementObject to IDataItem
                EnterpriseManagementObjectDataType dataType = new EnterpriseManagementObjectDataType(organizationClass);
                IDataItem newOrganizationDataItem           = dataType.CreateProxyInstance(newOrganization);

                //Open Console form for created object
                //ConsoleContextHelper.Instance.PopoutForm(itemIdentity);
                Microsoft.EnterpriseManagement.GenericForm.FormUtilities.Instance.PopoutForm(newOrganizationDataItem);
            }
            else
            {
                MessageBox.Show("Please select parent organization!", "No organization selected", MessageBoxButton.OK, MessageBoxImage.Warning);
                Trace.WriteLine(DateTime.Now + " : " + "Not have Values for create Rel");
            }
        }
 private void brd_Org_Refresh_MouseDown(object sender, MouseButtonEventArgs e)
 {
     FillTreeView.Now(emg, organizationClass, relationClass, OrganizationTreeView);
 }