Tuesday, December 12, 2017

SharePoint REST API double encoding field internal names when using $expand=FieldValuesAsText

This is a simple bug I found when building one of our products while it was used by a French speaking customer...

He simply created a column with a french name that had some special characters, for example: "Français"

So, the letter ç was encoded when creating the column's internal name, which is what you'd expect. The column's internal name was: "Fran_x00e7_ais"

When making REST requests to SharePoint to get items, query items, or anything you like - you would get the item field value under this internal name - gain, as you would expect.


If you wanted to get the values as text and used the $expand=FieldValuesAsText in your query (or /FieldValuesAsText if getting a single item) - SharePoint returns something very different.

The value for this column would be set in "Fran_x005f_x00e7_x005f_ais", which is basically a double encoded version of the real internal name: "Fran_x00e7_ais"

I couldn't find a solution for this problem besides hacking it, so now I hard coded updated my code to replace _x005f_ with _ when looking for field values as text...

I'll report this issue but since changing it would potentially break so many existing applications I doubt it would be fixed any time soon.

So for now - keep that in mind, and let me know if there is a better way to finding out the field name that this REST API uses!

Monday, October 30, 2017

Changing SharePoint list item attachment to allow multiple files

I just got this email from a colleague saying he wants to upload multiple files as attachments to an item, and having to click add and confirm each and every one separately is quite annoying and time consuming.

So I wrote a little script that changes this behavior of the OOB file upload to allow multiple files selection.

Suprisingly, it works well with multiple files. there was only one part I had to fix, which was the label it shows before you save the item. It still shows the first file name only.

Here is the JavaScript code you can use to enable it on your system today. Just add this script in your new/edit item forms and it should work:

ExecuteOrDelayUntilScriptLoaded && ExecuteOrDelayUntilScriptLoaded(function(){
var oldShowPartAttachment = window.ShowPartAttachment;
var oldOkAttach = window.OkAttach;
window.OkAttach = function(){
var index = String(FileUploadIndex);
var index2 = String(FileUploadIndex);
if(index2 === index)//it means OK fail.
var files = GetAttachElement(FileuploadString + index);
if(files && files.files.length > 1)//more than one file
var text = "";
for(var i=0;i<files.files.length; i++) text+=', '+ files.files[i].name;
text = text.slice(2);
document.getElementById("attachRow" + index).cells[0].innerHTML = text;
window.ShowPartAttachment = function(){
var index = String(FileUploadIndex);
var files = GetAttachElement(FileuploadString + index);
files.multiple = true;
}}, "form.js");

Also, we will be adding this little script to a future release of our forms solution, so stay tuned for the update.

Let me know if you like it!

Monday, October 16, 2017

SharePoint Online Search not returning all results part 2

Continuing the investigation into SharePoint Online Search not returning all results

We found that SharePoint search API has the "remove duplicates" flag turned on by default.

At first we didn't pay much attention to it, since the results we were missing were not duplicates...

However, after digging some more into it, it seems SharePoint Search was not only removing identical duplicates (meaning, the same item returned twice) but also similar duplicates - meaning items it deems are too similar to one another.

It is on by default, so you should turn it off on every request. It seems to have helped with our issues, we haven't been able to replicate this issue since we turned that off.

In query string, add:

In post, add:
{ '__metadata':{'type':'Microsoft.Office.Server.Search.REST.SearchRequest'}, 'Querytext':'sharepoint', 'TrimDuplicates'='False' }

Read more

The strange thing about it was - why did "Reindex List" button help in some cases? Perhaps there were two issues, we can not be sure.

Hope this helps!