Posts

Showing posts with the label C#

How to make sure only one instance is running

Some application cannot be run in multiple instance for several reasons. So you need to avoid a second instance will run, but how? You cannot directly block the execution of your application, so you need to take care of the problem in your code.

Excel column letters into number

Sometimes you need to loop cells in Excel. In fact it is looping rows and columns, because cells have two dimensions. The rows are counted up, but the columns are named alphabetic. Looping them is not that easy and gets harder until you hit the Z , because the next 'letter' would be AA . So a number would be more easy.

is the DateTime empty?

Have you ever tryied to check a DateTime for the state "not set"? Some of you would say "this is easy, I will check the variable for null" like this: 1 2 3 4 if (myDateTime == null ) { // it is empty } The funny fact is: this will never work. But why?

How to support multiple languages in WPF

Image
If you are developing many solutions for many costomer, one day one of them will require a multilingual WPF-solution. So there is the question: how to make a WPF form multilingual? At first you need at least two languages that you want to support. You can add some other languages at the end very easy. First of all create a folder in your project and call it "Languages", to keep an overview. Once it is done you can create Resource Dictionaries in this folder. You can call them however you want, but make sure you can assign them to the language. You can use language codes (like "en-us") or you can call it like the language is called. At the end it can look like this:

Get the mail address from a FieldUserValue Client Side vs Server Side

In SharePoint are two ways to code by using an object model. The one way is the Client-Side Object Modell (CSOM) and the other side is the Server-Side Object Model (SSOM). At the first look they both look the same the only difference is how the objects are looking for example the is the 'Site'-object (client side) is called 'SPSite' on server side. A good way to demonstrate the difference is to get the mailaddress of a userfield. That sounds easy, but needs more lines that I thought at first. So let's say you got a SharePoint 2013 and want to read the mailaddress on client side. That would look like this: 1 2 3 4 5 6 7 8 9 // get the userfield of the item e.q. Author FieldUserValue Author = (FieldUserValue)currentItem[ "Author" ]; // ask the web to find a user with the id in the field (only works if the user visited the site) var user = web.SiteUsers.GetById(Author.LookupId); // load the data with the context context.Load(user); context.ExecuteQue...

Get data of all users on the SharePoint with CSOM C#

Sometimes you need to get information abot some user. To do so you need the SiteUserInfoList. This list contains data about all user that visited the SharePoint site. This is how you can read the list with C# and CSOM: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // build the SharePoint connection ClientContext clientContext = new ClientContext( "http://your site/" ); Web web = clientContext.Web; // get the userinfolist List siteUserInfoList = web.SiteUserInfoList; // build a query CamlQuery query = new CamlQuery(); query.ViewXml = "" ; // get the data IEnumerable<ListItem> itemColl = clientContext.LoadQuery(siteUserInfoList.GetItems(query)); clientContext.ExecuteQuery(); // work with the userinformations foreach ( var item in itemColl) { Console.WriteLine( "ID:{0} Email:{1} Title:{2}" , item.Id, item[ "EMail" ], item[ "Title" ]); }

Read an Exchange inbox with WebServices and C#

In some situations you will need to read mails from mailserver. One of those servers is the Exchange. Reading it is pretty easy with the WebServices. At first you need to add the reference to Microsoft.Exchange.WebServices. After that you can begin to code. All you need are two functions. One to build a connection to the Exchange and the other one to find folder and read them.

Fix the MailItem.Send() warning

Sometimes you need to create and send mails in your code. There are many ways to solve this requirement. You can connect to your Exchange with WebServices or you can use a WebService to connect to a SMTP server. But what if you are not able to connect to them, but you know the client is using Outlook. You can call Oulook to send your mails. That is pretty easy all you need is the interop namespace of Outlook (using Oulook = Micosoft.Office.Interop.Outlook;). The code you need for this should contain lines of the following snippet: 1 2 3 4 5 6 // ..call Outlook .. // create the mailitem Outlook.MailItem mItem = outlookapplication.CreateItem(Outlook.OlItemType.olMailItem); // ..set subject, body recipient and much more.. // send the mail mItem.Send();

How the ClientContext of SharePoint CSOM works

If you are writing clientsite applications for SharePoint you can use for example the Client Site Object Modell (CSOM). In the CSOM you will need to use the ClientContext to communicate with the SharePoint, but how does it work? Using the ClientContext is like going to the supermarket.First of all you are looking out to which supermarket you will go. Then you figure out what you need and write it down to a shopping list. After that you are going to the supermarket and get all the stuff you need. So first of all lets get the SharePoint (supermarket) we want to get our stuff from. ClientContext ctxSupermarket = new ClientContext( "http://mySharePointURL/mySupermarket/" );

DragAndDrop in Outlook - is it a mail, an attachment or a file?

If you are developing an addin for outlook and you are building a drag and drop able control, you will need to check what kind of data droped in. In the following code I made differences between those three types: files from the filesystem, mails out of Outlook and attachments that came from mails in Outlook. I used the DragEventArgs to figure out what was droped and then I handle each way a bit different.

Refreshing the UI in WPF

If you are using WPF and missing the 'Application.DoEvents();' method, you know from Windows Forms to refresh the UI, here is a solution for you. I had the same problem and was looking for a way to refresh the UI in runtime.

XML Manager - read and write XML-files

If you want to use XML structures in your project you need to read and create XML files. For this I got a class called XMLManager. The only thing you need for this is a shema of the XML structur you want to read or create.

Create CustomTaskPanes in Office AddIns with C#

Image
To create you own workspace in an Office application like Outlook you need a CustomTaskPane . To call a CustomTaskPane you need to create at first a UserControl . In that UserControl you can build your own layout. After that you need to create a class called TaskPanes. This class contains two functions, one to create a CustomTaskPane and one to remove it. In the create-function you are creating an object of your UserControl and a CustomTaskPane-object. By creating the CustomTaskPane you are giving the UserControl and a headline over.

convert HTML to PDF in C#

To convert a HTML file into a PDF file in C# is not as hard as it sounds. All you need is a usefull library, and there are many. For example you can use iTextSharp. This one is for free until version 4.1.6 and you can get it here .