Showing posts with label Outlook. Show all posts
Showing posts with label Outlook. Show all posts

April 14, 2022

How to Publish Outlook Add-in using Visual Studio Code?

Introduction

In this blog, we will see how to publish outlook add-ins using Visual Studio Code on the Azure Storage account.

An Office Add-in consists of a web application and a manifest file. The web application defines the Add-in's user interface and functionality, while the manifest specifies the location of the web application and defines the settings and capabilities of the Add-in.

While we are developing Add-in, we can run the add-in on our local web server (localhost). But what if we need to publish it? We can publish Outlook Plugin directly through Visual Studio Code using the Azure Storage Extension.

So, Let's get started and explore!

Steps to publish Outlook Add-in:

Note: These steps work only for projects created with Yeoman Generator.

Step 1: Open project in Visual Studio Code.

Step 2: Open the Extensions view in VS Code, search for the Azure Storage Extension and install it.


Step 3: Once installed, an Azure icon is added to the Activity Bar. Select it to access the extension.

Step 4: Sign into your Azure account by selecting Sign in to Azure.

Step 5: Once you have signed into your Azure account, you'll see your Azure storage accounts appear                 in the extension.



Step 6: Select and hold (right-click) your storage account, Choose Configure Static Website


 Step 7:  Once we select Configure Static Website, new popup will open inside the Visual Studio, enter the default document name. By default it's index.html, we can change and set it to taskpane.html and press enter key. Then, it will ask for 404 error document you can skip as it’s optional.


Step 8: Select and hold (right-click) your storage again and choose Browse static website. New browser window will open from that copy the website URL.

Step 9: Open your manifest.xml file from the project and then change all the references which point to your localhost (https://localhost:3000) to the URL you've copied.

Step 10: Open your Visual Studio Code terminal and run the following command. 

 npm run build  

When the build completes, the "dist" folder will be created in the root directory

Step 11: Then press right-click on the dis folder and select Deploy to Static Website.



Step 12: Once deployed successfully, "Browse to website" message appears from which you can select and open your app.



Conclusion

This is how we can publish outlook add-ins in the Azure Storage Account using Visual Studio Code.

If you have any questions you can reach out our SharePoint Consulting team here.

April 8, 2021

Access Events from Shared Calendar Using Exchange Web Service and Store Events Data to SharePoint List with C# Console Application

Introduction:

We implemented the intranet solution for a consulting firm based out of Alpharetta, GA, USA. One of the requirements was to store events from Shared Outlook Calendar (Office 365) to SharePoint List for easier accessibility for the users. In this blog, we will learn how to get events from a Shared Outlook Calendar using Microsoft Exchange web service and store in SharePoint Online List with CSOM using C# console application.

So, now Let’s get started with the procedure to build the custom console application.

Step 1: Create Solution

  1.  Open Visual Studio 2019, Click on Create New Project and then Choose Console App (.NET Framework).
  2.  Give it a meaningful Project Name and in Framework select .NET Framework 4 and click "Create".


Step 2: Establish Connectivity with Microsoft Exchange Webservice

  1. Install "Microsoft.Exchange.WebServices" NuGet package.
  2. Add namespace “using Microsoft.Exchange.WebServices.Data;”
  3. We will be using Exchange Service class to connect the Shared Calendar.
  4. We can use the below piece of code to create the instance of ExchangeService and define the credentials & endpoint URL.
     ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);  
     service.Credentials = new WebCredentials("userEmailAddress", "userPassword");  
     service.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");  
    

Step 3: Get Calendar Events

  1. The calendar can contain some folder, with the use of the "FolderView" class we can retrieve the same. Constructor FolderView(100) Initializes a new instance of the FolderView class with the maximum number of returned folders specified.
     FolderView view = new FolderView(100);
    

  2. Initialize a new instance of the FolderId class with the specified folder name and mailbox.
     FolderId folderToAccess = new FolderId(WellKnownFolderName.Calendar, "shared Email");  
     FolderId(WellKnownFolderName, Mailbox)  
    Use this constructor to link this folder ID to a well-known folder (for example, Inbox, Calendar or Contacts) in a specific mailbox.

  3. FindFoldersResults Represents the results of a folder search operation.
     FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.Root, view);  
    
    FindFolders Obtains a list of folders by searching the subfolders of the specified folder.

  4. CalendarFolder Represents a folder that contains appointments.
     var calendar2 = CalendarFolder.Bind(service,folderToAccess);  
    
    CalendarFolder.Bind(service,folderToAccess) binds to an existing calendar folder and loads its first-class properties. Calling this method results in a call to Exchange Web Services (EWS).

  5. Define a date range view of appointments in the calendar folder search operations.
     CalendarView cv = new CalendarView(StartDate, EndDate);  

  6. FindAppointments obtains a list of appointments by searching the contents of a specified folder.
     FindItemsResults<Appointment> fapts = service.FindAppointments(folderToAccess, cv);  
    
    FindItemsResults<Appointment> a collection of appointments that represents the contents of the specified folder.

    Now with the use of appointment class property, we can retrieve Event Subject, Organizer, and other property.

  7. Here, we will get the Event Title and Organizer.

Step 4: Store in SharePoint List.

  1. Once we get data, we can store it in SharePoint List with CSOM. We will add the Event subject and Organizer Name, for that we create one Single Line of Text and one Person column in a SharePoint List.
  2. Add namespace “using Microsoft.SharePoint.Client;” and “using System.Security;”.
  3. Represents the context for SharePoint objects and operations.
     ClientContext clientContext = new ClientContext(siteUrl);  
    

  4. Give Email and Password, Gets or sets the authentication information for the client context
     clientContext.Credentials = new SharePointOnlineCredentials("username", "password");  
    

  5. Connect List.
     List appointmentsList = clientContext.Web.Lists.GetByTitle("Appointments");  
    

  6. ListItemCreationInformation specifies the properties of the new list item.
     ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();  
    
    ListItem represents an item or row in a list.

  7. List.AddItem method returns a ListItem instance representing the creation of a new list item in the list.
     ListItem newItem = appointmentsList.AddItem(itemCreateInfo);  
    

  8.  As we know to add items in Person columns into the list, we need to provide Id, below code, will get Id of the organizer.
     var demo = Appoint.Organizer;  
     string testName = demo.ToString();  
     var demo2 = testName.Substring(0, testName.IndexOf("<") - 1);  
     User userTest = clientContext.Web.EnsureUser(demo2);  
     clientContext.Load(userTest);  
     clientContext.ExecuteQuery();  
    

  9. Now, the final step to insert a record into the SharePoint list.
     newItem["Title"] = Appoint.Subject;  
     newItem["Users"] = userTest.Id;  
     newItem.Update();  
     clientContext.ExecuteQuery();  
    

Complete Code

 using System;  
 using Microsoft.Exchange.WebServices.Data;  
 using Microsoft.SharePoint.Client;  
 using System.Security;  
 using Folder = Microsoft.Exchange.WebServices.Data.Folder;  
 namespace SharedCalender  
 {  
 class Program  
 {  
 static void Main(string[] args)  
 {  
 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);  
 service.Credentials = new WebCredentials("userEmailAddress", "userPassword");  
 service.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");  
 FolderView view = new FolderView(100);  
 FolderId folderToAccess = new FolderId(WellKnownFolderName.Calendar, "shared Email");  
 FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.Root, view);  
 foreach (Folder f in findFolderResults)  
 {  
 var calendar2 = CalendarFolder.Bind(service,folderToAccess);  
 DateTime StartDate = DateTime.Today.AddMonths(-1);  
 DateTime EndDate = DateTime.Today.AddMonths(1);  
 CalendarView cv = new CalendarView(StartDate, EndDate);  
 FindItemsResults<Appointment> fapts = service.FindAppointments(folderToAccess, cv);  
 if (fapts.Items.Count > 0)  
 {  
 foreach (Appointment Appoint in fapts)  
 {  
 string siteUrl = "https://constoso.sharepoint.com/sites/SiteName";  
 ClientContext clientContext = new ClientContext(siteUrl);  
 SecureString passWord = new SecureString();  
 foreach (char c in "Password".ToCharArray()) passWord.AppendChar(c);  
 clientContext.Credentials = new SharePointOnlineCredentials("userEmailAddress", passWord);  
 List appointmentsList = clientContext.Web.Lists.GetByTitle("TestList");  
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); ListItem newItem = appointmentsList.AddItem(itemCreateInfo);
//get user id var demo = Appoint.Organizer; string testName = demo.ToString(); var demo2 = testName.Substring(0, testName.IndexOf("<") - 1); User userTest = clientContext.Web.EnsureUser(demo2); clientContext.Load(userTest); clientContext.ExecuteQuery(); newItem["Title"] = Appoint.Subject; newItem["Users"] = userTest.Id; newItem.Update(); clientContext.ExecuteQuery() } } Console.ReadLine(); } } } }

Conclusion:

This is how we can access Events from Shared Calendar using Exchange web service and store data to SharePoint list.

If you have any questions you can reach out our SharePoint Consulting team here.

November 2, 2009

Programmatically configure custom contact list value to Outlook 2007 using Web Part

It is inbuilt functionality in SharePoint ‘Action’ menu ‘Connect to outlook’. This functionality used stssync protocol and JavaScript function ‘ExportHailStorm’.


Many times you need some customized functionality like Connect SharePoint contact list to outlook 2007.

I searched many sites but none helped me to find the perfect solution but the show me the way to achieve this.

Here is code for that how to modify inbuilt ‘Connect to outlook’ functionality in under webpart.

To implement this, write function in Creatchildcontrol method in web part.

//code here

Protected override void Creatchildcontrol ()

{

OutlookButton.Attributes.Add("onClick", "javaScript:OutlookContactSync('Contacts'

,'" + SPContext.Current.Web.Url + "', '/Lists/GeneralContact/'

, '" + SPContext.Current.Web.Lists["GeneralContact"].ID + "'

, 'GeneralContact'

, '" + SPContext.Current.Web.Title + "');");

}

Protected override void Render (HtmlTextWriter writer)

{

OutlookButton.RenderControl (writer);

writer. Write (javascriptpath);

}



Add the following JavaScript function that will be used to call the method you mentioned above.

//JavaScript function

function OutlookContactSync (type, baseUrl, listUrl, listId, listName, siteName)

{

var outlookURL = encodeURI ('stssync://sts/?ver=1.1&type='+type+'&cmd=add-folder&base-url='+baseUrl+'&list-url='+listUrl+'&guid={'+listId+'}&site-name='+siteName+'&list-name='+listName+'');

Var objWindow=window.open (outlookURL,'','width=0, height=0');

objWindow.close ();

}

If you have any questions you can reach out our SharePoint Consulting team here.