Get Calendar Items in Exchange - c#

I have a little problem with my Sharepoint 2 Exchange Tool. Maybe sb from you can help me here :)
using (ClientContext clientContext = new ClientContext(m_Office365URL))
{
SecureString passWord = new SecureString();
foreach (char c in m_password.ToCharArray()) passWord.AppendChar(c);
SharePointOnlineCredentials xCred = new SharePointOnlineCredentials(m_userName, passWord);
clientContext.Credentials = xCred;
Web xWeb = clientContext.Web;
clientContext.Load(xWeb);
clientContext.Load(clientContext.Site.RootWeb);
clientContext.ExecuteQuery();
DateTime calDate = startDate;
List targetList = xWeb.Lists.GetByTitle(m_TargetListName);
clientContext.Load(targetList);
clientContext.ExecuteQuery();
This is fully working an haven't any error inside.
Now my thing is to get this as similar as possible working for Exchange.
Here's what I have until now:
ExchangeService m_Service = new ExchangeService(m_Url);
SecureString passWord = new SecureString();
foreach (char c in m_Password.ToCharArray()) passWord.AppendChar(c);
m_Service.Credentials = new WebCredentials(m_UserName, m_Password);
foreach (Appointment appointment in m_Service.FindItems(WellKnownFolderName.Calendar, new ItemView(int.MaxValue)))
{
DateTime calDate = startDate;
ExchangeService xWeb = m_Service;
m_Service.UpdateItems(xWeb);
m_Service.Load(m_Service.Site.RootWeb);
m_Service.ExecuteQuery();
List targetList = xWeb.Lists.GetByTitle(m_TargetListName);
m_Service.Load(targetList);
m_Service.ExecuteQuery();
But As you can guess I get Poblems startin at "ExchangeService xWeb = new m_Service"
For sure the m_Service.UpdateItems, m_Service.Load etc won't work either.
If somebody might have a hint for me that'd be great.

I was able to run this code using:
System.Net.ServicePointManager.ServerCertificateValidationCallback = Ise_ExchangeInterface.CertificateValidationCallBack;
m_Service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
m_Service.Credentials = new WebCredentials(m_UserName, m_Password);
m_Service.AutodiscoverUrl(m_UserName, Ise_ExchangeInterface.RedirectionUrlValidationCallback);

Related

Can not upload file to provided Azure containers

I try upload file to provided Azure containers which it is taken from ProvisionMigrationContainers() method, however it was not successful.
I am using below C# code:
ClientContext clientContext = new ClientContext(urlSring);
var securePassword = new SecureString();
foreach (var c in passWord) securePassword.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
ClientResult<ProvisionedMigrationContainersInfo> containerInfo = clientContext.Site.ProvisionMigrationContainers();
clientContext.ExecuteQuery();
var containerInfoList = containerInfo.Value;
var dataContainerUri = containerInfoList.DataContainerUri;
var metadataContainerUri = containerInfoList.MetadataContainerUri;
CloudBlobContainer dataContainer = new CloudBlobContainer(new Uri(dataContainerUri));
CloudBlobContainer manifestContainer = new CloudBlobContainer(new Uri(metadataContainerUri));
//Upload data to container start
var testfiles = new[]
{
new SourceFile
{
Filename = "test.txt",
LastModified = DateTime.Now,
Contents = Encoding.UTF8.GetBytes("Hi, this is a test text-file"),
Title = "Title of file 1"
},
new SourceFile
{
Filename = "test2.txt",
LastModified = DateTime.Now.AddDays(-1),
Contents = Encoding.UTF8.GetBytes("Tesfile2"),
Title = "Second title"
}
};
foreach (var testfile in testfiles)
{
var blobReference = dataContainer.GetBlockBlobReference(testfile.Filename);
blobReference.UploadFromByteArray(testfile.Contents, 0, testfile.Contents.Length);
}
When i get list blob from provided Azure containers with below code, it returns null.
var blobList = dataContainer.ListBlobs();
var fileList = blobList.OfType<CloudBlockBlob>().Select(x => x.Name).ToList();
Would you please tell me the reason?
Thanks in advance
RonLee.

Add item to SharePoint Online list c#

I'm having a web api which is having some data in its body and that data i want to add to SharePoint online list using c#. But below code is giving me unauth error.
using (var context = new ClientContext(siteUrl))
{
context.ExecutingWebRequest += Context_ExecutingWebRequest; // for oAuth it working in get list dat
Web web = context.Web;
List topicsList = context.Web.Lists.GetByTitle("ListName");
ListItemCreationInformation newTopicInfo = new ListItemCreationInformation();
ListItem oListItem = topicsList.AddItem(newTopicInfo);
oListItem["Title"] = "Test";
oListItem["Column1"] = "Test1";
oListItem.Update();
context.ExecuteQuery();
}
The above code is working perfectly fine now, i was missing permissions to my app. For using oAuth we have to register an Add-in in SharePoint, and to post data in sharepoint i have to give FullControl to my add-in, but while creation i have made it read-only.
Below is the referance.
Referance
For SharePoint Online, use SharePointOnlinCredentials class pass credentials to authencation:
string password = "*******";
string account = "username#tenant.onmicrosoft.com";
var secret = new SecureString();
foreach (char c in password)
{
secret.AppendChar(c);
}
using (ClientContext ctx = new ClientContext("https://tenant.sharepoint.com/sites/dev/"))
{
ctx.Credentials = new SharePointOnlineCredentials(account, secret);
ctx.Load(ctx.Web);
ctx.ExecuteQuery();
List topicsList = ctx.Web.Lists.GetByTitle("ListName");
ListItemCreationInformation oListItemCreationInformation = new ListItemCreationInformation();
ListItem oListItem = topicsList.AddItem(oListItemCreationInformation);
oListItem ["Title"] = "New List Item";
oListItem["Column1"] = "Test1";
oListItem .Update();
ctx.ExecuteQuery();
};

Deleting SharePoint calendar links In Exchange via EWS

Does anyone have examples of how to delete a user's SharePoint calendar link in Outlook/Exchange via EWS Managed API? I am able to successfully find the Calendar link item, however the item.Delete(DeleteMode.HardDelete) executes without actually removing the calendar link. Any help or guidance would be appreciated.
Edited to include code:
//Will target a specific user mailbox with parameter mbMailboxname
static Dictionary<string, Folder> GetSharedCalendarFolders(ExchangeService service, String mbMailboxname)
{
Dictionary<String, Folder> rtList = new System.Collections.Generic.Dictionary<string, Folder>();
DateTime startDate = new DateTime(2018, 8, 1);
DateTime endDate = new DateTime(2018, 8, 31);
CalendarView calView = new CalendarView(startDate, endDate);
Mailbox mb = new Mailbox(mbMailboxname);
FolderId rfRootFolderid = new FolderId(WellKnownFolderName.Root, mb);
FolderView fvFolderView = new FolderView(1000);
SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Common Views");
FindFoldersResults ffoldres = service.FindFolders(rfRootFolderid, sfSearchFilter, fvFolderView);
if (ffoldres.Folders.Count == 1)
{
PropertySet psPropset = new PropertySet(BasePropertySet.FirstClassProperties);
ExtendedPropertyDefinition PidTagWlinkAddressBookEID = new ExtendedPropertyDefinition(0x6854, MapiPropertyType.Binary);
ExtendedPropertyDefinition PidTagWlinkGroupName = new ExtendedPropertyDefinition(0x6851, MapiPropertyType.String);
psPropset.Add(PidTagWlinkAddressBookEID);
ItemView iv = new ItemView(1000);
iv.PropertySet = psPropset;
iv.Traversal = ItemTraversal.Associated;
SearchFilter cntSearch = new SearchFilter.IsEqualTo(PidTagWlinkGroupName, "Other Calendars");
FindItemsResults<Item> fiResults = ffoldres.Folders[0].FindItems(cntSearch, iv);
foreach (Item itItem in fiResults.Items)
{
Microsoft.Exchange.WebServices.Data.Item foundItem = itItem;
if (foundItem.Subject.ToString().Trim().Contains("Company Group Calendar"))
{
Console.WriteLine("Deleting calendar..");
//Executing the delete command here does not delete the calendar
foundItem.Delete(DeleteMode.MoveToDeletedItems);
}
}
}
return rtList;
}

Is it possible to remotely read data from Sharepoint on prem server

Reading userprofile data from Sharepoint online is straightforward with CSOM. For example:
string siteURL = #"https://foo.sharepoint.com";
ClientContext ctx = new ClientContext(siteURL);
var securePass = new SecureString();
"password123".ToList().ForEach(s => securePass.AppendChar(s));
ctx.Credentials = new SharePointOnlineCredentials("userName", securePass);
var peopleManager = new PeopleManager(ctx);
var personProperties = peopleManager.GetMyProperties();
ctx.Load(personProperties, p => p.UserProfileProperties);
ctx.ExecuteQuery();
Is this possible to do to a Sharepoint 2013 on premise using WebClient or something similar?

How to get site's URL using ServerManager object

I am trying to get site's URL here but not able to figure out how to get it,
using (var mgr = new ServerManager())
{
foreach (var site in mgr.Sites)
{
var siteURL = site. ??
Here's class I am using
http://msdn.microsoft.com/en-us/library/microsoft.web.administration.application.virtualdirectories(v=vs.90).aspx
Umm..I guess this might help..
ServerManager serverMgr = new ServerManager();
Site site = serverMgr.Sites["YourSiteName"];
List<string[]> urls = new List<string[]>();
foreach (Binding binding in site.Bindings)
{
string bindingInfo = binding.BindingInformation;
string subString = bindingInfo.Substring(2, bindingInfo.Length - 2);
string[] adrs = subString.Split(':');
adrs[0] = "localhost:" + adrs[0];
urls.Add(adrs);
}
Sites is a Sitecollection and you can get sites by looping through its items.
If you are just looking for website's url why dont you use Request object.

Categories