Friday, August 16, 2013

Convert web addresses contained in a string to hyperlinks using a regular expression

In the event you ever need to convert web addresses contained within a string to hyperlinks, I stumbled across some code on StackOverflow that will allow you to do this via a regular expression.  The code for this solution is presented as follows:


private string ConvertUrlsToLinks(string msg) 
{
    string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
    Regex r = new Regex(regex, RegexOptions.IgnoreCase);
    return r.Replace(msg, "$1").Replace("href=\"www", "href=\"http://www");
}

Special thanks goes to a user by the name of "Rob" on StackOverflow for providing this solution to the problem!

Tuesday, August 13, 2013

Using C# to read data from a SharePoint list using the SharePoint REST API

If you're working with a C# application that is required to read information contained in a SharePoint list located on an external SharePoint farm, the SharePoint REST API can provide just the solution that you're looking for.  Here is some sample code that you can use for accessing the information contained in that SharePoint list:

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web.Script.Serialization;

namespace Sample
{
    public class SharePointListReader
    {
        ...

        public List<SharePointListItem> GetAllSPListItems()
        {
            List<SharePointListItem> posts = new List<SharePointListItem>();
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://webapp/site/_api/web/lists/getbytitle('listName')/items?$select=id,Title");
            request.Method = "GET";
            request.Accept = "application/json;odata=verbose";
            request.ContentType = "application/json;odata=verbose";
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;
            WebResponse response = request.GetResponse();
            Data data = null;

            // Read the returned posts into an object that can be consumed by the calling application
            using (response)
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    try
                    {
                        string jSON = reader.ReadToEnd();
                        data = serializer.Deserialize(jSON);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("An error occurred when reading the list items from SharePoint: {0}; {1}", ex.Message, ex.StackTrace));
                    }
                }
            }
            foreach (SharePointListItem post in data.d.results)
            {
                posts.Add(post);
            }
            return posts;
        }
    }

    public class Data
    {
        public Results d { get; set; }
    }

    public class Results
    {
        public SharePointListItem[] results { get; set; }
    }

    public class SharePointListItem
    {
        public string id { get; set; }
        public string Title { get; set; }
    }
}

Wednesday, August 7, 2013

Visual Studio hotkey for formatting XML: Ctrl+k+f

There will inevitably come a time when you're going to obtain some XML output that you need to review that would, in all honesty, be impossible to read without proper formatting.  A good example would be this response from the the YouTube API that provides a list of videos that a specified user has uploaded (i.e. which would appear as follows in Notepad):




In many cases, simply copying and pasting the XML it into Visual Studio might work, but, in the off chance it doesn't, you might get something like this.




If that happens, simply hit Ctrl+k+f and the XML will be displayed in an easy-to-read format:



Tuesday, August 6, 2013

Calculating the number of days since an event took place using Javascript

If you're looking for a way to determine how many days have passed since a given event took place using Javascript, here is a function that will provide you with the basic functionality that you're looking for:

function calculateDaysSinceEvent(eventDateTime) {
    // Obtain the current date and time
    var todaysDateTime = new Date();
    // Calculate the difference in time between today and when the event took place
    var diff = todaysDateTime - eventDateTime;
    // Convert the difference to a value that represents a day and round the value up
    return Math.round(diff/(1000*60*60*24));
}