Visualizzazione post con etichetta Carletto Technologist. Mostra tutti i post
Visualizzazione post con etichetta Carletto Technologist. Mostra tutti i post

venerdì 9 ottobre 2015

SharePoint List Item attachment: "old" browser compatibility

Hi everybody!
If you need to add an attachment to an existing list item in SharePoint, which solution would you consider?

Well, using REST API, make a POST call, getting the file content with a File Reader and passing the Blob as body.

Simple, isn't it?

Sure, but which browser are you using? I hope for you Chrome, Firefox and IE...10.
Because, if you need to make the solution compatible with IE9, you will feel much pain.
In fact, reading Marc Anderson's post:
To upload a file to a list you need to make use of the fileReader javascript class, using the readAsDataURL method and stripping the first part off the dataurl to get the base64 component. Then submit this to SPServices.
So, to make the solution working with the bloody IE9, I have used the following ingredients:
  • ngFileReader: angular component, compatible IE8+, that allow you to read the file content through the readAsDataURL method.
  • Stripping the first part of the file content off to get the base64 component.
    var BASE64_MARKER = ';base64,';
    var parts = dataURL.split(BASE64_MARKER);
    contentType = parts[0].split(':')[1];
    var data = parts[1];
  • Calling the $().Service passing the data retrieved above
    var deferred = $q.defer();
    jQuery().SPServices({
      operation: 'AddAttachment',
      async: true,
      listName: listId,
      listItemID: itemId,
      fileName: fileName,
      attachment: data,
      completefunc: function(xData, Status) {
        deferred.resolve(xData);
      }
    });
    return deferred.promise;
Otherwise, if you are using also a "modern" browsers, you can get rid of the previous implementation, creating the Blob from the file content

var BASE64_MARKER = ';base64,';
var parts = dataURL.split(BASE64_MARKER);
contentType = parts[0].split(':')[1];
var raw = window.atob(parts[1]);
var rawLength = raw.length;

var uInt8Array = new Uint8Array(rawLength);

for (var i = 0; i < rawLength; ++i) {
  uInt8Array[i] = raw.charCodeAt(i);
}

return new Blob([uInt8Array], {type: contentType});

and then setting this object (var file in the code snippet below) as data parameter to build the Ajax POST call to the SharePoint Web API endpoint.

var url = _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/GetById('" + listId + "')/items("+ itemId +")/AttachmentFiles/add(FileName='"+ fileName +"')"
      
var params = {
  method: "POST",
  url: url,
  data: file,
  headers: {
    "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
  }          
};

Thats all!

lunedì 24 novembre 2014

SharePoint 2013: Sign in as Different User

SharePoint 2013 does not include the ability to log in as another user...and this sometimes could be painful.

There are several solutions to this problem:
  1. Start your browser as a different user
  2. Use the _layouts/closeConnection.aspx?loginasanotheruser=true page
  3. Add a link to _layouts/closeConnection.aspx?loginasanotheruser=true page either in master page or in the left menu of page that you need to modify adding a security trimmed control in order to render this link only to certain users. For this purpose see Security Trimmed Control
  4. Edit the Welcome.aspx to include the link again (Sign in as Different User and SharePoint 2013)
  5. Use JavaScript as a Bookmarklet (SharePoint 2013 – Sign in as Another User)
I do not suggest the third option: the customization of the layouts pages is not the best solution and it is not safe in case of updates on the server farm (like configuration of service pack, etc).
Moreover, this link cannot be trimmed and probably this functionality have to be avoided to certain users.

My advice is to use other solutions such as the 2, 3 or 5.
For more information, please read the official MSDN article about this problem: "Sign in as Different User" menu option is missing in SharePoint Server 2013

Enjoy!

AC


martedì 4 novembre 2014

JSON in place with IE

How many time have you tried to do a REST call in IE expecting to display the JSON data but the browser IE has prompted to download it?
To me, too many!

The solution to this problem is to update the registry telling IE that for docs that have a mime type of application/json, just view it in place. This won't affect any application/json documents downloaded via script tags, or via XHR, and so on.

How it works

Stop IE. Then, cut and paste the following into a file, by the name of json.reg.
 
 Windows Registry Editor Version 5.00  
 ;  
 ; Tell IE to open JSON documents in the browser.   
 ; 25336920-03F9-11cf-8FD0-00AA00686F13 is the CLSID for the "Browse in place" .  
 ;   
 [HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json]  
 "CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"  
 "Encoding"=hex:08,00,00,00  
 [HKEY_CLASSES_ROOT\MIME\Database\Content Type\text/json]  
 "CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"  
 "Encoding"=hex:08,00,00,00  

Then double-click the .reg file and restart IE. Eventually the response you will have for Content-Type: application/json or Content-Type: text/json will be correct.

 {   
   "OutcomeType": 0,   
   "Message": null,   
   "Exception": null,   
   "Value": {   
     "Detail": {   
       "EquipmentCode": null,   
       "DocumentNumber": "1245",   
       "PurchaseCustomerNumber": "quote",   
       "OrderReason": 0,   
       "CreationDate": "2014-10-31",   
       "StartValidityDate": null,   
       "EndValidityDate": null,   
       "Value": 0,   
       "Currency": null,   
       "Approved": 1  
     },   
     "Items": [   
       {   
         "PositionNumber": "000010",   
         "MaterialCode": "4355656",   
         "Description": "Item 1",   
         "TotalAmount": 12,   
         "SchedulDate": null,   
         "ShippedAmount": 0,   
         "BilledAmount": 0,   
         "NetValue": 96.48,   
         "GrossValue": 100.44,   
         "Currency": "EUR",   
         "SalesUnit": null  
       }  
     ]  
   }  
 }  

For Chrome and FF it is more simple: just download the add-on JSON Viewer!
FF: https://addons.mozilla.org/it/firefox/addon/jsonview/
Chrome: https://chrome.google.com/webstore/detail/json-viewer/gbmdgpbipfallnflgajpaliibnhdgobh

Enjoy!