Montag, 30. März 2015

ExecuteOrDelayUntilScriptLoaded not working

Problem:
ExecuteOrDelayUntilScriptLoaded in a page or specifically in a publishing page does not work.
This might be also the case when a user who has only read/view access to the site, sp.js refuses to load when ExecuteOrDelayUntilScriptLoaded is used.

Solution:
In SharePoint 2013, the correct way to execute a function after script is loaded is to use SP.SOD class and not ExecuteOrDelayUntilScriptLoaded.
For example I needed to use it in:
 
function openInDialog(dlgWidth, dlgHeight, dlgAllowMaximize, dlgShowClose, pageUrl ) {
      var options = {
          url: pageUrl,
          width: dlgWidth,
          height: dlgHeight,
          allowMaximize: dlgAllowMaximize,
          showClose: dlgShowClose
      };

      options.dialogReturnValueCallback = Function.createDelegate(null, CloseDialogCallback);
      SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);
  }


  function CloseDialogCallback(dialogResult, returnValue) {
      if (dialogResult == SP.UI.DialogResult.OK) { // refresh parent page
          SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.RefreshPage', SP.UI.DialogResult.OK);
      }
      // if user click on Close or Cancel
      else if (dialogResult == SP.UI.DialogResult.cancel) { // Do Nothing or add any logic you want
      } else { //alert("else " + dialogResult);
      }
  }

Sonntag, 22. März 2015

Create client-side email using mailto before server postback

Problem:
So it looks like it isn't a good idea to set the window.location.href before a postback.

 I tried a few options, like using the setTimeout function to force setting window.location.href slightly later in the JS thread lifecycle. But that didn't work; it seemed that any alteration of the DOM's location was causing issues with the postback.

So how can we get the browser to call the mailto link without affecting the postback?
Use an iframe, as the source of the iframe would be separate from the current document and should not interfere with any postbacks.
I modified my CreateEmail function so it would create a new iframe with its src attribute set to the mailto link, append it to the document body so the browser would execute the mailto link, and then remove it again from the DOM:
Solution:

<script type="text/javascript">

  function CreateEmail() {
    var mailto =  'mailto:?to=any@anydomain.com';

    $('<iframe />')
       .prop('src', mailto)
       .css('display', 'none')
       .appendTo('body')
       .remove();
  }
</script>

Montag, 16. März 2015

Setting EventFiringEnabled property from Powershell

You would like to upload documents to a document library via Powershell and disable events that might be fired or don't start workflows on this item change?

How to set the EventFiringEnabled property from Powershell?

Solution:
Actually this is not really something you generally do from PowerShell. The purpose of EventFiringEnabled is to prevent an event receiver from triggering the same event recursively.
However,  you can switch it off for the thread upon which your PowerShell code is running:

$myAss = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");
$type = $myAss.GetType("Microsoft.SharePoint.SPEventManager");
$prop = $type.GetProperty([string]"EventFiringDisabled",[System.Reflection.BindingFlags] ([System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Static));
$prop.SetValue($null, $true, $null);
#code to update list goes here!