A collection of TypeScript Definition files for SharePoint 2013 et al.

I’ve been doing a lot of work recently with TypeScript and have created a few definition files for stuff that I use often such as:

I’m planning to maintain these on GitHub as I add to them. Hopefully they’ll come in useful. You can find them here:  https://github.com/chaholl/TypeScriptDefinitions/

Posted in Development, JavaScript, JQuery, SharePoint 2013, TypeScript | 3 Comments

Quick tip: Auto copy SharePoint 2013 .Net 4 DLL to GAC with VS2012

When working on complex SharePoint projects I often haver a number of projects, some of which produce DLL’s that are deployed to the GAC when the completed solution is deployed to SharePoint. Since life is short, I don’t want to be redeploying the solution every time I update one of these DLL’s. Instead I just want to copy the DLL to the GAC and maybe recycle an app pool.

While we wait eagerly for the SP2013 version of CKSDev, here’s a quick post-build script that’ll do the job:

  1. Select a project then Properties > Build Events.
  2. In the Post-build event command line box add the following:
if "$(ConfigurationName)"=="Debug" (
"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\gacutil.exe" -i $(TargetPath)
"C:\Windows\System32\inetsrv\appcmd.exe" recycle apppool "19311de9d6b64338a982be8c9af45345"
)

Note: Change the app pool name to something appropriate to your project. If you’re DLL is being used in a front end process you probably want:

"C:\Windows\System32\inetsrv\appcmd.exe" recycle apppool "SharePoint – 80"

Now when you build your project in debug mode (i.e. on your dev box), you’ll see the following in the output window:

1>------ Build started: Project: MyCompany.MyWidget, Configuration: Debug Any CPU ------
1>  MyCompany.MyWidget -> C:\Code\SharePoint2013\MyCompany.MyWidget.dll
1>  Microsoft (R) .NET Global Assembly Cache Utility.  Version 4.0.30319.17929
1>  Copyright (c) Microsoft Corporation.  All rights reserved.
1>
1>  Assembly successfully added to the cache
1>  "19311de9d6b64338a982be8c9af45345" successfully recycled
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Posted in CKSDev, SharePoint 2013, Tip | 3 Comments

A circular reference was detected while serializing an object of type ‘System.Reflection.RuntimeModule’

When building web services that return JSON you’ll probably come up against a variant of this error when trying to serialize exceptions. (It also occurs with many other types but my solution only deals with exceptions so we’ll stick with that for now)

The reason for the problem, as the error suggests, is a circular reference in the object graph that prevents the serialization from completing. Thankfully this can be easily resolved by implementing a converter to return only the relevant parts of the exception (or any other object):

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web.Script.Serialization;

namespace MyCompany.MyWidget
{
    internal class ExceptionConverter : JavaScriptConverter
    {
        public override IEnumerable<Type> SupportedTypes
        {
            //Add a list of the type you want to convert here
            get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(Exception) })); }
        }

        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            if (dictionary == null) throw new ArgumentNullException("dictionary");

            //check that the passed object is of the correct type
            if (type == typeof(Exception))
            {
                //Since we've lost most of the data of the original exception
                //we'll deserialize to a plain Exception object
                var message = dictionary[@"Message"].ToString();
                return new Exception(message);
            }

            return null;
        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            var exception = obj as Exception;
            if (exception != null)
            {
                var result = new Dictionary<string, object>
                    {
                        //Make sure anything added here is serializable!
                        {@"Type", exception.GetType().ToString()},
                        {@"Message", exception.Message},
                        {@"Source", exception.Source}
                        //add whatever other properties you're interested in seeing on the client
                    };

                return result;
            }
            return new Dictionary<string, object>();
        }
    }
}

This code converts an exception into a dictionary of serializable objects and outputs the dictionary as JSON. You’d use this in your code like this:

public Stream MyWebServiceCall(Stream value)
{
    try
    {
        //Web service foo goes here
    }
    catch (Exception ex)
    {
        //It's all gone wrong. Send an exception to the caller

        var serializer = new JavaScriptSerializer();
        serializer.RegisterConverters(new JavaScriptConverter[] { new ExceptionConverter()});
        string json = serializer.Serialize(ex);
        return new MemoryStream(Encoding.UTF8.GetBytes(json));
    }
}

For all your effort, you’ll now see this in your JSON output: image

Fabulous eh?

Posted in Development, JavaScript, JQuery, WCF | 2 Comments

Quick Tip: SPRoot shortcut

How many times a day do you find yourself typing C:\Program Files\Common Files\Microsoft Shared \Web Server Extensions\<whatever>?

Save yourself some trouble – define an environment variable called SPROOT that point to the folder then you can use it everywhere. Here’s how to do it:

  1. Open a command prompt.
  2. Enter the command:
setx SPROOT "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\

Hit return, close the command prompt and your done. (Note: The lack of closing quotation marks is intentional)

You can now navigate to the SharePoint hive by simply typing %SPROOT%. From the search box in the start menu it’ll show you the contents of the folder for easy access.

Another 2 seconds saved!

Posted in Development, SharePoint 2010, SharePoint 2013, Tip | Tagged | Leave a comment