public ListSettingsDialogViewModel(String listAsmxUrl, string listOrgAsmxUrl, string peopleAsmxUrl, string customPermissionsAsmxUrl, SPList dropDownSelectedList, string siteURL,string siteRootURL, StringBuilder log,List<BusinessUnit> units)
        {
            EventAggregator.Instance.Publish<bCheck.Admin.MainPage.MessageSample>(new bCheck.Admin.MainPage.MessageSample());
            PeopleAsmx = peopleAsmxUrl;
            SPClientContext = new ClientContext(siteURL);
            CustomPermissionsAsmx = customPermissionsAsmxUrl;
            _dispatcher = Deployment.Current.Dispatcher;
            ListCollection = new ObservableCollection<SharepointList>();
            SiteRootUrl = siteRootURL;
            ListSettingsAgent = new ListSettingsServiceAgent(listAsmxUrl, listOrgAsmxUrl, siteURL, SiteRootUrl);
            dialogService = new DialogService();
            dialogService.Width = 180;
            dialogService.Height = 120;
            PermissionsAgent = new PermissionsServiceAgent(PeopleAsmx, CustomPermissionsAsmx);            
            SaveListCommand = new RelayCommand(SaveListAction);
            DeleteListCommand = new RelayCommand(DeleteListAction);
            ListPermissionsCommand = new RelayCommand(ListPermissionsAction);
            CloseCommand = new RelayCommand(CloseAction);
            CloseCommand.IsEnabled = true;
            SaveListCommand.IsEnabled = false;
            SelectedList = new SharepointList();
            SelectedList.IsQuickLaunchEnabled = true;
            PeoplePickerCommand = new RelayCommand(PeoplePickerAction);
            DropDownSelectedList = dropDownSelectedList;
            AvailableListName = dropDownSelectedList.ListName;
            IsDropDownListSelected = false;
            SiteUrl = siteURL;
           
            _log = log;
            if (AvailableListName != null)
            {
                EditListSelected = true;
                IsNewListEnabled = false;
                NewListSelected = false;
                IsDropDownListSelected = true;
            }
            else
            {
                NewListSelected = true;
                IsNewListEnabled = true;
            }

            if (units != null)
            {
                BusinessUnitDropDownValues = new ObservableCollection<string>();
                foreach (BusinessUnit unit in units)
                    BusinessUnitDropDownValues.Add(unit.Title);
            }                                            
        }
        public void IsbCheckItemActivated(SharepointList selectedList)
        {
            bool isActivated = false; ;
            XDocument xdoc = new XDocument();
            string querystring = "<Query><Where><Contains><FieldRef Name='bcheck_x0020_list_x0020_template'/><Value Type='Text'>" + HttpUtility.HtmlEncode(selectedList.ListName) + "</Value></Contains></Where></Query>";
            XElement queryOptions = new XElement("QueryOptions");
            XElement viewFields = new XElement("ViewFields");
            XElement query = XElement.Parse(querystring);
            EventHandler<GetListItemsCompletedEventArgs> handler = null;
            handler = (s, e) =>
            {
                if (e.Error == null)
                {
                    MethodID++;
                    XNamespace ns = "http://schemas.microsoft.com/sharepoint/soap/";
                    XNamespace rs = "urn:schemas-microsoft-com:rowset";
                    XNamespace z = "#RowsetSchema";
                    isActivated = false;
                    if ((e.Result.Descendants(z + "row")).Count() > 0)
                    {
                        var bCheckItem = (from field in e.Result.Descendants(z + "row")
                                          select new
                                          {
                                              EmailString = field.Attribute("ows_bcheck_x0020_email"),
                                              ID = field.Attribute("ows_ID"),
                                              Activated = field.Attribute("ows_bcheck_x0020_Process")
                                          }).First();
                        selectedList.bCheckListItemID = bCheckItem.ID.Value;
                        selectedList.IsActivated = (bCheckItem.Activated.Value == "Yes" ? true : false);
                        selectedList.OwnerEmailString = bCheckItem.EmailString.Value;
                        selectedList.OwnerEmailString.Split(new char[] { '#', ';' }).Where(str => str.Contains("@")).ToList().ForEach(str => selectedList.OwnerEmail += str + ",");
                        selectedList.OwnerEmail = selectedList.OwnerEmail.Substring(0, selectedList.OwnerEmail.Length - 1);
                    }
                }
                proxy_queryorg.GetListItemsCompleted -= handler;
                //  reply(isActivated);
            };

            proxy_queryorg.GetListItemsCompleted += handler;
            proxy_queryorg.GetListItemsAsync("bcheck", null, query, viewFields, null, queryOptions, null);
        }
 private void DeleteListAction()
 {
     dialogService.Show("Delete Confirmation",
         "Are you sure you want to delete this list ?",
         (result) =>
         {
             if (result == true)
             {
                 IsBusy = true;
                 if (SelectedList.ListName == null || SelectedList.ListName == "")
                 {
                     ExceptionMessage = "Please select a list";
                     IsBusy = false;
                     //StartExMessageTimer();
                 }
                 else
                 {
                     ListSettingsAgent.DeleteList(SelectedList, (deleteresult =>
                     {
                         if (deleteresult == true)
                         {
                             EventAggregator.Instance.Publish<SPList>(DropDownSelectedList);
                             ExceptionMessage = "List deleted.";
                             ListCollection.Remove(SelectedList);
                             SelectedList = new SharepointList();
                         }
                         else
                         {
                             ExceptionMessage = "Error! Contact admin.";
                         }
                         IsBusy = false;
                         //StartExMessageTimer();
                     }));
                 }
             }
         });
 }
        private void SaveListAction()
        {
            IsBusy = true;
            if (EditListSelected == true)
            {
                if (SelectedList.ListName == null || SelectedList.ListName == "")
                {
                    ExceptionMessage = "Please select a list";
                    IsBusy = false;
                    //StartExMessageTimer();
                }
                else
                {
                    var verifiedemails = VerifyOwnerEmail(SelectedList.OwnerEmail);
                    if (verifiedemails != null)
                    {
                        PermissionsAgent.GetMultiplePrincipals(verifiedemails, (results) =>
                        {
                            if (results.Count == verifiedemails.Count)
                            {

                                SelectedList.OwnerEmailString = PermissionsAgent.ConvertToEmailString(results);
                                ListSettingsAgent.SaveList(SelectedList, (result) =>
                                {
                                    if (result == true)
                                    {
                                        DropDownSelectedList.Description = SelectedList.Description;
                                        DropDownSelectedList.Title = SelectedList.ListName;
                                        DropDownSelectedList.QuickLaunch = SelectedList.IsQuickLaunchEnabled == true ? Navigation.Yes : Navigation.No;
                                        EventAggregator.Instance.Publish<SPList>(DropDownSelectedList);
                                        ExceptionMessage = "List settings saved.";
                                    }
                                    else
                                    {
                                        ExceptionMessage = "Error! Contact admin.";
                                    }
                                    //StartExMessageTimer();
                                    IsBusy = false;
                                });
                            }
                            else
                            {
                                ExceptionMessage = "Enter Valid Email.";
                                IsBusy = false;
                                //StartExMessageTimer();
                            }
                        });
                    }
                    else
                    {
                        ExceptionMessage = "Enter Valid Email.";
                        IsBusy = false;
                        //StartExMessageTimer();
                    }
                }
            }
            else
            {
                if (SelectedList.ListName == null || SelectedList.ListName == "")
                {
                    ExceptionMessage = "Please enter a list name";
                    IsBusy = false;
                    //StartExMessageTimer();
                }
                else
                {
                    var existinglist = ListCollection.Where(sl => sl.ListName.ToLower().Trim() == SelectedList.ListName.ToLower().Trim());
                    if (existinglist.Count() == 0)
                    {
                        var verifiedemails = VerifyOwnerEmail(SelectedList.OwnerEmail);
                        if (verifiedemails != null)
                        {
                            PermissionsAgent.GetMultiplePrincipals(verifiedemails, (results) =>
                            {
                                if (results.Count == verifiedemails.Count)
                                {
                                    SelectedList.OwnerEmailString = PermissionsAgent.ConvertToEmailString(results);
                                    ListSettingsAgent.CreateList(SelectedList, (result) =>
                                    {
                                        string listguid = SelectedList.GUID;
                                        if (result == true)
                                        {
                                            _dispatcher.BeginInvoke(() =>
                                            {
                                                IsBusy = false;
                                                ExceptionMessage = "List created succesfully.";
                                                EventAggregator.Instance.Publish<SPList>(DropDownSelectedList);
                                                ListCollection.Add(SelectedList);
                                                SelectedList = new SharepointList();
                                            });
                                        }
                                        else
                                        {
                                            _dispatcher.BeginInvoke(() =>
                                            {
                                                ExceptionMessage = "Error! Contact admin.";
                                                IsBusy = false;
                                            });
                                        }
                                    });
                                }
                                else
                                {
                                    ExceptionMessage = "Enter Valid Email.";
                                    IsBusy = false;
                                    //StartExMessageTimer();
                                }
                            });
                        }
                        else
                        {
                            ExceptionMessage = "Enter Valid Email.";
                            IsBusy = false;
                            //StartExMessageTimer();
                        }
                    }
                    else
                    {
                        ExceptionMessage = "List name exists. Use different name.";
                        IsBusy = false;
                        //StartExMessageTimer();
                    }
                }
            }

        }
 public void CreateList(SharepointList newList, Action<bool> reply)
 {
     SharepointList tempnewList = newList;
     proxy_update = new ListsSoapClient("ListsSoap", ServiceParamStatHelper.SetParams(AsmxUrl, ServiceParamStatHelper.Actions.UPDATE, "UNKNOWN"));
     EventHandler<AddListCompletedEventArgs> addlisthandler = null;
     addlisthandler = (s, e) =>
     {
         tempnewList.GUID = e.Result.Attribute("ID").Value;
         if (e.Error == null)
         {
             XElement flds = XElement.Load("Data/ListDefinition2.xml");
             string isEnabled = tempnewList.IsActivated == true ? "Yes" : "No";
             EventHandler<UpdateListCompletedEventArgs> updatelisthandler = null;
             updatelisthandler = (s1, e1) =>
             {
                 if (e1.Error == null)
                 {
                     EventHandler<UpdateListItemsCompletedEventArgs> updatelistitemshandler = null;
                     updatelistitemshandler = (s2, e2) =>
                     {
                         if (e2.Error == null)
                         {
                             //Removing all existing permissions.
                             ClientContext clientContext = new ClientContext(SiteURL);
                             Site collSite = clientContext.Site;
                             Web site = clientContext.Web;
                             SecurableObject listSecurable = site.Lists.GetByTitle(tempnewList.ListName);
                             listSecurable.BreakRoleInheritance(false, true);
                             clientContext.ExecuteQueryAsync((sender, args) =>
                             {
                                 reply(true);
                             }, (sender,args) => 
                             {
                                 reply(false);
                             });
                         }
                         else
                         {
                             reply(false);
                         }
                         proxy_updateorg.UpdateListItemsCompleted -= updatelistitemshandler;
                     };
                     string sBatch = "<Batch><Method ID='1' Cmd='New'>";
                     sBatch += "<Field Name='Title'>" + tempnewList.ListName + "</Field>";
                     sBatch += "<Field Name='bcheck_x0020_list_x0020_name'>" + "operations checklist" + "</Field>";
                     sBatch += "<Field Name='bcheck_x0020_list_x0020_template'>" + tempnewList.ListName + "</Field>";
                     sBatch += "<Field Name='bcheck_x0020_site'>" + "/tasks/Lists/operations checklist" + "</Field>";
                     sBatch += "<Field Name='bcheck_x0020_email'>" + tempnewList.OwnerEmailString + "</Field>";
                     sBatch += "<Field Name='bcheck_x0020_Process'>" + isEnabled + "</Field>";
                     sBatch += "</Method></Batch>";
                     XElement updates = XElement.Parse(sBatch);
                     proxy_updateorg.UpdateListItemsCompleted += updatelistitemshandler;
                     proxy_updateorg.UpdateListItemsAsync("bcheck", updates);
                 }
                 else
                 {
                     reply(false);
                 }
                 proxy_update.UpdateListCompleted -= updatelisthandler;
             };
             proxy_update.UpdateListCompleted += updatelisthandler;
             proxy_update.UpdateListAsync(tempnewList.GUID, null, flds, null, null, null);
         }
         else
         {
             reply(false);
         }
         proxy_update.AddListCompleted -= addlisthandler;
     };
     proxy_update.AddListCompleted += addlisthandler;
     proxy_update.AddListAsync(newList.ListName, newList.Description, 100);
 }
 public void DeleteList(SharepointList selectedList, Action<bool> reply)
 {
     proxy_update = new ListsSoapClient("ListsSoap", ServiceParamStatHelper.SetParams(AsmxUrl, ServiceParamStatHelper.Actions.UPDATE, "UNKNOWN"));
     EventHandler<AsyncCompletedEventArgs> deletelisthandler = null;
     deletelisthandler = (s, e) =>
     {
         if (e.Error == null)
         {
             String strBatch = "<Batch>";
             strBatch += "<Method ID='1' Cmd='Delete'>";
             strBatch += "<Field Name='ID'>" + selectedList.bCheckListItemID + "</Field>";
             strBatch += "</Method></Batch>";
             XElement deleteItem = XElement.Parse(strBatch);
             EventHandler<UpdateListItemsCompletedEventArgs> deletebCheckItemhandler = null;
             deletebCheckItemhandler = (s1, e1) =>
              {
                  if (e1.Error == null)
                  {
                      reply(true);
                  }
                  else
                  {
                      reply(false);
                  }
                  proxy_updateorg.UpdateListItemsCompleted -= deletebCheckItemhandler;
              };
             proxy_updateorg.UpdateListItemsCompleted += deletebCheckItemhandler;
             proxy_updateorg.UpdateListItemsAsync("bcheck", deleteItem);
         }
         else
         {
             reply(false);
         }
         proxy_update.DeleteListCompleted -= deletelisthandler;
     };
     proxy_update.DeleteListCompleted += deletelisthandler;
     proxy_update.DeleteListAsync(selectedList.ListName);
 }
        public void SaveList(SharepointList selectedList, Action<bool> reply)
        {
            proxy_update = new ListsSoapClient("ListsSoap", ServiceParamStatHelper.SetParams(AsmxUrl, ServiceParamStatHelper.Actions.UPDATE, "UNKNOWN"));
            proxy_updateorg = new ListsSoapClient("ListsSoap", ServiceParamStatHelper.SetParams(ListOrgAsmx, ServiceParamStatHelper.Actions.UPDATE, "UNKNOWN"));
            XElement listprops = new XElement(new XElement("List"));
            listprops.SetAttributeValue("Title", selectedList.ListName);
            listprops.SetAttributeValue("Description", selectedList.Description);
            listprops.SetAttributeValue("OnQuickLaunch", selectedList.IsQuickLaunchEnabled);

            EventHandler<UpdateListCompletedEventArgs> updatehandler = null;
            updatehandler = (s, e) =>
            {
                if (e.Error == null)
                {
                    selectedList.Version = (Int32.Parse(selectedList.Version) + 1).ToString();
                    reply(true);

                }
                else
                {
                    reply(false);
                }
                proxy_update.UpdateListCompleted -= updatehandler;
            };

            proxy_update.UpdateListCompleted += updatehandler;
            proxy_update.UpdateListAsync(selectedList.GUID, listprops, null, null, null, selectedList.Version);

            XDocument xdoc = new XDocument();
            XElement queryOptions = new XElement("QueryOptions");
            XElement viewFields = new XElement("ViewFields");
            string isEnabled = selectedList.IsActivated == true ? "Yes" : "No";
            XElement updateQuery = XElement.Parse("<Batch><Method ID='1' Cmd='Update'>" + "<Field Name='ID'>" + selectedList.bCheckListItemID + "</Field>" + "<Field Name='bcheck_x0020_Process'>" + isEnabled + "</Field><Field Name='bcheck_x0020_email'>" + selectedList.OwnerEmailString + "</Field><Field Name='Title'>" + selectedList.ListName + "</Field><Field Name='bcheck_x0020_list_x0020_template'>" + selectedList.ListName + "</Field></Method></Batch>");
            EventHandler<UpdateListItemsCompletedEventArgs> handler2 = null;
            handler2 = (s1, e1) =>
            {
                proxy_updateorg.UpdateListItemsCompleted -= handler2;
            };
            proxy_updateorg.UpdateListItemsCompleted += handler2;
            proxy_updateorg.UpdateListItemsAsync("bcheck", updateQuery);
        }