How to add a category to an email item - c#

I am updating an old Outlook.Interop App that moves emails around, downloads attachments, and auto assigns categories to the emails that it moves. In the old app you could do something like...
email.Categories = "Blue Category";
That does not work in EWS. Does anyone have any ideas?
Thanks
I have tried:
email.Categories.Add("Blue Category");
email.Categories.Equals("Blue Category");
and obviously:
email.Categories = "Blue Category";

email.Categories.Add("Blue Category");
is the way to do it you also need to call update on the Item for the change to be saved.I suggest you enable tracing https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-trace-requests-responses-to-troubleshoot-ews-managed-api-applications then you should be able to see if the update is being sent to the server. You can also see if the change has been applied by calling load on the Item after the update and see if the property on the Item has been updated on the server (as you may just be having a client cache issue).

Related

Clearing a product from nopCommerce cache programatically

I have run into a problem where I need to manually click the clear cache button for a specific product to appear correct on a specific page after it's saved.
I'm wondering how I can go about this with code, but only clear the cache for that product.
clearing complete cache can be done like this:
var _cacheManager = EngineContext.Current.Resolve<ICacheManager>();
_cacheManager.Clear();
I have tried doing it like this but it didn't work.
var product = _productService.GetProductById(productModel.Id);
var productTemplateCacheKey = string.Format(ModelCacheEventConsumer.PRODUCT_TEMPLATE_MODEL_KEY,
productModel.Id);
_cacheManager.Remove(productTemplateCacheKey);
Not sure how to go about this, anyone got any ideas?
Thanks

EWS Attached emails missing DatatimeReceived property after .Load()

I am currently working on a former colleague's project that deals with saving emails from Exchange to our ERP system.
But I have run into a strange problem. Sometimes when the system receives an email, that contains an email as an attachment. It throws an error because the DataTimeReceived date is not set. Even after a .Load();
private int HandleEmail(Item item, Folder moveToFolder, Folder moveToFailedFolder, Mailbox mailbox, int fatherId = 0, string uploaderEmail = "", bool isEmbeddedMail = false)
The way it work. is that the HandleEmail() method goes through the original email until it gets the if-check.
If the attachment is not a FileAttachment, and is an ItemAttachment. The following code is called.
else if (attachment is ItemAttachment)
{
var itemAttachment = attachment as ItemAttachment;
itemAttachment.Load(new PropertySet(ItemSchema.Attachments, ItemSchema.TextBody, EmailMessageSchema.Sender, EmailMessageSchema.DisplayCc, EmailMessageSchema.DateTimeReceived, EmailMessageSchema.From, ItemSchema.MimeContent, ItemSchema.Body, ItemSchema.TextBody, EmailMessageSchema.BccRecipients, ItemSchema.Attachments));
var item_ = itemAttachment.Item;
HandleEmail(item_, null, null, mailbox, (int)mailid, uploaderEmail, true); // The attached email is then looped through like it is an regular email instead of like an image.
}
In this check. It takes the item attachment and loads an additional property set, which contains some data that is needed.
Finally the method calls it self, but now with the Item Attachment as to read it like a regular email.
My issue is the fact, that some attached emails do not have the DatetimeReceived property set.
Instead this error is shown in its place.
Microsoft.Exchange.Webservice.Data.ServicesObjectPropertyException
And this expection message is thrown
You must load or assign this property before you can read its value
And I can't quite figure out why it only happens to some attached emails. When I look at the emails it self, it does have an received date. But for some reason I can't get it using .Load()
I've tried a few things, such as using .Load for both Item and ItemAttachment, both without getting anything worthwhile.
Tried looking into using the service.loadpropertiesforitems()
I've forgotten a few of the other things I've looked into, since I have been looking at this for a few days before the weekend aswell.
Two suggestions i would have is first try the EmailMessageSchema.DateTimeSent (which should be the same as DateTimeReceived) the difference maybe being caused when attaching a messages that was sent. The other thing would be enable tracing and have a look at the traces to see what is actually coming back from the server also maybe just try loading the BasePropertySet.FirstClassProperties rather then your custom set (just to test that property anyway)

C# Sharepoint 2010 Alert List not found

I´m trying to write an application to set Alerts on a list for all people that are already having minimum one alert in the sitecollection. I know how to get the user objects for all this users, but when I try to set the alert for each user there appears the problem. For some users there comes an SPException that says that the list is not existing. I looked in the user.Alerts propertie and there is a field Web, where the sitecollection name is at the users that dont work. It works only for the users that have the right web in there to access the list and set the alert. Does anyone know a workaround so that i can set alert for all the users that i want?
Here is the code I´m using to set the alerts:
SPAlert alert = user.Alerts.Add();
alert.AlertType = SPAlertType.List;
alert.EventType = SPEventType.Add;
Console.WriteLine(alertWeb.Url+" "+ alertingListName);
alert.List = alertWeb.Lists.TryGetList(alertingListName);
alert.AlertFrequency = SPAlertFrequency.Immediate;
alert.AlwaysNotify = false;
alert.Update(false);
Thanks in advance for helping me
I have solved the problem on myself now. I post the solution here so people who have the same problem can find it.
The problem was that in the User Object propertie user.alerts.web was the wrong web. I solved this problem by getting the user object not from the web.alerts collection but from the web.allusers collection, so that the right web stands in the propertie.

Exchange Managed API: Why is the event not fired when a item is deleted?

I use pull subscriptions in exchange managed api to catch newly created, modified or deleted items.
It works fine for created or modified appointments, but for some reason it doesn't work for deleted items. Here is how i created the subscription:
PullSubscription subscription = m_exchangeService.SubscribeToPullNotifications(
new FolderId[] { WellKnownFolderName.Calendar },
1440,
null,
EventType.Created, EventType.Modified, EventType.Deleted);
subscriptions.Add(usermail, subscription);
Do I have to configure something in exchange to make it work? Does this maybe only work for hard deleted appointments, and not for appointments that moved to the deleted folder?
Items are moved to the recycle bin. Therefore, you don't get a delete notification.
Get the FolderId of the Delected Items folder by binding to it via the WellknownFolder Enumeration. Then compare the UniqueId of that folder with the unique Id of the target of the move event.
When i got a deleted contact i got a Moved event.
Then i have to do this to check when a contact is deleted :
switch (outlookEvent.EventType)
{
case EventType.Moved:
var folder = Folder.Bind(subscription.Value.EwsInstance, WellKnownFolderName.DeletedItems);
if (Equals(outlookEvent.ParentFolderId.UniqueId, folder.Id.UniqueId))
{
Console.WriteLine("Moved to DeletedItems " + outlookEvent.ItemId);
}
In case of an Appointment, i got a Modified Event, then i wanted to do the some kind of code but the FolderId is not the DeletedItems but the Calendar for this one...
I would be interested to see your code as the only one i see now is a try/catch :/

Enabling SPAudit automatically for new files in list

I'm working with the Audit framework in MOSS 2007, and I've programatically enabled Auditing for 2 specific lists in a bunch of sites in a site collection(meaning the sites all have their own two lists). I've only enabled the update mask, as I only want to know when something's been changed or added.
However, I'm only able to log when something's been added, with my current setup. To allow for seeing changes to the list items, I'm aware that the list item itself has to have auditing enabled. But when adding a new item to the list, how do I automatically enable Auditing for the item? If possible, I'd prefer to avoid creating an event receiver to accomplish this.
*EDIT
The code I use to enable auditing looks something like this:
foreach (SPSite site in webApp.Sites) {
foreach (SPWeb website in site.AllWebs) {
website.Lists["MyList"].Audit.AuditFlags = SPAuditMaskType.Update;
website.Lists["MyList"].Audit.Update();
}
}
And to read it:
SPUserToken sysAdmin = website.Site.SystemAccount.UserToken;
using (SPSite elevatedSite = new SPSite(website.Site.ID,sysAdmin)) {
using (SPWeb elevatedWeb = elevatedSite.OpenWeb(website.ID)) {
SPAuditQuery auditQuery = new SPAuditQuery(elevatedSite);
auditQuery.SetRangeStart(myDatetime);
auditQuery.RestrictToList(elevatedWeb.Lists["MyList"]);
listChanges = elevatedWeb.Lists["MyList"].Audit.GetEntries(auditQuery);
}
}
I realize I'm restricting to list here, but when I didn't I got changes from other lists on the site as well. Even though logically I'd assume I only got the changes from the list I called "GetEntries" on...
Thanks
If the library has auditing enabled, then new items will be audited. I'm not aware of any document-specific auditing
A little late in replying to this, but here is some additional information in case others find this post.
It is possible to track the auditing events at the document level. This link explains how it can be done. They use an event handler to make it work. I know you mention you do not want to do it this way, but this is what is needed to automatically track document events. Setting the audit mask type on the document library appears to only track the DL events, not the individual items.

Categories