1

I am combining a MVC 5 SharePoint Provider Hosted App together with pages with just javascript. I created a page with the following javascript:

<script type="text/javascript">

    var hostweburl;
    var appweburl;

    $(document).ready(function () {


        //Get the URI decoded URLs.
        hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
        appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));

        // resources are in URLs in the form:
        // web_url/_layouts/15/resource
        var scriptbase = hostweburl + "/_layouts/15/";

        // Load the js files and continue to the successHandler
        // Load the js files and continue to the successHandler
        $.getScript(scriptbase + "SP.Runtime.js",
            function () {
                $.getScript(scriptbase + "SP.js",
                    function () { $.getScript(scriptbase + "SP.RequestExecutor.js", InitForm); }
                    );
            }
        );

    });

    function getQueryStringParameter(paramToRetrieve) {
        var params =
            document.URL.split("?")[1].split("&");
        var strParams = "";
        for (var i = 0; i < params.length; i = i + 1) {
            var singleParam = params[i].split("=");
            if (singleParam[0] == paramToRetrieve)
                return singleParam[1];
        }
    }
    // prepare the data
    function InitForm() {
        var json;
        // executor: The RequestExecutor object
        // Initialize the RequestExecutor with the app web URL.
        var executor = new SP.RequestExecutor(appweburl);

        var completeUrl = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getByTitle('Ploegen')/items?@target='" + hostweburl + "'";


        var testSource = executor.executeAsync({
            url: completeUrl,
                    method: "GET",
                    headers: { "Accept": "application/json; odata=verbose" },
                    error: function (data) {
                        alert("error: " + data.statusText);
                    },
                    success: function (data) {
                        alert("succes: " + data.d.results);
                        json = data.d.results
                    }
                }).responseText;



    }
</script>

The only problem is that when I run the script I got the message the name 'target' does not exist in the current context.

HostWebUrl and WebAppUrl are filled, but still getting @target errors, even when I am only creating a string with @target init.

What am I doing wrong, because I don't get it?

Thanks

1 Answer 1

0

Finally I found a trick to fix it. Since @target is interpreted by MSIL and not by JS the error CS0103 returns. Compiling and deploying works without issues, but then comes the problem. Even when it is a string it returns CS0103.

So after using: @@target instead of @target the problem resolved. The problem occurs when you use cshtml files instead of *.js files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.