Can't remove contacts from an address book in Outlook 2010 - c#

I can add a contact to a address book but for some reason I can't remove it. The code I'm executing is as follows.
String abName = "Name ofthe targetted address book";
Outlook.Folder addressBook;
if (targetFolder.Folders.OfType<Outlook.Folder>().Any(element
=> element.Name == abName))
addressBook = targetFolder.Folders[abName] as Outlook.Folder;
else
addressBook = targetFolder.Folders.Add(
abName, Outlook.OlDefaultFolders.olFolderContacts) as Outlook.Folder;
addressBook.ShowAsOutlookAB = true;
for (int i = addressBook.Items.Count - 1; i >= 0; i--)
if (!stringList.Any(element
=> element == addressBook.Items.OfType<Outlook.ContactItem>()
.ToList()[i].Email1Address))
addressBook.Items.OfType<Outlook.ContactItem>().ToList().RemoveAt(i);
The fetching of the address book works and the matching for strings too. I get into the RemoveAt line for the exactly correct contacts. There's no error or other message when I execute the removal. Still, the contact list remain unaffected.
Why?
What can I do to actually remove the contacts?
I suspect that I may be working on a copy of the actual list containing the contacts. The problem is that if I don't create a List, I'm not sure how to alter the list of contacts.
So, the most helpful answer would shed some light on how to alter addressBook (or perhaps addressBook.Items) given certain condition. E.g., say that we'd like to remove all the contants the name of whom starts with the letter "Q".
At this moment I can only think of a super ugly work-around and it's so rectum-ugly that I don't even mention it here. Really ugly...

You ae not removing an Outlook contact. You are removing an OUtlook object from your own List object.
You need to call ContactItem.Delete.
As a side note, do not use multiple dot notation when working with COM objects, especially in a loop - you will receive a brand new COM object for each dot.

Here is a solution
private void ClearContact(Outlook.Application outlookApplication)
{
Outlook.MAPIFolder contactFolder = outlookApplication.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
int total = contactFolder.Items.Count;
while (total > 0)
{
// first index number is 1 not 0
var contact = (Outlook.ContactItem)contactFolder.Items[1];
contact.Delete();
total = contactFolder.Items.Count;
}
}
I use netoffice outlook api
http://netoffice.codeplex.com/wikipage?title=Outlook_Example05
And use while loop to delete all contact

Related

Selenium C# Find Element with class and text

im very new to testing and have no training in automated tests so please bare with me if i say stupid things but ill try the best i can.
Bascially i am trying to assert that a specific employee in the employee list has the status of 'leaver'.
This is what i have tried (and other variations with the different classes)
Assert.Equal("image-tile__badge background-color--status-leaver ng-star-inserted", Driver.FindElement(By.XPath("//*[contains(#class,'image-tile__content-header') and contains(text(),'End Date, Contract') and contains(#class, 'image-tile__badge')]")).GetAttribute("Class"));
Assert.Equal("image-tile__badge background-color--status-leaver ng-star-inserted", Driver.FindElement(By.XPath("//*[contains(#class,'image-tile__content-header') and contains(text(),'End Date, Contract')]")).FindElement(By.XPath("//*[contains(#class, 'image-tile__badge')]")).GetAttribute("Class"));
The last one finds the element when the status is 'new', but when i change the employee status to 'leaver', it still returns as 'new' so possibly looking at another employee with a 'new' status.
Hopefully this is enough info, let me know if more is needed (this is my first ever post!)
HTML code in image below
[HTML code on Chrome]
[1]: https://i.stack.imgur.com/kUxkf.png
Summary: im trying to assert that the Employee "End Date, Contract" has the status of leaver (aka the leaver class "image-tile__badge background-color--status-leaver ng-star-inserted")
Thanks everyone for their help!
One of my devs managed to take #noldors example and modify it a bit so heres what ended up working for me:
var newElmList1 = Driver.FindElements(By.CssSelector("div.background-color--status-leaver")).ToList();
List<string> newNames1 = new List<string>();
foreach (var newElm in newElmList1)
{
var newName1 = newElm.FindElement(By.XPath(".."))
.FindElement(By.CssSelector("div.image-tile__content-header")).Text;
newNames.Add(newName1);
}
if (!newNames.Contains("End Date, Contract"))
{
throw new Exception("Exception Error on leaver Person");
}
As per your screenshot i fill it's better if you try using Xpath
var elmList = Driver.FindElements(By.Xpath("//div[contains(text(),'leaver')]")).ToList();
i hope it will help you
Thank You.
According to your screenshot, you can find all elements with 'Leaver' specific class with this;
var leaverElmList = Driver.FindElements(By.CssSelector("div.background-color--status-leaver")).ToList();
List<string> leaverNames = new List<string>();
foreach (var leaverElm in leaverElmList) {
var leaverName = leaverElm.FindElement(By.XPath(".."))
.FindElement(By.CssSelector("div.image-tile__content-header"));
.Text()
leaverNames.Add(leaverName);
}
Enddate, Contract which is not related to the div that contains Leaver. It's direct parent is the image-tile div

Enterprise Architect 9: Add Note to Connector

I want to add notes to connectors in an Enterprise Architect diagram programmatically.
So far I only managed to add notes to elements with the following code:
foreach (EA.Element element in Package.Elements)
{
foreach (EA.Connector conn in element.Connectors)
{
EA.Element newNote = Package.Elements.AddNew("MyNote", "Note");
newNote.Notes = "Some string";
newNote.Update();
//position calculation is left out here
EA.DiagramObject k = diagram.DiagramObjects.AddNew(position, "");
k.ElementID = newNote.ElementID;
k.Sequence = 9;
k.Update();
EA.Connector newConn = newNote.Connectors.AddNew("NewLink", "NoteLink");
newConn.SupplierID = conn.SupplierID;
newConn.Update();
EA.DiagramLink newLink = diagram.DiagramLinks.AddNew("newLink", "NoteLink");
newLink.ConnectorID = newConn.ConnectorID;
newLink.Update();
The image maybe makes it more clear what I actually want:
http://www.directupload.net/file/d/3536/6bkijpg2_png.htm
My question is: How do I get the note attached to the connector? I assume I have to change this line "newConn.SupplierID = conn.SupplierID;", but "newConn.SupplierID = conn.ConnectorID" causes an exception.
I would be very happy if someone could help me!
Best regards
EA handles note links to connectors very differently from note links to elements.
Connectors always run between two elements. In your example, there are four elements (two of type Activity named O1 and O2, and two of type Note; these are usually nameless) and three connectors (O1 - O2, "This is what I have" - O2, and one from O1 running off the edge of the image).
The thing that looks like a connector from "This is what I want" to the O1 - O2 connector is not, in fact, a connector at all -- it just looks like one. In the GUI, the link to a connector is unresponsive and you can't bring up a properties dialog for it. This is why.
The fact that the note is linked to the connector is stored in the note element itself, in the MiscData collection. What you need to do is add the string idref=<connector_id>; to MiscData(3). You may also need to set the Note's Subtype field to 1.
However, MiscData is read-only so you'll have to go into the database and update t_object (where elements are stored) directly. MiscData in the API corresponds to PDATA1 etc in the table. Note that the indices differ by one, so MiscData(0) corresponds to PDATA1, etc.
You will also need to use the undocumented Repository.Execute() since Repository.SQLQuery() only allows select statements.
So the following should work:
foreach (EA.Connector conn in element.Connectors) {
EA.Element newNote = Package.Elements.AddNew("MyNote", "Note");
newNote.Subtype = 1;
newNote.Notes = "Some string";
newNote.Update();
repository.Execute("update t_object set PDATA4='idref=" + conn.ConnectorID + ";' " +
where Object_ID=" + newNote.ElementID);
//position calculation is left out here
EA.DiagramObject k = diagram.DiagramObjects.AddNew(position, "");
k.ElementID = newNote.ElementID;
k.Sequence = 9;
k.Update();
}
You may need to set the element subtype after the database update, I'm not sure.
Element.Subtype values are undocumented in the API, as are the contents of Element.MiscData, so this solution isn't future-proof (but it's very unlikely EA will ever change the way it handles these things).

Outlook add-in, cannot add mailitem into a custom folder

I want to take mails from inbox and sentbox folders, compare their subjects and if they match, put it all into a new custom folder.
Here's the code so far:
Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)
this.Application.ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);
// I also have made this for the sentBox folder
string userName = (string)this.Application.ActiveExplorer()
.Session.CurrentUser.Name;
Outlook.MAPIFolder customFolder = null;
customFolder = (Outlook.MAPIFolder)inBox.Folders.Add(userName,
Outlook.OlDefaultFolders.olFolderInbox);
inBox.Folders[userName].Display();
// This is the custom folder in which i wish to place the matching mails
for (int i = 1; i <= sentboxFolder.Items.Count; i++)
{
outboxItem = sentboxFolder.Items[i];
for (int a = 1; a <= inBox.Items.Count; a++)
{
inboxItem = inBox.Items[a];
if ("RE: " + outboxItem.Subject == inboxItem.Subject)
{
customFolder.Items.Add(inboxItem);
// Here I loop through the inbox and outbox folders and if the subjects match I want to add the inbox part to the custom folder.
I have 3 questions:
1. Is there a way to put both matching mails into one folder ?
2. I know there should be a smarter way to this aside from comparing subjects, Can anyone help how to use conversation ID here?
3. I get an exception at the last line, that it cannot add inbox item into custom folder because it's not an actual object instance. Where should I instantiate mailitem to fix this ?
Thanks in advance.
Firstly, do not use multiple dot notation, especially in a loop - cache the Items collection before entering the loop.
Secondly, do not just loop through all items in a folder looking for a match - use Items.Find.
That being said, you can use MailItem.Move(OtherFolder) . If you want to preserve the original item, use MailItem.Copy (returns new item), then move it to the target folder.

Retrieve Lotus Notes contact information using contact ID in c#

Im developing a tool that needs to access to the names.nsf database inside IBM Lotus Notes, and, using the lotus contact ID (Employee ID) (this id will be provided by the user), retrieve the full information of the person (Name, Position, Phone #....)
I found an example at Codeproject.com (http://www.codeproject.com/Articles/18517/Lotus-Notes-Integration-with-Microsoft-NET-Platfor), however it takes around 10 minutes to get the information the way the example does it (the database has more or less 5000 entries), so I'm searching for a faster way of doing it (if I actually use Lotus notes for this it takes about a second!).
Is there a way to accomplish this task without having the user waiting for minutes?
Thought that maybe you can help me out with this one.
The sample you are using goes through the view using
NotesViewEntry viewEntry = notesViewCollection.GetNthEntry( rowCount );
This is (one of) the worst methods to use as it goes for every iteration from the top of the view and iterates through all docs until it reached the nth document.
There are two options:
1) Optimize this code by using
NotesViewEntry viewEntry = notesViewCollection.GetFirstEntry();
and at the end
viewEntry = notesViewCollection.GetNextEntry(viewEntry);
2) (in my humble opinion the better way): Change the code:
- you need a view with the first column sorted by your key => contact ID (Employee ID)
- You can the access the ViewEntry by a code like
LotusNotesView.GetEntryByKey( EmployeeID, true);
If you are lucky the names.nsf is full text indexed. If it's not you could try to ask if it could be full text indexed. When it's indexed you can get the person document quicly like this:
LotusNotesView.FTSearch("[EmployeeID]=1234567", 1);
NotesDocument docPerson = LotusNotesView.GetFirstDocument();
The use of GetNthEntry certainly causes some performance issues. I've taken the relevant code from that site and rewrote it to use the GetFirst/GetNext pattern, which is recommended for all view processing in Lotus Notes.
Note this hasn't been tested, of course. The point is to get the first entry in your collection, check that it is an object, and then process it. At the end of the loop, get the next entry and repeat until you hit null.
NotesViewEntryCollection notesViewCollection = LotusNotesView.AllEntries;
NotesViewEntry viewEntry = notesViewCollection.GetFirstEntry();
while (viewEntry != null)
{
//Get the first document of particular entry.
NotesDocument document = viewEntry.Document;
object documentItems = document.Items;
Array itemArray1 = (System.Array)documentItems;
for( int itemCount=0 ; itemCount< itemArray1.Length; itemCount++ )
{
NotesItem notesItem =
(Domino.NotesItem)itemArray1.GetValue( itemCount );
//compare field value with specific value entered by user
if( notesItem.Text !=null )
{
if( (notesItem.Text.ToUpper()).StartsWith( fieldValue ))
{
Contact contact = new Contact();
for( int icount=0 ; icount< itemArray1.Length; icount++ )
{
NotesItem searchedNotesItem =
(Domino.NotesItem)itemArray1.GetValue( icount );
string FieldName = searchedNotesItem.Name.ToString();
//For FirstName
if( searchedNotesItem.Name == "FirstName" )
contact.FirstName= searchedNotesItem.Text;
//For LastName
if( searchedNotesItem.Name == "LastName" )
contact.LastName = searchedNotesItem.Text;
//For Office Phone Number
if( searchedNotesItem.Name == "OfficePhoneNumber" )
contact.OfficePhoneNumber = searchedNotesItem.Text;
if( searchedNotesItem.Name == "InternetAddress" )
contact.EmailId = searchedNotesItem.Text;
}//end for
contactsList.Add( contact );
break;
}//End if
}
}
//Get the nth entry of the selected view according to the iteration.
NotesViewEntry viewEntry = notesViewCollection.GetNextEntry(viewEntry);
}
Why are you asking the user to provide his Employee ID? You should ask him to provide his Notes username (either FullName or ShortName), or his email address. Any of those can be looked up very quickly in the $Users view in names.nsf, giving you fast access to the document containing all the data that you need.
Note: I'm aware that some companies actually enter their Employee ID into the ShortName field in names.nsf. If that's the case for your organization, then what you should be doing is opening a NotesView object using the NotesDatabase.getView() method, and then use the NotesView.getDocumentByKey() method to get the document for the user. E.g., something like this:
NotesView usersView = namesDb.getView("$Users");
NotesDocument userDoc = usersView.getDocumentByKey(employeeId);
Then just read the data that you want, using userDoc.getItemValue() for each information field that you are interested in. You should only do a loop through the entire userdoc.Items array if you are really trying to capture everything, including a bunch of internal-use values.

Using lambda expression to get values in an array

Alright, here's the deal. I am doing a data conversion where I'm taking data from two databases and putting it into another. I'm adding a list of contacts, and then I'm adding a list of communication records. In order to simplify the process, I made a small array of all of the communication records with the household address of the contacts. Now I'm trying to use a lambda expression to sort out email addresses from the array, but I'm having a problem. The code so far is as follows:
DataRow[] Comms = dtComms.Select("household_id = " + previousID);
if (Comms.Where(x => x.Field<string>("communication_type") == "Home Phone").Count() > 0)
{
string HomePhone = rNDigits.Replace(Comms[0].Field<string>("communication_value").ToString().Trim(), "");
if (HomePhone.Length > 6)
oAddress._Phone = HomePhone;
}
if (Comms.Where(x => x.Field<string>("communication_type") == "Email").Count() > 0)
{
string FamilyEmail = rNDigits.Replace(Comms[0].Field<string>("communication_value").ToString().Trim(), "");
if (FamilyEmail.Contains('#') && FamilyEmail.Contains('.'))
oAddress._FamilyEmail = FamilyEmail;
}
The problem is that obviously, this always will return the first value in the array, which might not always be the one that I want. How can I change the code so that it selects only the value from the array that matches the entry containing the email? Or, is there a better way to search through values in an array?
I suggesting to use a simple for or foreach loop in this case, LINQ can't modify data only select it.

Categories

Resources