Is it possible to create an extended property across all item types ie MapiContact, MapiCalendar, MapiMessage.
Hi Ashley,
Thank you for contacting Aspose support team.
You may please use functions like AddCustomProperty(), SetProperty() and Properties.Add() functions to set custom properties, as shown in the following sample code. It adds properties to MapiMessage, MapiCalendar, MapiContact and folder.
string YOUR_DATA_STRING = "This is a simple data text which is set by user.";
MapiMessage mapi1 = new MapiMessage("from@domain.com", "to@domain.com", "Subject", "Body");
long tag = mapi1.NamedPropertyMapping.GetNextAvailablePropertyId(MapiPropertyType.PT_UNICODE);
MapiProperty property = new MapiProperty(tag,Encoding.Unicode.GetBytes(YOUR_DATA_STRING));
mapi1.AddCustomProperty(property, "StringProp");
MapiContact contact1 = new MapiContact("Sebastian Wright", "SebastianWright@dayrep.com");
contact1.SetProperty(property);
MapiCalendar calendar1 = new MapiCalendar("Location", "Summary", "Description", new DateTime(2015, 7, 28), new DateTime(2015, 7, 29));
calendar1.SetProperty(property);
if (File.Exists("output21.pst"))
File.Delete("output21.pst");
PersonalStorage pst = PersonalStorage.Create("output21.pst", FileFormatVersion.Unicode);
FolderInfo info = pst.CreatePredefinedFolder("Inbox", StandardIpmFolder.Inbox);
info.Properties.Add(tag, property);
Thanks for getting back to me and the code sample.
Hi Ashley,
Not, sure what happened. Appears my reply to the last post on this thread has disappeared. Will try again
I’ve tried adding a property to a MapiContact and it does not appear to work or give the desired results, it appears the tag with. [Here is a screen shot:](http://images.devs-on.net/Image/5yK1Go10DftQCMwH-fileCProjectsAs.png)
At the top of the screen you will see the tag id and the value it should contain, when I look at the corresponding tag in the properties it has another value
private static void Main(string[] args) {
string id = “thisismyid”;
MapiMessage mapi1 = new MapiMessage();
long tag = mapi1.NamedPropertyMapping.GetNextAvailablePropertyId(MapiPropertyType.PT_UNICODE);
MapiProperty property = new MapiProperty(tag, Encoding.Unicode.GetBytes(id));
if (!File.Exists(_pstPath)) {
using (PersonalStorage pst = PersonalStorage.Create(_pstPath, FileFormatVersion.Unicode)) {
FolderInfo folder = pst.CreatePredefinedFolder(“Contacts”, StandardIpmFolder.Contacts);
Console.WriteLine(folder.EntryIdString);
Add(mapi1, tag, property, pst);
Read(mapi1, tag, property, pst);
}
} else {
using (PersonalStorage pst = PersonalStorage.FromFile(_pstPath)) {
Read(mapi1, tag, property, pst);
}
}
Console.ReadKey();
}
private static void Read(MapiMessage mapi1, long tag, MapiProperty property, PersonalStorage pst) {
FolderInfo folderInfo = pst.RootFolder.GetSubFolder(“Contacts”);
FolderInfo subFolderInfo = folderInfo.GetSubFolder(“Sub Folder”);
MessageInfoCollection coll = subFolderInfo.GetContents();
Console.WriteLine(“TAG : " + tag);
foreach (MessageInfo messageInfo in coll) {
MapiContact contact = (MapiContact)pst.ExtractMessage(messageInfo).ToMapiMessageItem();
foreach (DictionaryEntry entry in contact.Properties) {
Console.WriteLine(entry.Key + “|” + entry.Value);
}
}
Console.WriteLine(Environment.NewLine);
}
private static void Add(MapiMessage mapi1, long tag, MapiProperty property, PersonalStorage pst) {
mapi1.AddCustomProperty(property, “StringProp”);
FolderInfo folderInfo = pst.RootFolder.GetSubFolder(“Contacts”);
FolderInfo subFolderInfo = folderInfo.GetSubFolder(“Sub Folder”);
if (subFolderInfo == null) {
subFolderInfo = folderInfo.AddSubFolder(“Sub Folder”, “IPF.Contact”);
}
MapiContact contact1 = new MapiContact(“Sebastian Wright”, "SebastianWright@dayrep.com”);
contact1.SetProperty(property);
subFolderInfo.AddMapiMessageItem(contact1);
Console.WriteLine(Environment.NewLine);
}
Hi Ashley,
Thanks, I didn’t get any email notifying me that it had been created.
Not sure whats going on here, I’m using the following code:
MapiMessage mapiMessage = new MapiMessage();
long tag = mapiMessage.NamedPropertyMapping.GetNextAvailablePropertyId(MapiPropertyType.PT_UNICODE);
return tag;
This returns the following : 2147483679
I use the following to set the value :
MapiProperty TagItem(string id) {
long tag = GetTag();
return new MapiProperty(tag, Encoding.Unicode.GetBytes(id));
}
Message.SetProperty(TagItem(“myid”));
At this point just before adding the item I checked the NameProperties I can see my id under [2147483678](http://images.devs-on.net/Image/9WA6YPpvCltbHXZS-Region.png)
When I go to read the contents of the PST and try and retrieve the id it appears under [2147614751](http://images.devs-on.net/Image/LuUgdePJ4Hj7mtR7-Region.png)
My previous test was done on a very basic mapi message, Is this a bug?
Thanks
Hi Ashley,
Hi Ashley,
As mentioned by Dmitry, nammed properties are differentiated by their names. You can use the following sample code to find the property that you have set.
Code:
MapiMessage msg = new MapiMessage("user1@gmail.com", "user2@gmail.com", "Sample Subject", "Sample Body");
MapiNamedPropertyMappingStorage mappingStorage = msg.NamedPropertyMapping;
//Set property 1
Guid PS_PUBLIC_STRINGS = new Guid("00020329-0000-0000-C000-000000000046");
MapiProperty stringProperty = new MapiProperty(mappingStorage.GetNextAvailablePropertyId(MapiPropertyType.PT_UNICODE), Encoding.Unicode.GetBytes("test1"));
msg.SetProperty(stringProperty);
string stringNameId = "Archive ID";
mappingStorage.AddNamedPropertyMapping(stringProperty, stringNameId, PS_PUBLIC_STRINGS);
//Set property 2
Guid PS_PUBLIC_STRINGS2 = new Guid("00020329-0000-0000-C000-000000000047");
MapiProperty stringProperty2 = new MapiProperty(mappingStorage.GetNextAvailablePropertyId(MapiPropertyType.PT_UNICODE), Encoding.Unicode.GetBytes("test2"));
msg.SetProperty(stringProperty2);
string stringNameId2 = "Saveset ID";
mappingStorage.AddNamedPropertyMapping(stringProperty2, stringNameId2, PS_PUBLIC_STRINGS);
msg.Save(@"test.msg");
MapiMessage msgSaved = MapiMessage.FromFile(@"test.msg");
// Get all named MAPI properties
MapiPropertyCollection properties = msgSaved.NamedProperties;
// Read all properties in foreach loop
foreach (MapiNamedProperty mapiNamedProp in properties.Values)
{
// Read any specific property
switch (mapiNamedProp.NameId)
{
case "Archive ID":
Console.WriteLine("{0} = {1}", mapiNamedProp.NameId, mapiNamedProp.GetString());
break;
case "Saveset ID":
Console.WriteLine("{0} = {1}", mapiNamedProp.NameId, mapiNamedProp.GetString());
break;
default:
break;
}
}
Thanks, just what I was looking for.
HI Ashley,