Поделиться через


Passing parameters from a workflow email to a provider hosted app

On a recent project, we had a need to redirect a user from a workflow email to a provider hosted SharePoint application. The scenario is that as the workflow runs it pace (emailing people etc.), we might want one of the steps to be editing the data in a provider hosted or CAM (cloud application model) app. This posed a few issues: how do we pass variables, such as an ItemID to our CAM app, and establish the correct SharePoint context for the provider model. Also there is the whole authentication/authorization issue. As there are a few ways to do this, I am going to show a neat way to handle this with minimal maintenance.

Disclaimer:
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose.

This sample assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. if you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200.

Furthermore, there are many ways to do this. I personally found this to be a nice, easy way to do it.

OK,

The basic idea is, from the workflow email, the user clicks a link to a page inside the host web (SharePoint), then some JavaScript redirects them back to the CAM app with an ItemID in the querystring you can read in the app and load the work item accordingly. What is nice about this approach is redirecting them back to SharePoint first, establishes the authentication and authorization and gives you a chance to track or log activities or anything else before the app. Also you might want to redirect them to another version or some instructional page. Going to SharePoint first allows an easy way to manage this without having to edit the workflow each time. You are basically hardcoding the url to the redirect page in the workflow to a SharePoint page, then in the page redirecting them to the app.

To establish SharePoint context, the framework needs a few things: HostURL, AppURL, Language, ClientTag, ProductNumber and RemoteAppURL. An easy way to get these parameters is clicking on your app the first time. In the querystring you will see all this populated:

https://URL?SPHostUrl=URL&SPLanguage=en%2DUS&SPClientTag=0&SPProductNumber=15%2E0%2E4719%2E1000&SPAppWebUrl=APPWEBURL

The next part is adding the parameters you need, like ItemID and going to the page that loads your work item:

https://URLToCAMApp?ItemID=55&SPHostUrl=URL&SPLanguage=en%2DUS&SPClientTag=0&SPProductNumber=15%2E0%2E4719%2E1000&SPAppWebUrl=APPWEBURL

In your app you will need to read the ItemID and load data etc.

string strItemID = Request.QueryString[“ItemID”];

//Load work item etc.

After all that is setup, the next part is putting some JavaScript on a page in SharePoint that redirects them to your app. I personally like the XML editor over the content editor webpart. So add a webpart to a page named “Redirect.aspx” and add this code:

/*
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose.
This sample assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. if you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200.
*/
<script src="../SiteAssets/jquery-1.11.3.js"></script>

    <script>
        $(document).ready(function () {
            var nItemID = qs("ItemID");
                   
            if(nItemID != -1)
            {
                location.href = "https://AppURL?ItemId=" + nItemID + "ItemID=55&SPHostUrl=URL&SPLanguage=en%2DUS&SPClientTag=0&SPProductNumber=15%2E0%2E4719%2E1000&SPAppWebUrl=APPWEBURL";
            }
        });

        function qs(key) {
            key = key.replace(/[*+?^$.\[\]{}()|\\\/]/g, "\\$&"); // escape RegEx meta chars
            var match = location.search.match(new RegExp("[?&]" + key + "=([^&]+)(&|$)"));
            return match && decodeURIComponent(match[1].replace(/\+/g, " "));
        }
    </script>

After that is in place, calling Redirect?ItemID=55 from your workflow (or anywhere) will launch your CAM application with the correct context and pass the ItemID where you can load the work item.