Rename Folders in Outlook PST-File - c#

since a few days I'm trying to rename the sent mail folder, deleted elements and the inbox folder via c#.
I've tryed something like this:
List<Outlook.MailItem> mailItems = new List<Outlook.MailItem>();
Outlook.Application app = new Outlook.Application();
Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
// Add PST file (Outlook Data File) to Default Profile
outlookNs.AddStore(pstFilePath);
Outlook.MAPIFolder rootFolder = outlookNs.Stores[pstName].GetRootFolder();
Outlook.Folders subFolders = rootFolder.Folders;
foreach (Outlook.Folder folder in subFolders)
{
folder.Name = (folder.Name == "deleted Elements"?"deleted":folder.Name);
}
But without success. I always get the exceptiion that I do not have permissions to change the name. Other custom created folders I'm able to rename without any problems.
Is there something to do to unlock the folder?
Or is there an other possibility to access the folders?
Thanks a lot
Edit: The Expetion is: You do not have permissions.

public string RenameFolder(string name, string folderid)
{
Outlook.Application app = new Outlook.Application();
Outlook.NameSpace ns = null;
Outlook.Folder folder = null;
string n= null;
try
{
ns = app.GetNamespace("MAPI");
folder = ns.GetFolderFromID(folderid) as Outlook.Folder;
n=folder.Name;
folder.Name = (folder.Name = name) ;
return n + " has been successfully changed to " + folder.Name;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (app != null)
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(app);
}
if (folder != null)
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(folder);
}
if (ns != null)
{
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(ns);
}
}
}
this code is working for me..when i run visual studio in administator mode..

Related

"File not found": when trying to create a new Folder in a Library with blank at the first in the title

I am trying to create a new folder within a document library. Actually, the code works well, but when the title of the new folder starts with a blank, I get an exception "File not found" and the folder is not created.
I already tried to encode the title: replaced the blach by "%20" and by "+". In this case the folder is created, but it contains other characters like"+" at the first position in the title.
I tried to create the folder " blankBefore" in the SharePoint application by using the browser - and it works. The folder is create and looks like this " blankBefore".
I can create folders with a blank in the title, but not, if the title of the folder starts with a blank.
public bool CreateFolder(SharePointNode spParentNode, string strFolderName)
{
ClientContext localCTX = new ClientContext(spParentNode.ParentSite);
ConectClient(localCTX);
Folder newFolder = null;
var folder = localCTX.Web.GetFolderByServerRelativeUrl(spParentNode.URL);
localCTX.Load(folder);
localCTX.Load(folder.Folders);
Folder newFolder = folder.Folders.Add(strFolderName);
newFolder.Update();
localCTX.ExecuteQuery();
return true;
}
Create folder in SharePoint list (SharePoint 2010, 2013, 2016)
using (var clientContext = new ClientContext("http://sp/sites/test"))
{
string folderName = "test";
var list = clientContext.Web.Lists.GetByTitle("ListBase");
list.EnableFolderCreation = true;
clientContext.Load(list);
clientContext.Load(list.RootFolder);
clientContext.Load(list.RootFolder.Folders);
clientContext.ExecuteQuery();
var folderCollection = list.RootFolder.Folders;
foreach (var folder in folderCollection)
{
if (folder.Name == folderName)
{
clientContext.Load(folder.Files);
clientContext.ExecuteQuery();
}
else
{
var itemCreateInfo = new ListItemCreationInformation
{
UnderlyingObjectType = FileSystemObjectType.Folder,
LeafName = folderName
};
var newItem = list.AddItem(itemCreateInfo);
newItem["Title"] = folderName;
newItem.Update();
clientContext.ExecuteQuery();
break;
}
}
}

How to read emails from Clutter outlook folder C#

I am trying to read the emails that are being moved to the clutter folder by outlook. The below code works fine for other folders, but when I try to open the clutter folder the code defaults to the error message indicate the folder does not exist.
oApp = new Microsoft.Office.Interop.Outlook.Application();
oNS = (Microsoft.Office.Interop.Outlook._NameSpace)oApp.GetNamespace("MAPI");
oNS.Logon(null, null, false, false);
oFolder = oNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
string folderName = "Clutter";
try
{
oSubfolder = oFolder.Folders[folderName];
for (int i = 1; i <= oSubfolder.Items.Count; i++)
{
item = (Microsoft.Office.Interop.Outlook.PostItem)oFolder.Items[i];
}
}
catch
{
MessageBox.Show("There is no folder named " + folderName +
".", "Find Folder Name");
}
You are assuming Clutter is the child of the Inbox folder. It is Inbox's peer:
oFolder = (Outlook.MAPIFolder)oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Parent;

How could I add nodes of a treeview as folders from a remote directory with WinSCP and C#

I'm trying to create a tree view to search for a directories in a remote server using FTP/SFTP connections, What I'm trying to do is start filling the tree view with all the available directories starting with the home directory such as the following example:
Home---->SubFolder
|
|---->Another Folder
|
|---->MyOtherFolder
Then when the user start clicking in each folder it start to display their subdirectories from the tree view as the follwoing example (clicking in Another Folder):
Home ---->SubFolder
|
|---->Another Folder -------> MyFolder1
| | -------> MyFolder2
|
|---->MyOtherFolder
I'm trying to get those folders but it's throwing an exception, also it is gathering files, not folders....
this is the code that I have....
private void FillTree()
{
SessionOptions SessionOptions = new SessionOptions();
Session MySession = new Session();
SessionOptions.HostName = InterfaceValues[0];
SessionOptions.UserName = InterfaceValues[2];
SessionOptions.Password = InterfaceValues[3];
SessionOptions.PortNumber = Convert.ToInt32(InterfaceValues[1]);
if (string.Compare(InterfaceValues[9], "FTP", true) == 0)
SessionOptions.Protocol = WinSCP.Protocol.Ftp;
else if (string.Compare(InterfaceValues[9], "SFTP", true) == 0)
{
SessionOptions.Protocol = WinSCP.Protocol.Sftp;
SessionOptions.SshPrivateKeyPath = InterfaceValues[12];
SessionOptions.SshHostKeyFingerprint = InterfaceValues[10];
}
try
{
MySession.Open(SessionOptions);
foreach (RemoteFileInfo info in MySession.EnumerateRemoteFiles("/", "*", EnumerationOptions.AllDirectories))
{
if (info.IsDirectory)
tvRemoteDirectory.Nodes.Add(info.Name);
}
MySession.Close();
}
catch (Exception ex)
{
MySession.Close();
MessageBox.Show("Not possible to connect to " + InterfaceValues[0] + "\nError Message: " + ex.Message);
this.Close();
}
The exception that I'm getting is:
{WinSCP.SessionRemoteException: Error listing directory '/jpm_icl'. ---> WinSCP.SessionRemoteException: Permission denied.
Error code: 3
Error message from server: Permission Denied!
Any idea what could I do at this point?
What I did was this:
ListDirectory function to retrieve all the directories, as I don't want the directory "." and "." I have to exclude it.
RemoteDirectoryInfo RemoteDirectory;
if (RemoteDirectoryPath != "Home")
RemoteDirectory = MySession.ListDirectory(RemoteDirectoryPath);
else
RemoteDirectory = MySession.ListDirectory("/");
if (tvRemoteDirectory.SelectedNode.Nodes.Count > 0) tvRemoteDirectory.SelectedNode.Nodes.Clear();
foreach (RemoteFileInfo fileinfo in RemoteDirectory.Files)
{
if (fileinfo.IsDirectory)
{
if (fileinfo.Name != "." &&
fileinfo.Name != "..")
{
TreeNode ChildNode = new TreeNode();
ChildNode.Text = fileinfo.Name;
ChildNode.ImageIndex = 0;
tvRemoteDirectory.SelectedNode.Nodes.Add(ChildNode);
tvRemoteDirectory.ExpandAll();
}
}
}

C# - Outlook - get access to a new calendar

I am trying to read all the calendars I have in my outlook with C#, but i have a problem getting access to the ones I create inside outlook (right click -> new calendar).
I'm trying to get them by:
Outlook.Application app = new Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
Outlook.MAPIFolder folderss = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
or by:
Application.Session.Stores
but none of them holds my new calendar.
do you have an Idea how to reach them?
Calendars are just Folders with DefaultItemType OlItemType.olAppointmentItem.
They can be created in any of the Outlook Stores on any level of the Folder hierarchy.
Assuming that the calendar was created in the root folder of one of your Stores, the following C# code will find it:
void findMyCalendar(String name)
{
string path = null;
Outlook.Application app = new Outlook.Application();
Outlook.NameSpace ns = app.GetNamespace("MAPI");
// there may be more than one Store
// each .ost and .pst file is a Store
Outlook.Folders folders = ns.Folders;
foreach (Outlook.Folder folder in folders)
{
Outlook.MAPIFolder root = folder;
path = findCalendar(root, name);
if (path != null)
{
break;
}
}
MessageBox.Show(path ?? "not found!");
}
// non-recursive search for just one level
public string findCalendar(MAPIFolder root, string name)
{
string path = null;
foreach (Outlook.MAPIFolder folder in root.Folders)
{
if (folder.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) &&
(folder.DefaultItemType == OlItemType.olAppointmentItem))
{
path = folder.FolderPath;
break;
}
}
return path;
}

Download Email Attachments from Outlook 2007

I am new 2 C# and i have been given a task...
I have to write a C# code to download the email attachments from outlook 2007 to a local drive or any specified location.The program should be in such a way that, given any username and password it should connect to that particular users outlook and download the files specified from a particular from address or subject line.
Any kind of help is appreciated.
So you are using outlook in an Exchange 2007/2010 environment? If yes you cold take a look at EWS.
Go through the following piece of code. It should work!
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
Microsoft.Office.Interop.Outlook.PostItem item = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
//Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
try
{
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
ns.Logon(null,null,false, false);
inboxFolder = ns.GetDefaultFolder (Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
//subFolder = inboxFolder.Folders["MySubFolderName"];
//folder.Folders[1]; also works
//Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
//Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());
for (int i = 1; i <= inboxFolder.Items.Count; i++)
{
item = (Microsoft.Office.Interop.Outlook.PostItem)inboxFolder.Items[i];
foreach (Microsoft.Office.Interop.Outlook.Attachments attachment in item.Attachments)
{
// Process the "attachment" object as per your requirement!
}
//Console.WriteLine("Item: {0}", i.ToString());
//Console.WriteLine("Subject: {0}", item.Subject);
//Console.WriteLine("Sent: {0} {1}", item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
//Console.WriteLine("Categories: {0}", item.Categories);
//Console.WriteLine("Body: {0}", item.Body);
//Console.WriteLine("HTMLBody: {0}", item.HTMLBody);
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
ns = null;
app = null;
inboxFolder = null;
}

Categories