How to load .pst file into Outlook.Application? - outlook

I need to read a couple of pst files and extract attachments.
The code below is loading the default existing Outlook profile (in this case my Office Outlook profile).
Outlook.Application OlApp = new Outlook.Application();
OlApp.Session.AddStore(pstfile); // Loading c:\\ xyz.pst
Outlook.Folders folders = OlApp.GetNamespace("MAPI").Folders;
.........
.....
Outlook.MailItem mi = (Outlook.MailItem)itms[j];
foreach (Outlook.Attachment attachment in mi.Attachments)
{
string fileName = attachment.FileName ?? attachment.DisplayName;
string filePath = txtDestination.Text + "\\" + fileName;
attachment.SaveAsFile(filePath);
}
How to load pst files into Outlook.Application and loop those folders?

After adding a new store to the profile, you can find your store in the Stores collection:
Outlook.Application OlApp = new Outlook.Application();
Outlook.Namespace ns = OlApp.GetNamespace("MAPI");
ns.AddStore(pstfile); // Loading c:\\ xyz.pst
Outlook.Stores stores = ns.Stores;
Outlook.Folders folders = stores[stores.Count].Folders;

Related

How to copy google doc to another folder in google drive service

I have a class that makes a copy of a google doc template file to then merge placeholders with data. The problem is that it's creating the copy in the same folder as the original template file and I can't find a way to copy it to another folder.
Say I have a structure like so in google drive.
Main
TemplateFiles
MainTemplateDoc
Contracts
CopyOfMainTemplateDocAfterBeingMerged
As you can see, the CopyOfMainTemplateDocAfterBeingMerged doc wouldn't be located in the TemplateFiles folder where the original template was, but copied to the Contracts folder.
Is it possible to use the google drive v2 service to move the file after creating the copy? I'm using the .net Google.Apis.Drive.v2 nuget package. Here's the code that I have so far to create the copy.
private string CopyDocument(string documentId, string title)
{
var newFile = new File { Title = title };
var documentCopyFile = driveService.Files.Copy(newFile, documentId).Execute();
return documentCopyFile.Id;
}
You are using Drive API v2.
You want to copy a file to the folder of Contracts.
You have already been able to use Drive API.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
In this answer, I would like to propose the following modification.
From:
var newFile = new File { Title = title };
To:
var newFile = new File {
Title = title,
Parents = new List<ParentReference> {new ParentReference {Id = parentId}}
};
or
var newFile = new File() {
Title = title
};
newFile.Parents = new List<ParentReference>() {new ParentReference() {Id = parentId}};
or
var newFile = new File();
newFile.Title = title;
newFile.Parents = new List<ParentReference>() {new ParentReference() {Id = parentId}};
parentId is the folder ID of the folder Contracts.
Note:
If you want to use Drive API v3, please use new List<string> {folderId} instead of new List<ParentReference> {new ParentReference {Id = folderId}}.
References:
Files: copy
Files: insert
If I misunderstood your question and this was not the direction you want, I apologize.

How to create an evet in other user calendar which I have write permision, using C#?

I am trying to create an application where I add an event in other calendar which I have write access to it.
I was manage to create an application to send an meeting invitation. But I would like to direct add to calendar?
Second step if the schedule was change I would like to change the event date.
Any one could give any idea in how can I create a C# code to add an event in other calendar?
Thanks for any idea!
Tried alredy sending an invitation for a meeting, this worked but it is necessary user approval, them it will not work very well for the application.
using OutLook = Microsoft.Office.Interop.Outlook;
public void NovoAtendimento(MeetInfo nwVisit)
{
OutLook.Application objOL = new OutLook.Application();
OutLook.AppointmentItem objAppt = (OutLook.AppointmentItem)objOL.CreateItem(OutLook.OlItemType.olAppointmentItem);
objAppt.Subject = nwVisit.Titulo;
objAppt.Body = nwVisit.Detalhe;
objAppt.Location = nwVisit.Local;
objAppt.Start = nwVisit.DiaInicio;
objAppt.End = nwVisit.DiaFim;
objAppt.MeetingStatus = OutLook.OlMeetingStatus.olMeeting;
objAppt.RequiredAttendees = nwVisit.Email;
objAppt.Save();
objAppt.Send();
objAppt = null;
objOL = null;
}
Instead of using Application.CreateItem, retrieve the shared folder using Namespace.CreateRecipient / Namespace.GetSharedDefaultFolder, then add an appointment using MAPIFolder.Items.Add.
After checking the hint from Dmitry, this was the final prograns lines that worked:
public void TestCheckCalendar(MeetInfo usrCall)
{
OutLook.Application myApp = new OutLook.Application();
OutLook.NameSpace myNs = myApp.GetNamespace("MAPI");
OutLook.Recipient myReci = myNs.CreateRecipient(usrCall.Email);
OutLook.MAPIFolder calFolder = myApp.Session.GetSharedDefaultFolder(myReci,OutLook.OlDefaultFolders.olFolderCalendar);
OutLook.Items itemTest = null;
OutLook.AppointmentItem appItem = null;
itemTest = calFolder.Items;
appItem = itemTest.Add(OutLook.OlItemType.olAppointmentItem) as OutLook.AppointmentItem;
appItem.Start = usrCall.DiaInicio;
appItem.End = usrCall.DiaFim;
appItem.Location = usrCall.Local;
appItem.Subject = usrCall.Titulo;
appItem.Body = usrCall.Detalhe;
appItem.Save();
}

Upload a image in a porltet Liferay

I am doing a portlet to create banners. I preferences I made the form with: input type="file" and the form nctype='multipart/form-data'
In the processAction I get the image, but I don't know how save it in the server, because I only get save in temporal instance portlet, but if I restart the server I lose the image.
This is my code to save the image:
private boolean uploadFile( ActionRequest request, ActionResponse response) throws ValidatorException, IOException, ReadOnlyException {
try {
// Si la request es del tipo multipart ...
if (PortletFileUpload.isMultipartContent(request)) {
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
PortletFileUpload servletFileUpload = new PortletFileUpload(diskFileItemFactory);
servletFileUpload.setSizeMax(81920); // bytes
List fileItemsList = servletFileUpload.parseRequest(request);
Iterator it = fileItemsList.iterator();
while (it.hasNext()){
FileItem fileItem = (FileItem)it.next();
if (fileItem.isFormField()){
}
else{
String nombreCampo = fileItem.getFieldName();
String nombreArchivo = fileItem.getName();
String extension = nombreArchivo.substring(nombreArchivo.indexOf("."));
PortletContext context = request.getPortletSession().getPortletContext();
String path = context.getRealPath("/images");
File archivo = new File(path + "/" + nombreArchivo);
PortletContext pc = request.getPortletSession().getPortletContext();
fileItem.write(archivo);
}
}
}
} catch (Exception e) {}
return true;
}
I don't know if I am doing something wrong or this isn't the correct way.
Any idea?
Thanks in advance
EDIT:
Finally I tried do it with DLFolderLocalServiceUtil and DLFileEntryLocalServiceUtil, but it doesn't work correctly. When I load the page you can see the image, but after, when the page is load completely, the image disappears.
I don't know if it is because I don't create fine the fileEntry or the url is wrong.
This is my code:
long folderId = CounterLocalServiceUtil.increment(DLFolder.class.getName());
DLFolder folder = DLFolderLocalServiceUtil.createDLFolder(folderId);
long userId = themeDisplay.getUserId();
long groupId = themeDisplay.getScopeGroupId();
folder.setUserId(userId);
folder.setGroupId(groupId);
folder.setName("Banner image " + nombreArchivo+String.valueOf(folderId));
DLFolderLocalServiceUtil.updateDLFolder(folder);
ServiceContext serviceContext= ServiceContextFactory.getInstance(DLFileEntry.class.getName(), request);
File myfile = new File(nombreArchivo);
fileItem.write(myfile);
List<DLFileEntryType> tip = DLFileEntryTypeLocalServiceUtil.getFileEntryTypes(DLUtil.getGroupIds(themeDisplay));
DLFileEntry DLfileEntry = DLFileEntryLocalServiceUtil.addFileEntry(userId, groupId, 0, folderId, null, MimeTypesUtil.getContentType(myfile), nombreArchivo, "Image banner_"+nombreArchivo, "", tip.get(0).getFileEntryTypeId(), null, myfile, fileItem.getInputStream(), myfile.getTotalSpace(), serviceContext);
FileVersion fileVersion = null;
//FileEntry fileEntry = DLAppServiceUtil.getFileEntry(groupId, folderId, nombreArchivo);
//String path = DLUtil.getPreviewURL(fileEntry, fileVersion, themeDisplay, "&imagePreview=1");
String path1 = themeDisplay.getPortalURL()+"/c/document_library/get_file?uuid="+DLfileEntry.getUuid()+"&groupId="+themeDisplay.getScopeGroupId();
String path = "/documents/" + DLfileEntry.getGroupId() + "/" + DLfileEntry.getFolderId() + "/" + DLfileEntry.getTitle()+"/"+DLfileEntry.getUuid();
System.out.println("path " + path);
System.out.println("path " + path1);
prefs.setValue(nombreCampo, path);
And this is the output:
path /documents/10180/0/cinesa888.png/f24e6da2-0be8-47ad-a3b5-a4ab0d41d17f
path http://localhost:8080/c/document_library/get_file?uuid=f24e6da2-0be8-47ad-a3b5-a4ab0d41d17f&groupId=10180
I tried to get the url like lpratlong said (DLUtil) but when I tried to get the FileEntry with DLAppServiceUtil.getFileEntry(..) I have an error that says no exist FileEntry.
I don't know what I am doing wrong.. Any idea?
Thanks.
You can use Liferay API to store the file in the Document Library : take a look in DLFolder and DLFileEntry API (for exemple, DLFileEntryLocalServiceUtil will show you allowed local operations).
These API will allowed you to store your file in your file system (in the "data" folder of your Liferay installation) and to store reference of your file in Liferay database.

How to create user properties with EWS or EWS Managed API so that they appear in field chooser

I would like to create User Properties using EWS so that they appear in the field chooser in Outlook. I know it's possible using VBA, the object model exposes an Item.UserProperties collection. However, using EWS I can only access Item.ExtendedProperty.
The issue with ExtendedProperty is that is doesn't appear in the selectable user properties list.
The underlying issue is that our server-side application tries to work nicely together with the Dynamics CRM Outlook Client. The CRM Outlook client uses UserProperty for storing custom properties and our application can only work with EWS and we cannot find a way to create user properties.
Some example code:
User Properties (VBA Outlook):
Dim WithEvents m_objApp As Outlook.AppointmentItem
Private Sub Application_ItemLoad(ByVal Item As Object)
If Item.Class = olAppointment Then
Set m_objApp = Item
End If
End Sub
Private Sub m_objApp_Open(Cancel As Boolean)
Dim oProp1 As UserProperty
Dim oProp2 As UserProperty
If m_objApp.UserProperties.Count = 0 Then
Set oProp1 = m_objApp.UserProperties.Add("crmid", olText)
oProp1.Value = ""
Set oProp2 = m_objApp.UserProperties.Add("crmLinkState", olText)
oProp2.Value = "0"
m_objApp.Save
End If
End Sub
Extended Properties (Exchange EWS):
CalendarItemType item = new CalendarItemType();
item.MeetingTimeZone = new TimeZoneType() { TimeZoneName = _userTimeZone };
item.StartSpecified = true;
item.Start = GetDateFromXml(node.Value);
item.EndSpecified = true;
item.End = GetDateFromXml(node.Value);
List<ExtendedPropertyType> properties = new List<ExtendedPropertyType>();
properties.Add(CreateExtendedProperty("crmid", pending.CrmId.Value.ToString(), MapiPropertyTypeType.String));
properties.Add(CreateExtendedProperty("crmLinkState", "2", MapiPropertyTypeType.Double));
item.ExtendedProperty = properties.ToArray();
CreateRequest createRequest = new CreateItemType()
{
Items = new NonEmptyArrayOfAllItemsType
{
Items = new ItemType[] { item }
},
SavedItemFolderId = new TargetFolderIdType()
{
Item = new DistinguishedFolderIdType()
{
Id = folder,
Mailbox = new EmailAddressType() { EmailAddress = _user.MailBox }
}
},
SendMeetingInvitations = CalendarItemCreateOrDeleteOperationType.SendToNone,
SendMeetingInvitationsSpecified = true
};
CreateItemResponseType response = exchange.CreateItem(createRequest);
private ExtendedPropertyType CreateExtendedProperty(string name, string value, MapiPropertyTypeType type)
{
return new ExtendedPropertyType()
{
ExtendedFieldURI = new PathToExtendedFieldType()
{
PropertyName = name,
DistinguishedPropertySetId = DistinguishedPropertySetType.PublicStrings,
DistinguishedPropertySetIdSpecified = true,
PropertyType = type
},
Item = value
};
}
A similar question has been asked on a Microsoft forum almost a year ago, but no answer yet. http://social.technet.microsoft.com/Forums/en-NZ/exchangesvrdevelopment/thread/c4d6bbb9-ba6a-4aa4-9e39-98a52b733a8c
I was hoping SO would be more successful :)
Thanks,
Jeffry
I thought the two methods were equivalent as long as you used publicstrings (which it looks like you do). How about using MFCMAPI to see the difference in what's generated?

Get Folder from Outlook using C# & GetFolderFromID EntryIdStore

I'm trying to get a specific folder in outlook with c#. Someone else had the same issue here Using Outlook API to get to a specific folder but when using the Folders collection I'm not sure how to get through the folders collection. I mean, I've looked at the type of object that the Folders collection returns and it looks like it's a Folders object. But when I try to use that in a loop it gives me an invalid cast exception. I also hoped I could use the GetFolderFromID method to give it the string name of the folder but that doesn't want to work either... but I also can't find an example of how to use it so I'm not sure I'm coding it correctly. Here's an example of what I tried. Anyone know how to get the Processed folder which is on the same level as the Inbox folder? Thanks.
MAPIFolder oProcessed = null;
foreach (var folder in oNS.Folders)
if (folder.ToString() == "Processed")
{
oProcessed = (MAPIFolder)folder;
}
if (oProcessed == null)
throw new Exception("Missing processed folder.");
you need to get hold of the root level mailbox folder
Outlook.MAPIFolder rootFolder= Application.Session.DefaultStore.GetRootFolder();
Then loop through the rootFolder folders collection check in the names
Outlook.MAPIFolder processedFolder = null;
foreach (Outlook.MAPIFolder folder in rootFolder.Folders)
{
if (folder.Name == "Processed")
{
processedFolder = folder;
break;
}
}
Check out http://msdn.microsoft.com/en-us/library/bb176810.aspx to get you head round the API.
Marcus
Outlook.MAPIFolder rootFolder = Application.Session.DefaultStore.GetRootFolder();
var processedFolder = rootFolder.Folders.Cast<Outlook.MAPIFolder>().Where(r => r.Name == "Processed").FirstOrDefault();
This is an inept translation from VBA, but may offer some ideas, seeing you have no answers as yet. In VBA, it is best to get the parent folder of Inbox and to look in that for folders at the same level.
Microsoft.Office.Interop.Outlook._Folders oFolders;
Microsoft.Office.Interop.Outlook.MAPIFolder oPublicFolder =
olNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox).Parent;
//Folders at Inbox level
oFolders = oPublicFolder.Folders;
foreach (Microsoft.Office.Interop.Outlook.MAPIFolder Folder in oFolders)
{
string foldername = Folder.Name;
if (foldername == "Test")
Console.Write(Folder.Name);
}
If you have the folder path as a string, you can use this function:
private MAPIFolder GetFolder(string folderPath, Folders folders)
{
string dir = folderPath.Substring(0, folderPath.Substring(4).IndexOf("\\") + 4);
try
{
foreach (MAPIFolder mf in folders)
{
if (!(mf.FullFolderPath.StartsWith(dir))) continue;
if (mf.FullFolderPath == folderPath) return mf;
else
{
MAPIFolder temp = GetFolder(folderPath, mf.Folders);
if (temp != null)
return temp;
}
}
return null;
}
catch { return null; }
}

Resources