SharePoint REST Utility

May 28, 2016 by · Leave a Comment
Filed under: REST 

This file may be used to reduce the amount of JavaScript code required to complete a given task while working with the REST API. The code was originally authored by Andrew Connell and made available for viewing on his blog at:

http://www.andrewconnell.com/blog/simplifying-sharepoint-2013-rest-api

The code was rewritten to use the Revealing Module design pattern.

PowerShell Traps vs Try/Catch

June 25, 2014 by · Leave a Comment
Filed under: PowerShell 

This post (http://blogs.msdn.com/b/powershell/archive/2009/06/17/traps-vs-try-catch.aspx) gives a great summary of Traps vs Try/Catch as shown below.

  • Trap:
    • Designed for admins
    • V1 and V2
    • Introduces a new scope
    • Is “global”, meaning it applies to all code in the same scope, before or after.
    • Does not support rethrow (an empty throw statement throws a special RuntimeException with the message “ScriptHalted”)
  • Try/Catch
    • Designed for developers
    • V2 only
    • Does not introduce a new scope
    • Guarded code is in the try statement block, not the entire scope containing the try statement
    • Supports finally
    • Supports rethrowing exceptions

As a developer I am always looking for new ways to improve my code. Pristine error handling is always a goal with any new code, whether it be PowerShell, C#, or something else. Here are some of the rules I try to follow when developing PowerShell scripts for SharePoint:

DO

  • Use $ErrorActionPreference = “Stop” on all scripts so that exceptions are always caught in the “Catch” block. This line should go in the beginning of the “Try” block. Your “Finally” block should contain $ErrorActionPreference = “Continue”.
  • Use a robust logging capability so that you have a record of the changes made by your scripts. Consider using a separate log just for exceptions or anomalies. This can be hugely helpful when debugging your code.
  • Ensure that your “Catch” blocks are ordered from specific to generic. Look for specific errors first, less specific next, and so on all the way to your most generic.

DON’T

  • Don’t use -ErrorAction SilentlyContinue. While it has its place, it is almost always preferable to catch an error rather than silently continuing past it.
  • Don’t overlook the need for proper exception management in all parts of your code. Doing so can result in difficult to recover changes to your SharePoint farm.

Using Extension Methods in your SharePoint Code

October 5, 2012 by · Leave a Comment
Filed under: Extension Methods 

Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.
Read more

How to Determine if a Page in the Master Page Gallery has been Modified

July 15, 2012 by · Leave a Comment
Filed under: Customization 

You may have an occasion where you need to determine which of the pages in the Master Page Gallery have been modified. The code below is intended to create a quick sandboxed code solution that will easily allow you to display all pages in the Master Page Gallery with each items customization status.
Read more

How to Create a List Programmatically

April 5, 2012 by · Leave a Comment
Filed under: Lists 

The following series of classes demonstrate one approach to creating lists programmatically. Please let me know if you have any questions about how this was structured.
Read more

How to Use a SharePoint List to Log Application Errors

March 18, 2011 by · Leave a Comment
Filed under: Logging 

Occasionally, as a developer, you will not have access to the SharePoint log that is maintained within the file system. One way to get around this limitation is to make use of a SharePoint list as a location for logging application errors. This list can easily be created at the site collection root and then given very limited access for viewing. Here’s how it’s done…
Read more

How to Easily Display a Site’s Groups, Users, and Permissions

December 12, 2010 by · Leave a Comment
Filed under: Permissions 

I was recently asked to establish the SharePoint site structure for a fairly large organization. I concluded that, based on the customer’s needs, they would require a single site collection and 17 sub-sites, each with its own unique permissions. As this organization was new to SharePoint, I was concerned that each site might encounter permissions related issues, and quickly identifying the problem could be problematic. So, I created a web part that displays the groups, users, and permissions for each site, and the lists associated with that site. Read more