Thursday, 31 July 2014

JQuery-Upload: unexpected token

While working with the JQuery-Upload control, I encountered the following error:

unexpected token <

The real kicker was the fact that my target file was still being created.

The cause of the problem (as I eventually discovered) was my REST contract; my endpoint as configured as follows:

[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.WrappedResponse,
UriTemplate = "/file/upload")]
StatusData Upload(Stream fileData);

but my endpoint was returning a JSON response.

This resulted in a mismatch, as the expected response format was XML.

Changing the contract to Json resolved the problem.

Wednesday, 30 July 2014

JQuery-Upload - a few ways to upload a file

I was recently using the magnificent jQuery-File-Upload in a SharePoint 2010 application. I encountered the following scenarios to upload files.

1. Load immediately when the file is selected
This is the easiest option and is pretty much OOTB.
$('#fileupload').fileupload({
                dataType: 'json',
                headers: {
                    Accept: "application/json"
                },
                accept: 'application/json',
                formData: {
                    url: "http://mytargetlocation",
                    title: "myTitle"
                },
                done: function (e, data) {
                    success();
                }
            });

2. Defer load until a button is checked
This code will attach the 'submit' processing to the click event of a button with the ID 'btnSave'

<input id="btnSave" type="submit" >

 $('#fileupload').fileupload({
                dataType: 'json',
                headers: {
                    Accept: "application/json"
                },
                accept: 'application/json',
                formData: {
                    url: "http://mytargetlocation",
                    title: "myTitle"
                },
                add: function (e, data) {
                    data.context = $('#btnSave')
                        .click(function () {
                            data.submit();
                        });
                },
                done: function (e, data) {
                    success();
                }
            });

This approach is great if you are ready for a full postback when the button is clicked.

3. Defer load and load the file in code
This code puts the file in an array, which is used in the 'uploadFile' method to push it to SharePoint

I used this great posting on StackOverflow to find this solution.

var myfiles = [];

 $('#fileupload').fileupload({
                dataType: 'json',
                headers: {
                    Accept: "application/json"
                },
                accept: 'application/json',
                formData: {
                    url: "http://mytargetlocation",
                    title: "myTitle"
                },
                add: function (e, data) {
                    $.each(data.files, function (idx, file) {
                        myfiles.push(file);
                    });
                },
                done: function (e, data) {
                    success();
                }
            });

        function uploadFile() {
            var file = myfiles.pop();
            $('#fileupload').fileupload('send', { files: [file] });
}

Thursday, 10 July 2014

Powershell: How do I execute a script in a new Powershell instance?

The great thing about PS is that is caches its objects for reuse. The bad thing about PS is that is caches its objects for reuse. Sometimes, scripts need to be executed in their own PS instances in order to avoid any changed data.

Here is a simple example of a wrapper script (wrapper.ps1) that executes another script (test.ps1) in the same folder. The test script open notepad with the SchemaXml of a document library.

Wrapper.ps1:
function Get-ScriptDirectory 
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value 
 Split-Path $Invocation.MyCommand.Path 

$dir = Get-ScriptDirectory
cd "$dir"

powershell.exe "& `"$dir\test.ps1`" "

Test.ps1:
function AddSharePointSnapin([bool] $addSnapin)
{
    $sharePointSnapinName = "Microsoft.SharePoint.PowerShell"
    If ($addSnapin)
    {
        Write-Host "Adding $sharePointSnapinName Snapin ..."
        Add-PSSnapin $sharePointSnapinName -ErrorAction SilentlyContinue
    }
    Else
    {
        Write-Host "Removing $sharePointSnapinName Snapin ..."
        Remove-PSSnapin $sharePointSnapinName -ErrorAction SilentlyContinue
    }
}

AddSharePointSnapin $true

$fileName = "doclib.txt"
$web = Get-SPWeb "http://docreval.in.telstra.com.au"
$list = $web.Lists["Documents"]
$list.SchemaXml > $fileName 
Start-Process notepad  -ArgumentList $fileName

Wednesday, 9 July 2014

Powershell; Write a file and open it

I have found this code quite helpful in debugging - especially trying to look through a large xml schema file for a SharePoint list.

$fileName = "myFile.txt"

# Write some variable property to a file
$web = Get-SPWeb "http://blah.com"
$list = $web.Lists["My List"]
$list.SchemaXml > $fileName

# Open the file in notepad
 Start-Process notepad  -ArgumentList $fileName

Monday, 7 July 2014

SharePoint 2010: No item exists at http://site?ID=1. It has be been deleted by another user.

What the heck? I added a query string parameter to pass a value to a REST API and my solution exploded. Fortunately, there is a simple solution provided by our friends at Microsoft (see here).

It seems that the querystring parameter 'ID' is reserved, so renaming it to something else (in my case, docid) resolved the problem.

Wednesday, 2 July 2014

PowerShell: How do I run a Timer Service job?

This is an easy one liner:

Get-SPTimerJob | Where-Object { $_.Name -like "*My Timer Service*" } | Start-SPTimerJob