Point Deep

Mundeep's Tech Blog

Posts Tagged ‘.NET’

c# if else alternative syntax using ? : operator

Posted by mundeep on July 15, 2011

One of the c# operators i tend to use a lot is the ?: operator which is essentially an alternative syntax to an if else statement. Unfortunately i often forget the order of the statements so am adding this quick post as a note for myself to easily remember 🙂


condition ? first_expression : second_expression;

Is equivalent to:

if (condition) {
else {



Posted in .NET | Tagged: , , | 2 Comments »

Updating the Created Date of a Document or List Item Programmatically

Posted by mundeep on May 22, 2009

A common tasks developers are required to do with Sharepoint is migrated documents from either legacy systems or older Sharepoint sites into a document library. There are many ways to do this documented out there (most commonly using the Files.Add method of the API), but one of the common requirements during this ‘migration’ is to retain the Created Date or Created By fields. Sowmyan’s blog has a good description of how to set the Created By/Modified By user fields here.

Updating the Created Date of a document or list item in Sharepoint is even easier, simply set the “Created” field of the item to the value you wish to set. For example if using the Files.Add methods in the API (as described by Dave Hunter) then the following code snippet will update the Created Date:

// add the file   
SPFile file = docLib.RootFolder.Files.Add(newFileName, inputStream);   
// get the list item for that file   
SPItem item = file.Item;   
//Set the Created Date
item["Created"] = "2009-02-26 15:00:00";

Posted in .NET, Sharepoint | Tagged: , , , | Leave a Comment »

InfoPath 2007 Custom Field Validation (Programmatic)

Posted by mundeep on May 20, 2009

Adding custom (programmatic) field validation to a form in InfoPath 2007 is quite easy. Firstly make sure you have set your programming language of choice by going to the Tools menu then “Form Options” -> “Programming”, and then selecting your preferred language under “Form template code language”. For this example i am going to be using C#.

Back in your form design window right-click on the field you wish to validate, and select “Programming” -> “Validating Event”. This will open a “Visual Studio Tools for Applications” (VSTA) window, and generate the following code snippet:

        public void myFieldName_Validating(object sender, XmlValidatingEventArgs e)
            // Write your code here.

Obviously this code is not doing any validation yet, we just need to insert our validation code in place of the “your code here” comment. Let us try a simple validation of ensure the data entered in the field is at least 3 characters long with the following code.

public void myFieldName_Validating(object sender, XmlValidatingEventArgs e) {
//Check the Length of the NewValue of the field
if (e.NewValue.Length < 3) { //Report the validation error back to the client on the form e.ReportError(e.Site, false, "Please Enter a valid MyField"); } } [/sourcecode] As mentioned in the comments e.NewValue retrieves the 'New' value that is being entered/inserted into the field before it has been set and e.ReportError sends any validation errors back to the form for the client (read the MSDN article for details on the parameters of ReportError).

Unfortunately there is a minor issue with this basic piece of code and that is due to the fact that the validating event also gets fired during the loading of the form and as shown in this kb article we need to check the XmlOperation that is being performed when the validation is fired. This means updating our code to the following.

public void myFieldName_Validating(object sender, XmlValidatingEventArgs e) {
//Ensure validation is only done when inserting or changing a value
if (e.Operation == XmlOperation.Insert || e.Operation == XmlOperation.ValueChange) {
//Check the Length of the NewValue of the field
if (e.NewValue.Length < 3) { //Report the validation error back to the client on the form e.ReportError(e.Site, false, "Please Enter a valid MyField"); } } } [/sourcecode] Save your code changes and while still in VSTA build the project to ensure you don't have any coding errors (Ctrl+Shift+B, or from the menu "Build" -> "Build 'MyFormName'"). If you get a "Build succeeded" message in the status bar you can close the VSTA window and then preview your form to see your validation in action.

Posted in .NET, Office | Tagged: , , , , | 1 Comment »

Retrieve location of Temp folder for Current User

Posted by mundeep on March 12, 2008

To determine the location of the Temp folder of the current user (generally for writing temporary files) use the following bit of c#:

string tempFolder = System.IO.Path.GetTempPath();

Posted in .NET | Tagged: , , | Leave a Comment »

Remove Non-Alphanumeric Characters from a String

Posted by mundeep on March 7, 2008

A colleague was looking for an easy way to remove all non-alphanumeric characters from a string and it took some time to find the easiest way was to use RegEx.Replace() as follows:

Regex.Replace(stringToCleanUp, @"[\W]", "");

while \w (lowercase) matches any ‘word’ character, equivalent to [a-zA-Z0-9_]
\W matches any ‘non-word’ character, ie. anything NOT matched by \w

As an alternative if you don’t want to allow the underscore you can use [^a-zA-Z0-9].

The following regular expression quick reference helped in finding this solution:
Regular Expressions Quick Reference

Posted in .NET | Tagged: , , | 16 Comments »