Automating Office 365 with PowerShell

November 22, 2014 Leave a comment

For those of you attending CodeCamp tomorrow and just can’t wait to get your hands on those demos, here you are.  Enjoy!

Categories: Uncategorized

SharePoint Explorer view as a network drive (Windows 7/8x)

October 15, 2014 Leave a comment

Recently I was asked how to take the nifty SharePoint Explorer view and save a shortcut to it so users don’t need to constantly navigate to the library via the browser to access it.  This of course assumes that you do not want to or can not use OneDrive due to one of it’s many shortcomings.

  1. Ensure your site has been added to the “Trusted Sites” or “Intranet Sites” security zone in IE settings.  If it hasn’t been added, you’ll need to add it then reboot your computer.  If you don’t do this you’ll end up with an access denied error when you attempt to map the network drive.
    Screenshot - 10_21_2014 , 11_03_04 PM
  2. Navigate to the library in your browser and click the “Open with Explorer”.
    MapNetworkDrive-SharePointLib
  3. Depending on your operating system, you’ll need to do one of the following:

    Windows 8x only:

    1. In the explorer window tool bar click Easy access -> Map as Drive.
      MapNetworkDrive-MapDrive
    2. Wait for those slow Windows 7 people to catch up…

    Windows 7/8x:

    1. In the explorer window location bar, copy the location (from “http” to the very end).
      MapNetworkDrive-Win7CopyLocation
    2. Open My Computer from your desktop and select Map Network Drive.
      MapNetworkDrive-Win7MapNetworkDrive
    3. Paste the location we copied in step 2.1 into the folder text box.
      MapNetworkDrive-Win7PasteLocation
  4. In the Map Network Drive window, select the drive letter you wish to map to.
  5. For Office 365 users in the Map Network Drive window, check the box to Connect using different credentials.
    MapNetworkDrive-ChooseDrive
  6. Click Finish.
  7. If prompted, enter your SharePoint username/email and password and click OK.
    MapNetworkDrive-EnterCreds

[Edit – 11/22/2014] I found that this does not work well on Office 365, so if you’re using O365 and want a more permanent desktop integration solution, you’re stuck with OneDrive (until I can find a way to fix it).

Categories: Uncategorized

Creating a Publishing HTML field in SharePoint programmatically

Sooooooo, creating a Publishing HTML field in SharePoint (think wiki Page Content field) is a huge pain (well, the solution is easy, but the figuring it out part was a pain).  However, because I’m extremely busy, I’m going to skip the part where I explain all the hundreds of things I’ve tried and why they all failed, and move straight to the solution:

PowerShell

$web = Get-SPWeb “http://dev01”;
$spList = $web.Lists[“Pages”];
$newField = “<Field Type=’HTML’ DisplayName=’FieldName’ RichText=’TRUE’ RichTextMode=’FullHtml’ />”;
$spList.Fields.AddFieldAsXML($HTMLFieldXML);
$spList.Update();

Categories: SharePoint

SPS Presentation

I just wanted to post a link to my SharePoint Saturday Presentation.  Thanks to everyone for attending and making my first presentation a good one!

Download the slides here

Download the ULS Log Adapter here

Categories: SharePoint

SharePoint 2010 & HTTPS network connections

It’s really frustrating sometimes how Microsoft’s whitepapers are so specific to individual cases that they fail to apply to any useful scenario – particularly those scenarios that involve custom development.  Such is the case for making HTTPS web service calls from SharePoint 2010 (to external endpoints).

The Goal

Custom built web parts deployed into SharePoint 2010 need to make web service calls to a 3rd party API which used an HTTPS endpoint.

The Error

Could not establish trust relationship for the SSL/TLS secure channel with authority ‘[some url]’

My Flailings

The typical cause of this is that the root certificate that the SSL endpoint uses is not trusted by the computer that is attempting to make the web service connection.  The typical resolution?  Open the certificates snap-in through the Windows Management Console and add the root certificate (or even the certificate for the SSL endpoint) to the Trusted Authorities node and you’re all good.  You can even test this by navigating to the endpoint with your browser; if you get a certificate warning it didn’t work, otherwise it’s gravy.

The issue (and the reason for this blog post) is that this doesn’t work; apparently SharePoint completely ignores the machine settings for the trusted root certificates.

The Solution

It’s actually quite simple – open Central Administration and go to the security page.  Find and click the Manage Trusts link. Add a new trust, and in the window that opens upload your root server certificate, give it a good name and boom, things start working – just like magic.

I also found a nice little PowerShell script that will automate the trust creation for you:

$root = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("C:\MyRootCertificate.cer");
New-SPTrustedRootAuthority -Name "my root certificate" -Certificate $root;

Date parsing and formatting in XSL

One task I’ve found to be the bane of my existence when dealing with XSL is date formatting.  There’s no a good way to do it, and it makes things extremely difficult always needing to write new XSL because no 2 dates ever have the same format.

Since I’ve written more than my share of date parsing templates, I decided that I would try to do it once more, and this would be the last time I’d ever write one.  The DateFormatting.xsl contains callable templates for most common operations.

Format Strings

The majority of the templates require a format string for input and/or output.  The syntax for the string is as follows:

m – the numeric value for the month i.e. 8, 9, or 10

mm – the 2 digit padded numeric value for the month i.e. 08, 09, or 10

mmm – the short month name i.e. Aug, Sept, or Oct

mmmm – the long month name i.e. August, September, or October

d – the numeric value for the date i.e. 89, or 10

dd – the 2 digit padded numeric value for the date i.e. 0809, or 10

yy – the 2 digit century i.e. 0809, or 10

yyy – the 4 digit century i.e. 2008, 2009, or 2010

ww – the short day of the week i.e. Sun, Mon, or Tue

www – the full name for the day of the week i.e. Sunday, Monday, or Tuesday

Any other characters are considered delimiters and are not evaluated as part of the date.

So the date 01-Jan-2011 would use the format “dd-mmm-yyy”

Templates

The templates are callable templates.  There are internal templates that begin with an underscore.  The main templates (with no underscore) are mostly wrappers around the internal templates and provide the ability to call the templates with strings instead of structured xml dates – I’ll explain the main ones below:

FormatDate – takes in a date string and returns a new date string in the specified format.  It accepts 3 parameters: inputFormatString, outputFormatString, & dateTimeString.  The inputFormatString specifies the format of the dateTimeString parameter.  The outputFormatString specifies the desired format of the returned date.  The dateTimeString contains the actual date to be formatted (pretty obvious I would think, but you never know).

Example:

.

.

.

Result:   20100130

ParseDate – parses a date and returns a new XML structure containing the raw date values.  Input params are the format string and the date string.

Ex:

.

.

.

Result:

.

.

.

CalculateDayOfWeek – parses a date and returns the numeric day of the week. Sunday = 1, Monday = 2, etc…

Ex:

.

.

.

Result:  4

** The rest of these follow a similar format and return a number or formatted string so I will not include the examples or results for the sake of brevity…

CalculateTotalDaysSinceEpoch – parses a date and returns the number of days since January 1, 0000.

CalculateTotalSecondsSinceEpoch – this method is somewhat pointless since the script does not take into account time – only dates.  So right now this just takes the DaysSinceEpoch and multiplies it by the seconds in a day.

MonthShortString – returns the short name of the provided month.  i.e. “Jan”, “Feb”…

MonthLongString – returns the full name of the provided month. i.e. “January”, “February”…

MonthFromShortName – returns the month number from the provided short name.

MonthFromLongName – returns the month number from the provided long name.

WeekDayLongName – returns the full name of the provided weekday number.

WeekDayShortName – returns the short name of the provided weekday number.

WeekDayFromLongName – returns the day number from the full weekday name.

WeekDayFromShortName – returns the day number from the short weekday name.

You can find the full XSL code along with a test stylesheet and some sample data here.

Categories: Uncategorized

SharePoint 2010 – Cannot complete this action. Please Try again.

February 11, 2011 Leave a comment

Recently while debugging a web part in SharePoint 2010, I received this error message.  I had never seen it before but after doing some searching, I found that apparently this happens quite a lot in SharePoint.

First thing I tried was to open the page in ?contents=1 mode and found the following:

Once the erroneous web parts were removed, I redeployed my solution and added my new webpart onto the page, but alas, still getting the same error.

So, I decided to take a closer look at my code, more specifically, the list I was deploying, and the code used to query it.  After more than a few hours of staring at the answer (and even almost giving up and putting a post for help on stackoverflow.com) I noticed this:

<Where>
  <FieldRef Name="Source" />
  <Value Type="Text">http://dev01:80/Pages/default.aspx</Value>
</Where>

Damn! Poor CAML syntax – the bane of my existance.  After changing the CAML so it was formatted properly:

<Where>
  <Eq>
    <FieldRef Name="Source" />
    <Value Type="Text">http://dev01:80/Pages/default.aspx</Value>
  </Eq>
</Where>

Everything started working as it should.  Why Microsoft decided to stick us with this crappy query language then provide us with no means of validating it and give us completely ambiguous errors and COM errors on top of it all (or below it all depending on how you look at it) just makes me question why I ever decided to use Microsoft products to begin with.

Categories: Uncategorized Tags: ,

SharePoint Themes and how to make them work

November 11, 2010 Leave a comment

I’ll start at the end because I think I’m going to forget this the most often:

How to get a CSS file to be compiled by the theme engine

First of all, use the CssRegistration tag.  You’ll be happy you did when you don’t have to fight with SharePoint to get your CSS file to link properly.

 <SharePoint:CssRegistration Name="<%$SPurl:~SiteCollection/Style Library/en-us/Themable/MyHome.css%>" After="corev4.css" runat="server" />

The “<%$SPurl…” ensures that the url points to the current version (themed or non themed) of the css file.  Be sure to put this tag right after the SharePoint:CssLink tag, I’ve tried putting it in other places and for some unknown reason it breaks things pretty badly.

Place your CSS file under the themeable directory under the Style Library like the URL shown in the CssRegistration tag.

And now the secret sauce – CHECK IN AND PUBLISH YOUR CSS AND IMAGE FILES!

The theme engine will not pick up any non-published versions, you MUST check in your CSS and image files.

Finally, apply a theme – any old theme.

You’ll notice that when you apply the theme, and look at the page source your css files will have moved into a different directory:

 <link rel="stylesheet" type="text/css" href="/_catalogs/theme/Themed/9262B884/MyHome-4277C871.css?ctag=44">

Weird huh?

When you applied the style, the theme engine picked up all the style sheets and images and performed its magic on them.  Then it saved them to this separate location.

This means that any changes you now make to your original css or image files will not be reflected in your newly themed site – yes, that’s right, you need to go back and reapply your theme to the site so the theme engine will recompile the CSS and image files.  Man that’s annoying isn’t it?!

Using themes in your CSS files

Below are a list of the theme commands you can use in your CSS file.  The commands will replace CSS attributes or recolor images as appropriate if used properly.  But first, there’s a mysterious comment at the top of the corev4.css file that I’ve noticed:

/* _lcid=”1033″ _version=”14.0.4762″
_LocalBinding */

I don’t know what it does, and as far as I can tell, this page is the first reference to this comment anywhere on the interwebs.  For now I’ll leave this at the top, because it’s there in corev4.css and I want my style sheet to be just like corev4.css when it grows up.

Next – there are a few commands that can be used to theme-up your stylesheet:

ReplaceColor(themeColor) – should be put before a color attribute (obviously), this will replace the attribute color with a color from your theme.

ReplaceFont(themeFont) – this guy works like ReplaceColor, except with fonts.

RecolorImage(themeColor, method, includeRectangle) – should be put before an image reference – ie. url(<image url>).  This will color the image in the manner specified.  The only required parameter is the color, the other 2 params are optional:

themeColor – this one’s obvious, the color to use to re-color the image.
method – options here are: Filling, Blending, Tinting
includeRectangle – defines the section of the image to recolor i.e. {x:0,y:0,width:15,height:15} defines a rectangle starting at 0, 0 pixels, and is 15px high by 15px wide.

All of the above is pretty self-explanitory with the exception of the method parameter on the RecolorImage – for this, I refer you to an excellent blog post from Microsoft’s SharePoint team in which he shows examples of each recolor method, as well as all the permutations of the different colors.

One last thing – I’ve found themes to be not an extremely awful experience, just a painful one.  However, they can be useful every once in a while in limited situations.  For these limited situations it’s probably necessary to undo all the theming that’s in the corev4.css so that you can implement themes for only the elements that you wish.  The ThemeOverride.css stylesheet will override all the style rules in corev4.css, so that no theme colors or fonts will display.

To use the ThemeOverride.css stylesheet, just insert it between corev4.css and your custom css like so:

      <SharePoint:CssLink runat="server" Version="4" />
      <SharePoint:CssRegistration Name="<%$SPurl:~SiteCollection/Style Library/en-us/Themable/ThemeOverride.css%>" After="corev4.css" runat="server" />
      <SharePoint:CssRegistration Name="<%$SPurl:~SiteCollection/Style Library/en-us/Themable/MyHome.css%>"
           After="<%$SPurl:~SiteCollection/Style Library/en-us/Themable/ThemeOverride.css%>" runat="server" />

Be sure that you specify the ThemeOverride.css in the After attribute of your custom stylesheet.  Otherwise the ThemeOverride.css will be moved after your custom style sheet and will override all of your beautiful CSS work.

Categories: SharePoint, UI

The path specified cannot be used at this time. Exception from (HRESULT: 0x80070094)

November 8, 2010 2 comments

Recently, while attempting to deploy a new WSP file to my dev environment, I was presented with another exceptionally helpful error message:

Error: The path specified cannot be used at this time. Exception from (HRESULT: 0x80070094)

It took me a little bit to figure out, but basically, I reset the SP Timer service and all was well again.

What’s interesting is that even after getting this error, SharePoint claimed that the solution was properly deployed – though I didn’t want to take any chances with finding bugs due to a partially deployed solution.

Categories: Uncategorized

Setup incoming email for your SharePoint development VM

September 3, 2010 Leave a comment

So today I was trying to figure out how to debug an event handler on an email-enabled list.  My dev environment is running on a VM on my laptop so I didn’t really have an exchange server I could hook up to.  So, configuring the SMTP service on my Windows 2008 machine was the way to go.

In server manager, under features, click Add Feature, then select the SMTP service.  Accept the feature dependencies and finish the install.



Open the IIS 6.0 manager, and right-click your new SMTP Virtual Server node, then click properties.  Under access, ensure your authentication method is set to annonymous and your relay restrictions allow anyone.  Remember, this is just a development setup, so hopefully noone will be hacking into your new SMTP server to send out spam for Viagra.

Also, you may want to assign an alias for your domain, but that’s not required.

Next, go into Central Administration -> System Settings -> Configure incoming e-mail settings.  Select advanced settings, and at the bottom plug in your machine name and the email drop folder ‘c:\inetpub\mailroot\drop’ then hit OK.

Once that’s done we just need to enable the list to receive emails.  Just open your list (or create a new one) and go to list settings.  Under the communication section is a link for Incoming e-mail settings.  Select that and choose the settings appropriate for your list.  Note that you may want to configure this through a feature receiver or a list deployed in your solution (after all, isn’t that what we’re here for?).

Now lets deploy and debug our event receiver.  In your Visual Studio solution, click run to deploy and begin debugging the solution.  Now, although normal list event receivers are run on the w3wp.exe process, list event receivers running on e-mail enabled lists will be run by the OWSTIMER.EXE process, so use the Debug -> Attach to Process… option to find it.  You may need to check the Show processes from all users option.

Once we’ve got our event wired up and the proper process being debugged, we just need to fire up and email and see if this thing works.  So, unless you have an email client (or want to install one) on your virutal machine, back out to your host machine.  You’ll need to make sure that your host machine can connect to your VM over the network.  Now, in Outlook, go to Tools -> Account settings… In the email tab, click on New… and select the Microsoft Exchange, POP3, IMAP or HTTP option.  On the next screen, don’t enter anything, but check the box at the bottom of the screen specifying that you want to manually configure your settings, and next choose the Internet email option.

On the next screen you’ll be asked for a bunch of settings for the SMTP server, your email address, user name and password. The important thing here is to set the SMTP server to your VM server address, we don’t have a POP3 or IMAP server set up on the machine, so incoming mail won’t work, but that shouldn’t matter since we just want to send mail to our new SMTP server.  Enter in some test email, and the username and password of your development user.  Click the Test Account Settings… button and you should receive the results I got below.

If you didn’t, it’s most likely because your VM isn’t responding at the address you plugged in for your Outoing mail server (SMTP) value.  Remember, this value doesn’t need to have the domain your VM is on, it just needs to be able to find the SMTP service at that address.  One extra way to test it is to open the mail drop folder (c:\inetpub\mailroot\drop) on your VM and watch the test email file show up there, then promptly disappear when the timer job runs.

Now just create a new email and send it to your new list email address – be sure to use the new account you just created.  Type in your subject and email body and send away.

Categories: SharePoint